This project is a template for quickly setting up a boilerplate for new APIs. It contains the source code and supporting files for a serverless application using AWS SAM. It includes the following files and folders.
- src - Code for the application's Lambda function written in TypeScript.
- layers - Shared code and dependencies via Lambda layers.
- samconfig.toml - Config for deployment in AWS.
- template.yaml - A template that defines the application's AWS resources.
- .husky - Contains git hooks such as lint-staged and commitlint.
- .commitlintrc.json - Contains config for commit lints.
- .eslintrc.json - Contains config for eslint.
- .lintstagedrc.json - Contains commands ran when committing staged files.
- .prettierrc.json - Contains config for formatting code style.
- jest.config.ts - Contains config for running tests with Jest.
- tsconfig.json - Contains config for development and compilation with TypeScript.
-
Create a new repository using this template. Refer to the documentation for a more detailed walkthrough.
-
Clone this repository.
-
Install dependencies.
npm install
- Setup git submodule.
git submodule update --init --recursive
- Setup husky git hooks
yarn husky install
The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API.
To use the SAM CLI, you need the following tools.
- SAM CLI - Install the SAM CLI
- Node.js - Install Node.js 16, including the NPM package management tool.
- Docker - Install Docker community edition
Before deploying the application, we need to update the samconfig.toml
and template.yaml
files.
For samconfig.toml
, update the following fields:
- stack_name to the repository's name.
- s3_prefix
For the template.yaml
, update the following:
- POWERTOOLS_SERVICE_NAME
- POWERTOOLS_METRICS_NAMESPACE
- Resource properties
In order to pull the Lambda layer arn:aws:lambda:ap-southeast-1:853919431213:layer:${layer_name}:${version_number}
, AWS credentials must be configured using aws-cli.
Follow the steps here to configure AWS CLI profiles.
After configuring AWS CLI, running AWS SAM CLI commands like sam build
should work. sam deploy
will only work in the AWS account where the Lambda layer was deployed. It will not work between AWS accounts.
For syncing code and resources quickly into AWS, use sam sync
.
sam sync --stack-name test-dev --watch
You can find more information and examples about using sam sync
in the SAM CLI Documentation.
Build your application with the sam build
command. Builds are required everytime you make a change to your code before testing or deploying to AWS.
sam build
Deploy the application.
sam deploy --config-env develop --parameter-overrides Environment=develop
To simplify troubleshooting, SAM CLI has a command called sam logs
, which lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find bugs.
sam logs -n GetFunction --stack-name sample-stack-name --tail
You can find more information and examples about filtering Lambda function logs in the SAM CLI Documentation.
Tests are defined as files with filename ${file}.tests.ts
in this project. Jest is the test framework used.
yarn test
The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in the SAM specification, you can use standard AWS CloudFormation resource types.
See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.