atombrenner / aws-lambda-typescript

Boilerplate code to create, deploy and run AWS Lambda Functions written in Typescript
GNU General Public License v3.0
56 stars 22 forks source link

AWS Lambda Typescript Boilerplate Code

Fork this repository to quickstart lambda function development with Typescript. Perfect for microservices.

Features

Prerequisites

Commands

Hint: Currently the region is hardcoded to eu-west-1. TODO: AWS environment parameter should work. Example

AWS_REGION=eu-central-1 AWS_PROFILE=atombrenner npm run stack

Tools

Deprecated Tools

Learnings

Replaced ts-node with tsx

I used ts-node for many years, it helped me and many of my teams to write automation scripts in TypeScript instead of bash or ruby. With the introduction of ESM and the slow migration from CommonJS to ESM correct configuration of ts-node got harder and harder. With node 20 ts-node stopped working for ESM modules at all. Not the fault of ts-node (see here for details) but even after months it was never fixed or the workaround was too esoteric for me. I found tsx, which is powered by esbuild, and it works like a charm for scripts that are part of an esm package without special config.

Dropped CDK

Dropped CDK because it was too heavy-weight for simple lambda microservices. It was hard to maintain a second package.json and tsconfig.json just for CDK. Having a single Cloudformation template and deploy it via API is much faster and easier to maintain. The function can be updated (deployed) by a simple API call, decoupled from other infrastructure updates. Deploying a new version or rolling back to an old one takes only a few seconds.,

Using esbuild

Switched to use esbuild for transpiling and bundling lambda typescript source. Compared to webpack, esbuild configuration is minimal and it is unbelievably 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 for npm run dist.