This project has been generated using the aws-nodejs-typescript
template from the Serverless framework.
For detailed instructions, please refer to the documentation.
Some personal modifications are:
- Git hooks with Husky, running ESLint, Prettier and Tests on every commit.
- Created a directory for Architecture Decision Records, as described by Michael Nygard.
- Open API documentation with redoc-cli.
- CloudWatch Alerts with serverless-plugin-aws-alerts.
- Prune of Old Lambda Versions with serverless-prune-plugin.
- aws-vault for using securely store and access AWS credentials in a development environment.
- mermaid for rendering graphs in markdown files
- httpie http client for the terminal (like curl just better)
- Run
yarn
to install the project dependencies - Run
yarn sls deploy
to deploy this stack to AWS - Run
npx sls deploy --aws-profile <profile> --stage <stage>
to use the sls from node_module - Run
yarn sls <profile> <deploy | remove> <stage>
to securely deploy or remove to AWS using aws-vault
This template contains a single lambda function triggered by an HTTP request made on the provisioned API Gateway REST API /hello
route with POST
method. The request body must be provided as application/json
. The body structure is tested by API Gateway against src/functions/hello/schema.ts
JSON-Schema definition: it must contain the name
property.
- requesting any other path than
/hello
with any other method thanPOST
will result in API Gateway returning a403
HTTP error code - sending a
POST
request to/hello
with a payload not containing a string property namedname
will result in API Gateway returning a400
HTTP error code - sending a
POST
request to/hello
with a payload containing a string property namedname
will result in API Gateway returning a200
HTTP status code with a message saluting the provided name and the detailed event processed by the lambda
⚠️ 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 hello function locally, run the following command:
yarn sls invoke local -f hello --path src/functions/hello/mock.json
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 --location --request POST 'https://myApiEndpoint/dev/hello' \
--header 'Content-Type: application/json' \
--data-raw '{
"name": "Frederic"
}'
Another way is to use httpie
:
https https://myApiEndpoint/play/hello < src/functions/hello/mock.json
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
│ │ ├── hello
│ │ │ ├── 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
│
├── package.json
├── serverless.ts # Serverless service file
├── tsconfig.json # Typescript compiler configuration
└── 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 - redoc-cli - provides a tool to configure and write our documentation using OpenAPI Specification.
- serverless-prune-plugin - prunes old lambda versions.
- serverless-plugin-aws-alerts - creates alerts automatically in CloudWatch.
graph TD;
A-->B;
A-->C;
B-->D;
C-->D;