DEV Community

Mubbashir Mustafa
Mubbashir Mustafa

Posted on

Deploy containerized functions on AWS Lambda

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

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.

// A sample function to demo containers deployment on aws lambda
exports.helloLambda = async (event) => {
  const response = {
    isBase64Encoded: false,
    statusCode: 200,
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({
      message: "Containers on lambda!🐳",
    }),
  };
  return response;
};
Enter fullscreen mode Exit fullscreen mode

Create a Dockerfile with the following content

FROM amazon/aws-lambda-nodejs:12
COPY functions.js package*.json ./
# RUN npm install // uncomment if your functions has dependencies
CMD [ "functions.helloLambda" ]
Enter fullscreen mode Exit fullscreen mode

Build, tag and push the image to ECR*

aws ecr get-login-password --region <region-name> | docker login --username AWS --password-stdin <ecr-repo-uri-without-tag>

docker build -t node-app .

docker tag node-app:latest <ecr-repo-uri-without-tag>/<repo-name>:latest

docker push <ecr-repo-uri-without-tag>/<repo-name>:latest


Enter fullscreen mode Exit fullscreen mode

*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 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 pre-built image.


Let's connect:

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

Twitter: https://twitter.com/mubbashir100

Discussion (0)