You can deploy your TypeScript code to an AWS Lambda function as a Node.js container image. AWS provides base images for Node.js to help you build the container image. These base images are preloaded with a language runtime and other components that are required to run the image on Lambda. AWS provides a Dockerfile for each of the base images to help with building your container image.
If you use a community or private enterprise base image, you must add the Node.js runtime interface client (RIC) to the base image to make it compatible with Lambda. For more information, see Creating images from alternative base images.
Lambda provides a runtime interface emulator (RIE) for you to test your function locally. The base images for Lambda and base images for custom runtimes include the RIE. For other base images, you can download the RIE for testing your imagelocally.
Prerequisites
To complete the steps in this section, you must have the following:
-
Docker
The following instructions use Docker CLI commands to create the container image. To install Docker, see Get Docker on the Docker website.
-
Node.js 14.x or later
To create an image from an AWS base image for Lambda
-
On your local machine, create a project directory for your new function.
-
Create a new Node.js project with
npm
or a package manager of your choice.npm init
-
Add the @types/aws-lambda and esbuild packages as development dependencies.
npm install -D @types/aws-lambda esbuild
-
Add a build script to the package.json file.
"scripts": { "build": "esbuild index.ts --bundle --minify --sourcemap --platform=node --target=es2020 --outfile=dist/index.js" }
-
Create a new file called index.ts. Add the following sample code to the new file. This is the code for the Lambda function. The function returns a
hello world
message.import { Context, APIGatewayProxyResult, APIGatewayEvent } from 'aws-lambda'; export const handler = async (event: APIGatewayEvent, context: Context): Promise<APIGatewayProxyResult> => { console.log(`Event: ${JSON.stringify(event, null, 2)}`); console.log(`Context: ${JSON.stringify(context, null, 2)}`); return { statusCode: 200, body: JSON.stringify({ message: 'hello world', }), }; };
-
Create a new Dockerfile with the following configuration:
- Set the
FROM
property to the URI of the base image. - Set the
CMD
argument to specify the Lambda function handler.
Example Dockerfile
The following Dockerfile uses a multi-stage build. The first step transpiles the TypeScript code into JavaScript. The second step produces a container image that contains only JavaScript files and production dependencies.
FROM public.ecr.aws/lambda/nodejs:16 as builder WORKDIR /usr/app COPY package.json index.ts ./ RUN npm install RUN npm run build FROM public.ecr.aws/lambda/nodejs:16 WORKDIR ${LAMBDA_TASK_ROOT} COPY --from=builder /usr/app/dist/* ./ CMD ["index.handler"]
- Set the
-
Build your image.
docker build -t hello-world .
-
Authenticate the Docker CLI to your Amazon Elastic Container Registry (Amazon ECR) registry.
aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com
-
Create a repository in Amazon ECR using the
create-repository
command.aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE
-
Tag your image to match your repository name, and deploy the image to Amazon ECR using the
docker push
command.docker tag hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest
-
Create and test the Lambda function.
To update the function code, you must create a new image version and store the image in the Amazon ECR repository. For more information, see Updating function code.