AWS Logo
Menu
How to Deploy Lambda in Docker using CDK

How to Deploy Lambda in Docker using CDK

This guide shows how to deploy AWS Lambda as a Docker container using AWS CDK

Published Sep 20, 2024
Last Modified Oct 31, 2024
Introduction
AWS Lambda provides a powerful serverless architecture, enabling developers to focus solely on code without managing infrastructure. For many use cases, packaging dependencies using AWS Lambda Layers is sufficient. However, for more complex applications, where multiple dependencies or custom configurations are required, deploying Lambda as a Docker container offers a more streamlined solution.
In this article, I will demonstrate how to deploy AWS Lambda as a Docker image. While you can still use AWS Lambda Layers for dependency management, the Docker approach simplifies packaging everything including custom libraries and configurations into a single, portable container. Additionally, Docker allows you to locally test the image before deployment, making the development process more flexible.
We’ll cover the entire packaging process, deploy using AWS CDK for automation, and expose the AWS Lambda function through a URL, enabling API interaction. This method not only provides flexibility in managing complex dependencies but also allows you to package virtually any application as a Docker image and run it within AWS Lambda.
Moreover, the principles for keeping your Docker image lightweight such as minimizing unnecessary layers and using small base images apply to AWS Lambda Docker deployments as well. Following these practices ensures faster startup times and more efficient execution in production.

The benefits of packaging Lambda as a container are:

  1. Local development and testing
  2. You can dockerize your code and deploy it in AWS Lambda
  3. Package all the dependencies
Pre- Requirements:
  1. AWS Cli and profile configure
  2. AWS CDK installed.
Let's dive in and explore the structure of the CDK project!

Step 1

Create the DockerFile and Lambda code using the following structure for this example I am using Python and the source image is pre-building the AWS slim Python container
Below is the structure of the AWS CDK project for your reference.
CDK Project Structure
CDK Project Structure
1 . Dockerfile lib/functions/demo-image
Requirements file requirements.txt Use all the libraries that your image requires here.
src/main.py - This is your lambda code

Step-2 CDK code to deploy the AWS Lambda

Here is the snippet of the code for the Stack "lambda-stack-docker-demo.ts"
Note: In this example, we are exposing the Lambda function publicly without any authentication. For enhanced security, you have the option to deploy the AWS Lambda URL using authentication methods as well. using AWS_IAM for more information please read the AWS documentation.

Step -3 List of Stack

cdk list
List of Stacks

Step -4 Test the docker locally

  • Build your image locally and navigate to /lib/functions/demo-image
  • Run the command to build the image docker build -t demo-image .
  • Run the container docker run -p 9000:8080 demo-image
  • Run curl command curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
Here is the output
Local testing

Step -5 Let's deploy our AWS Lambda in AWS and test

After the lambda is deployed get the Function URL and test your lambda
To test the lambda use the console or simply run the following curl command
Curl

Conclusion

Deploying AWS Lambda as a Docker container offers flexibility, allowing you to package complex dependencies and test locally before deployment. Using AWS CDK streamlines the process, enabling automated deployments and easy API exposure. By following best practices, like keeping Docker images slim, you can ensure optimal performance and faster startup times. With this approach, you have a powerful and scalable solution for serverless applications.
This is just the beginning of a series of in-depth articles! Next up, we'll explore how to deploy and run a simple scikit-learn classification model in AWS Lambda. Stay tuned for more insights and hands-on guides!"
 

Comments