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"
Choose "Container image", give any name, add image URI (can be obtained from AWS ECR) and click 'Create function'
To test the function, add a trigger
Choose API Gateway as a trigger and create an HTTP API and leave the security to open (for simplicity)
Once the trigger has been created, copy the endpoint URL and paste it in the browser
It should show you the response content
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
Top comments (0)