How to use Docker in AWS Lambda?

AWS CDK Logo
AWS CDK Logo

Size. Much more size. With Docker images you can package and deploy Lambda functions as container images of up to 10 GB in size. In this way, you can also easily build and deploy larger workloads that rely on sizable dependencies, such as machine learning or data intensive workloads.

In this post we will show you how to deploy your AWS Lambda so that it uses a Docker image with AWS CDK.

Photo by Mehmet Ali Peker on Unsplash

🎓At the end of this post

You will have deployed an AWS Lambda function that runs your Python code. This allows you to bypass the size limitations of Lambda while you trade in some invocation/initialization time.

📝 Requirements — Level 100

This post requires very limited knowledge of AWS CDK. You should generate an sample project using the CLI or you can check out the source code of any of the other CDK posts.

  • You have an functionable AWS account
  • You have installed and a a starter understanding of AWS CDK

💼 Use cases

Serverless Machine Learning? That used to be a dream with the size limitations AWS Lambda had to offer. Data Scientists want to use libraries that are often big in size: Panda’s, Matplotlib, etc. With this Docker image implementation you won’t have to think about that anymore.

📚Tutorial

  1. Generate a new CDK project

You will first need to create a new folder and initialize your CDK project in it.

mkdir cdk-docker-lambda
cd cdk-docker-lambda
cdk init app --language typescript

2. Install the aws-lambda library

In order to use and deploy the Docker image you will need to use the DockerImage construct from the aws-lambda library. In order to do so:

npm i @aws-cdk/aws-lambda --save

3. Create your CDK construct

Constructs are the basic building blocks of AWS CDK apps. A construct represents a “cloud component” and encapsulates everything AWS CloudFormation needs to know.

First, import the newly installed library in your app.js:

import * as lambda from "@aws-cdk/aws-lambda";

And then use the DockerImage class of the aws-lambda library to create your new AWS Lambda:

const DockerImageFunction = new lambda.DockerImageFunction(scope, 'DockerImageFunctionHandler', {
code: lambda.DockerImageCode.fromImageAsset(path.join(__dirname, '../../lambda/lambdaname'), {
cmd: ["entryfile.handler"],
entrypoint: ["/lambda-entrypoint.sh"],
}),
memorySize: 10240,
timeout: Duration.seconds(40)
});

In this definition we are pointing the class towards a folder two levels up (lambda), called containing a subfolder (lambda name) where the dockerfile and actual code live.

4. Deploy the stack

That’s all you needed! The final step is to deploy the CDK stack to your account. To do so:

cdk deploy

🧽 Cleanup and Conclusion

If you are not continuing your project make sure to run the destroy command to remove all resources from your AWS account:

cdk destroy

NBTL

If you are interested in learning more about AWS CDK and how you can build applications on AWS with it, we recommend checking out similar posts on our blog:

By Mart

Tutorials at nbtl.blog

Leave a comment

Your email address will not be published.

Exit mobile version