This project provides an example of a full CRUD REST API implemented using the Serverless Framework's Node.js TypeScript template. The remainder of this document was generated by the template.
This project has been generated using the aws-nodejs-typescript
template from the Serverless framework.
For detailed instructions, please refer to the documentation.
Depending on your preferred package manager, follow the instructions below to deploy your project.
Requirements: NodeJS
lts/fermium (v.14.15.0)
. If you're using nvm, runnvm use
to ensure you're using the same Node version in local and in your lambda's runtime.
- Run
npm i
to install the project dependencies - Run
npx sls deploy
to deploy this stack to AWS
- Run
yarn
to install the project dependencies - Run
yarn sls deploy
to deploy this stack to AWS
This template contains a lambda functions triggered by an HTTP requests made on the provisioned API Gateway REST API /todo
and /todo/{id}
routes with GET
, POST
, PUT
, and DELETE
methods. The request body must be provided as application/json
. The body structure is tested by API Gateway against src/functions/{functionName}/schema.ts
- requesting any other path than those defined will result in API Gateway returning a
403
HTTP error code - sending a
POST
request to/todo
with a payload not containing a string property namedtitle
will result in API Gateway returning a400
HTTP error code - sending a
POST
request to/todo
with a payload containing a string property namedtitle
will result in API Gateway returning a200
HTTP status code with a response body containing the created Todo as JSON
⚠️ As is, this template, once deployed, opens a public endpoint within your AWS account resources. Anybody with the URL can actively execute the API Gateway endpoint and the corresponding lambda. You should protect this endpoint with the authentication method of your choice.
In order to test the todo function locally, run the following command:
npx sls invoke local -f listTodos --path src/functions/listTodos/mock.json
if you're using NPMyarn sls invoke local -f listTodos --path src/functions/listTodos/mock.json
if you're using Yarn
Check the sls invoke local command documentation for more information.
Copy and replace your url
- found in Serverless deploy
command output - and name
parameter in the following curl
command in your terminal or in Postman to test your newly deployed application.
curl --request POST \
--header 'Content-Type: application/json' \
--data-raw '{"title":"Wash the dishes","isComplete":false}' \
--verbose \
https://ikqynfzrd3.execute-api.us-east-1.amazonaws.com/dev/todos
curl --verbose \
https://ikqynfzrd3.execute-api.us-east-1.amazonaws.com/dev/todos/35300ed20bb2e853
curl --verbose \
https://ikqynfzrd3.execute-api.us-east-1.amazonaws.com/dev/todos
curl --request PUT \
--header 'Content-Type: application/json' \
--data-raw '{"todoId":"35300ed20bb2e853","isComplete":true,"title":"Wash the dishes"}' \
--verbose \
https://ikqynfzrd3.execute-api.us-east-1.amazonaws.com/dev/todos/35300ed20bb2e853
curl --request DELETE \
--header 'Content-Type: application/json' \
--data-raw '{"todoId":"35300ed20bb2e853","isComplete":true,"title":"Wash the dishes"}' \
--verbose \
https://ikqynfzrd3.execute-api.us-east-1.amazonaws.com/dev/todos/35300ed20bb2e853
The project code base is mainly located within the src
folder. This folder is divided in:
functions
- containing code base and configuration for your lambda functionslibs
- containing shared code base between your lambdas
.
├── src
│ ├── functions # Lambda configuration and source code folder
│ │ ├── createTodo
│ │ │ ├── handler.ts # `Hello` lambda source code
│ │ │ ├── index.ts # `Hello` lambda Serverless configuration
│ │ │ ├── mock.json # `Hello` lambda input parameter, if any, for local invocation
│ │ │ └── schema.ts # `Hello` lambda input event JSON-Schema
│ │ │
│ │ └── index.ts # Import/export of all lambda configurations
│ │
│ └── libs # Lambda shared code
│ └── apiGateway.ts # API Gateway specific helpers
│ └── handlerResolver.ts # Sharable library for resolving lambda handlers
│ └── lambda.ts # Lambda middleware
│
├── package.json
├── serverless.ts # Serverless service file
├── tsconfig.json # Typescript compiler configuration
├── tsconfig.paths.json # Typescript paths
└── webpack.config.js # Webpack configuration
- json-schema-to-ts - uses JSON-Schema definitions used by API Gateway for HTTP request validation to statically generate TypeScript types in your lambda's handler code base
- middy - middleware engine for Node.Js lambda. This template uses http-json-body-parser to convert API Gateway
event.body
property, originally passed as a stringified JSON, to its corresponding parsed object - @serverless/typescript - provides up-to-date TypeScript definitions for your
serverless.ts
service file
Any tsconfig.json can be used, but if you do, set the environment variable TS_NODE_CONFIG
for building the application, eg TS_NODE_CONFIG=./tsconfig.app.json npx serverless webpack