Deploy containerized functions on AWS Lambda - Serverless

Deploy containerized functions on AWS Lambda - Serverless

ยท

2 min read

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.

// 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;
};

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" ]

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

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


Originally posted on: dev.to


Let's connect:

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

Twitter: https://twitter.com/mubbashir100

ย