Skip to content

Boilerplate code to create, deploy and run AWS Lambda Functions written in Typescript

License

Notifications You must be signed in to change notification settings

adrianolettieri/aws-lambda-typescript

 
 

Repository files navigation

AWS Lambda Typescript Boilerplate Code

Fork this repository to quickstart lambda development with Typescript and CDK and the AWS SDK JS V3

Prerequisites

  • setup AWS CLI
  • run npm ci && cd infrastructure && npm ci && npm dedupe

Commands

  • npm t executes test with jest
  • npm run build creates ./dist/lambda.js bundle
  • npm run zip creates the ./dist/lambda.zip from ./dist/lambda.js r
  • npm run dist runs all of the above steps
  • npm run stack uses CDK to create or update CloudFormation infrastructure, see CDK readme. Add profile if necessary, e.g. -- --profile atombrenner. Will deploy lambda.zip if changed.
  • npm run deploy uses AWS CLI to just deploy the lambda.zip package code to the existing lambda function
  • npm start will run the lambda function locally

Tools

Learnings

Switched to use esbuild for transpiling and bundling lambda typescript source. Compared to webpack, esbuild configuration is minimal and simple and it is unbelievable fast. The generated bundle is slightly larger than with webpack, but for AWS Lambdas a waste of a few kilobytes doesn't matter. The important thing is, that all needed dependencies are bundled and all the noise from node_modules (tests, sources, readme, etc) is excluded. As esbuild is only transpiling typescript, a separate call to tsc run is necessary in npm run dist.

  • generate and use source-maps to have readable stack traces in production
  • --sourcemap --sources-content=false generates a small source-map without embedded sources
  • --keepnames does not minifiy names which makes stack traces even more human readable
  • NODE_OPTIONS=--enable-source-maps enables experimental source-map support in AWS Lambda nodejs

About

Boilerplate code to create, deploy and run AWS Lambda Functions written in Typescript

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 53.6%
  • JavaScript 46.4%