Dev to DevOps

Practical insights for devs to learn DevOps
Back to Posts

Deploy containerized functions on AWS Lambda

Published: 10/31/2021

series: Containers on AWS

AWS Lambda lets you deploy containerized functions by packaging your AWS lambda function code and dependencies using Docker up to a size of 10GB. Here's a tutorial to demonstrate how you can containerize and deploy nodejs based lambda functions.

Prepare Container Image for AWS Lambda:

If you prefer you can clone the repo, otherwise follow along.

Create a file called functions.js inside your node project and add the following sample function to it.

1// A sample function to demo containers deployment on aws lambda
2exports.helloLambda = async (event) => {
3  const response = {
4    isBase64Encoded: false,
5    statusCode: 200,
6    headers: {
7      "Content-Type": "application/json",
8    },
9    body: JSON.stringify({
10      message: "Containers on lambda!🐳",
11    }),
12  };
13  return response;
14};

Create a Dockerfile with the following content

1FROM amazon/aws-lambda-nodejs:12
2COPY functions.js package*.json ./
3# RUN npm install // uncomment if your functions has dependencies
4CMD [ "functions.helloLambda" ]

Build, tag and push the image to ECR*

1aws ecr get-login-password --region <region-name> | docker login --username AWS --password-stdin <ecr-repo-uri-without-tag>
2
3docker build -t node-app .
4
5docker tag node-app:latest <ecr-repo-uri-without-tag>/<repo-name>:latest
6
7docker push <ecr-repo-uri-without-tag>/<repo-name>:latest
8
9

*Learn how to publish images on ECR

Deploy The Image on AWS Lambda:

From the AWS Lambda landing page, select "Create function"

AWS Lambda Homepage

Choose "Container image", give any name, add image URI (can be obtained from AWS ECR) and click 'Create function'

AWS Lambda Function with Container Configuration

To test the function, add a trigger

Add trigger to AWS Lambda Function

Choose API Gateway as a trigger and create an HTTP API and leave the security to open (for simplicity)

Add API Gateway trigger to AWS Lambda function

Once the trigger has been created, copy the endpoint URL and paste it in the browser

API Gateway and AWS Lambda

It should show you the response content

API Gateway and AWS Lambda


For containers to work with AWS Lambda, you can either use the open-source base container images that AWS Provides or you can add lambda runtime interface clients to your base images. In the tutorial, we have used a pre-built images.


Let's connect:

Linkedin: https://www.linkedin.com/in/mubbashir10/

Twitter: https://twitter.com/mubbashir100