Skip to content

Node.js and Typescript serverless boilerplate using AWS SAM template.

License

Notifications You must be signed in to change notification settings

kshyun28/aws-sam-template-node-ts

Repository files navigation

aws-sam-template-node-ts

Deployment

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.

Development Setup

  1. Create a new repository using this template. Refer to the documentation for a more detailed walkthrough.

  2. Clone this repository.

  3. Install dependencies.

npm install
  1. Setup git submodule.
git submodule update --init --recursive
  1. Setup husky git hooks
yarn husky install

SAM CLI Setup

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.

Update configurations for local development and deployment

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

Using Lambda Layers already deployed in AWS

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.

Using SAM CLI for development

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.

Using AWS SAM CLI for deployments

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

Fetch, tail, and filter Lambda function logs

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.

Unit tests

Tests are defined as files with filename ${file}.tests.ts in this project. Jest is the test framework used.

yarn test

Add a resource to your application

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.

Resources

See the AWS SAM developer guide for an introduction to SAM specification, the SAM CLI, and serverless application concepts.

Additional Resources on serverless

Additional resources on code setup

About

Node.js and Typescript serverless boilerplate using AWS SAM template.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages