Skip to content
/ lambda-ts Public template

Template for developing AWS Lambda functions in Typescript

Notifications You must be signed in to change notification settings

TimHuynh0905/lambda-ts

Repository files navigation

AWS Lambda in Typescript Template

Template for developing and provisioning AWS Lambda Function in Typescript

Overview

Motivations

AWS Sam CLI - a great tool for developing and deploying Lambda function - only supports TypeScript in Beta (by 12/11/2022).

Also, I want to use Webpack to bundle AWS Lambda function handler and its dependencies instead of esbuild or tsc as illustrated in the docs.

Provisioning AWS Lambda function in a synchronized CI/CD Pipeline involves setting up many cloud resources which should be simplified for increasing productivity.

I truly believe this template can help you focus more on the coding process and be less worried about the underlying infrastructure.

Architecture

Getting Started

These instructions will give you a kickstart on developing and deploying AWS Lambda function.
Feel free to use and customize this template for your own preferences.

Prerequisites

Before you start, please make sure to have the following technologies installed and configured.

Installing

# Install dependencies
npm install

npm install installs Prettier, husky, and commitlint which are used to ensure code and commit messages conforms to consistent style.

Usage

# Run local lambda with live-reloading
npm run dev

npm run dev runs webpack --watch and sam local start-api concurrently. Webpack watches for changes to TypeScript code under src/ and updates the JavaScript bundle under build/. sam local start-api automatically refers to these changes and reloads local server.

Finally, this process achieves my desired live-reloading feature for developing and testing AWS Lambda function locally with TypeScript. Learn more here.

Developer should leverage AWS SAM CLI functionalities to work with AWS Lambda functions and other Serverless resources.

Note: Live-reloading requires Docker to run in the background to simulate AWS Lambda runtime environment.

Testing

I use Jest to run unit-tests on the source code. Write more tests **.test.ts under tests/ folder

# Run unit-tests
npm run test

Troubleshooting

If sam-cli returns the following error (which happened to me)

npm run dev
...
Docker error: "Cannot connect to the Docker daemon. Is the docker daemon running on this host?"

1. Make sure Docker is running
2. Make sure sam-cli can communicate with docker daemon by exporting DOCKER_HOST to your local machine's env. DOCKER_HOST(i.e. docker.sock) is the UNIX socket that Docker daemon is listening to.

# 1. Locate docker.sock location on your local machine
docker info # Try other methods of it does not work
...
ERROR: Cannot connect to the Docker daemon at unix:///.../docker.sock. Is the docker daemon running?

# 2. Export this path to DOCKER_HOST environment variable
export DOCKER_HOST=unix:///.../docker.sock

Deployment

Make sure that you have Terraform CLI installed.

Under terraform/, there are *.tf files which define resources needed for the CI/CD pipeline.

Required environment variables are AWS_REGION, AWS_ACCOUNT_ID, LAMBDA_FUNCTION_NAME. Define these variables in terraform/variables.tfvars

# terraform/variables.tfvars
AWS_REGION = "{{region}}"
AWS_ACCOUNT_ID = "{{ID of AWS account}}"
LAMBDA_FUNCTION_NAME = "{{name of lambda function}}"

# Before deployment
cd terraform
terraform init
chmod +x ./apply.sh

# Run deployment
./apply.sh
# OR
terraform apply -var-file="variables.tfvars"

Author