Deploy containerized functions on AWS Lambda
Tags: aws, beginners, devops, javascript
Published: 2021-10-31 19:21:31

---
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](https://github.com/mubbashir10/aws-proton-nodejs-sample.git)***, otherwise follow along.

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


```javascript
// 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](https://docs.docker.com/engine/reference/builder/) with the following content
```dockerfile
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*
```bash
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](https://dev.to/mubbashir10/push-docker-images-to-aws-ecr-4l7e)***


#### Deploy The Image on AWS Lambda:
From the AWS Lambda landing page, select "Create function"
![AWS Lambda Homepage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arejevk7jt9t40fes4ku.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3g99e44jwmuxrh6kd5h.png)

To test the function, add a trigger
![Add trigger to AWS Lambda Function](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ihzf7orjvli2v9tuf98.png)
 
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wpw9omnbq0ktv602wexf.png)

Once the trigger has been created, copy the endpoint URL and paste it in the browser
![API Gateway and AWS Lambda](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r54lcul8yd4gvbx0ydec.png)

It should show you the response content
![API Gateway and AWS Lambda](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3xfh2am2owrs29o1b2n9.png)

---

For containers to work with AWS Lambda, you can either use the open-source [base container images](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-images.html#runtimes-images-lp) that AWS Provides or you can add [lambda runtime interface clients](https://docs.aws.amazon.com/lambda/latest/dg/runtimes-images.html#runtimes-api-client) to your base images. In the tutorial, we have used a pre-built images.

---
Let's connect: 

Linkedin: [https://www.linkedin.com/in/mubbashir10/](https://www.linkedin.com/in/mubbashir10/) 

Twitter: [https://twitter.com/mubbashir100](https://twitter.com/mubbashir100)