How To Run Docker Containers in AWS Lambda

Varun Singh
3 min readMay 16, 2021
Image By: Varun Singh

Lambda has served many happy customers who do not wish to manage servers while developing the code. The users need to upload the code and Lambda will take care of execution using a specified execution environment.

AWS Lambda has served us well, by providing Layers so that we can install third party libraries for our functions.

Third party libraries that are not available within the base environment provided by the Lambda runtime, you will use layers. Also, with time one will add more such libraries. Hence, Lambda layers is not a solution for this problem.

To help you with that, you can now package and deploy Lambda functions as container images of up to 10 GB in size. This way, you can also build and deploy larger workloads. Containers packaged as ZIP archives and deployed as container images provides the same automatic scaling, high availability, and native integrations with many AWS services.

To deploy a container image, you can select one from an Amazon Elastic Container Registry repository. Here is a sample Docker file:

FROM public.ecr.aws/lambda/python:latest


COPY myLambda.py ./


CMD ["myLambda.handler"]

Now there are TWO scenarios here:

➡️ Building images with base image provided by AWS — Build your container images with base runtimes provided by AWS Lambda. Available runtime are listed Here. You can build your docker images and push it to Amazon ECR. After that, create a lambda function by selecting your uploaded container image. So simple, right!, you can get started by pulling one of the base docker images provided by Amazon ECR as shown below:

ECR Gallery Image

➡️ Running custom runtimes that are not provided by AWS Lambda — I mean, what if you wish to run your service in python 3.9.x, that is not provided by Lambda. This is where the only condition that I specified above comes in handy:

👉 The custom container images must implement the Lambda Runtime API.

How to run docker images in AWS Lambda?

Well, just like a normal Lambda function, that gets invoked manually or by an event. Invocation of lambda images is no different than running your non-containerized functions.

👉 You can invoke your containerized function even with API gateway.

What about Cold start issues when running containers in Lambda?

According to AWS survey, there is no substantial proof that containers running in lambda has any different cold start issues compared to when running without containers. Anyways, if you are still concerned about cold starts in AWS Lambda, I have written one good article on how to reduce cold starts by using Lambda Warmers. Here is the link — Avoid Cold Starts in AWS Lambda.

I will be publishing one more article on, How to build container images using serverless. Stay tuned for more updates.

More about the Author

I am a full-time software engineer with 3.5+ years of experience in Python, AWS, and many more technologies. I have started writing recently as it helps me to read more. I am looking forward to sharing technical knowledge and my life experiences with people out there.

Follow me for more interesting articles on Python and AWS. You can read more of my blogs and findings on Medium — Varun Singh.

--

--

50K+ views | Data Analyst turned Software Developer | Mentor | I can help you become Data Engineer | Connect with me ➡️ https://topmate.io/varunsingh