Using AWS Lambda functions with Docker Containers: A Tutorial

Search for a command to run...

Hey there, Nice Post. I made an npm package for handling AWS Lambda responses. Do checkout 😎
AWS-TS This package lets you handle and send responses from AWS lambda with ease. You have the ability to send various types of responses such as JSON or Plain Text without worrying about headers and status codes. You can also enable or disable cors for all or specific responses or set custom headers.
Is the local IDE dead? Sanket Sahu discusses the rise of 'vibe-coding' and how browser-native tools like RapidNative are reshaping the future of mobile app development.

OpenClaw is a powerful, self-hosted AI assistant that connects to your tools to perform actions. Explore its Gateway architecture, real-world use cases, and security precautions.

Discover how neo-brutalism is shaping 2026 design trends. See how anti-design principles can create distinct, usable, and memorable product experiences.

When code breaks a pipeline, developers have to stop working and figure out why. This blog shows how an AI agent reads the error, finds the fix, and submits it for review all on its own.

GeekyAnts built a 5-agent fraud detection pipeline that makes decisions in under 200ms — 15x cheaper than single-model systems, with full explainability built in.

GeekyAnts Tech Blog
349 posts
GeekyAnts is an AI-powered digital product engineering and consulting company helping startups, enterprises, and Fortune 500 brands build scalable, future-ready digital solutions. Since 2006, we have delivered 800+ successful projects for 550+ global clients across healthcare, BFSI, retail, logistics, education, and enterprise technology. We help businesses accelerate digital transformation through strategy, design, engineering, and AI-led innovation.

In December re:Invent 2020, AWS announced a major update for Lambda by introducing support for container images in lambda functions. By nature, Lambda or any other Function-as-a-Service provides benefits like managed scaling, fault-tolerant, and high availability along with pay-as-you-go facility.
In this article, we will see how to integrate docker container with AWS lambda functions and what are the benefits and use cases.
Before AWS joined forces with docker for lambda, there were two options to deploy code in lambda: to either use a build-in code editor on lambda console or via zip package. This zip file contains the code, required dependencies, and libraries required for the code to run. User can manually upload this zip file or use some automation like AWS SAM or third-party services like Serverless Framework. Since many developer clients have invested in Docker-based deployments and CI/CD, with this change in effect, developers can benefit from dockers, as well as server-less functionalities to create a uniform development process.
Lambda functions are developed in such a way that every process is an isolated and immutable entity. This remains the same with container-based functions as well. When a container-based lambda function is called, it runs as-it-is resulting in uniform and immutable deployment packaging among local development, like CI/CD, and Lambda execution environments.
Another benefit is that the runtime package size can now be extended up to 10 GB as compared to the previous 250 MB package limit which prevented many workloads from using lambda. The new size limit allowed many new workload possibilities, which are more dependency heavy or data-heavy processes allowing machine learning and data analytics developers to use packages like NumPy, PyTorch, or other heavy libraries. Even though lambda has layers as a workaround, this came with limitations. With this new feature, all those issues can now be tackled by container-based functions. This also unlocked increased portability amongst different AWS services like AWS Fargate and AWS EC2.
Container-based lambda supports Docker image manifest schema version 1.0 onwards and images from Elastic Container Registry. Lambda is currently providing many base images with pre-installed runtimes including Python, NodeJS, Go, Java, Ruby, and .Net. These images are created and maintained by AWS. Apart from this, developers can create their runtimes based on the Linux kernel.
If developers are using custom Linux kernel for different runtimes (say C++, PHP, elixir, etc) then they don't have access to RIC and RIE which are pre-installed in base images provided by AWS.
RIC and RIE are discussed in length below.
RIC - Runtime Interface Client(s) and RIE - Runtime Interface Emulator are the two new components introduced by AWS in re:Invent 2020.
What are the changes?
What stays the same?
/tmp space - 512 MBTo begin with, let's create a example lambda function with AWSCLI, Docker and NodeJS.
npm, and install faker.js for generating fake testing data, which should look like this:mkdir lambda-docker; cd $_
npm init -y
npm install faker
touch app.js
app.js file and add the following code. This handler code is similar to that of the zip-based lambda function. Let's see how this is done:const faker = require("faker");
module.exports.handler = async (event, context) => {
return faker.helpers.createCard();
};
app.js setup, let's create a Dockerfile and add the following code. This will instruct the docker to install necessary dependencies and create a container from them. The following is an important step as it also informs RIC about the handler function via the CMD attribute like this:FROM public.ecr.aws/lambda/nodejs:12
COPY app.js package*.json ./
RUN npm install
CMD [ "app.lambdaHandler" ]
docker build -t lambda-docker-demo .
<accountID> with your AWS account ID and <region> with your AWS region, as shown below:aws ecr create-repository --repository-name lambda-docker-demo --image-scanning-configuration scanOnPush=true
docker tag lambda-docker-demo:latest <accountID>.dkr.ecr.<region>.amazonaws.com/lambda-docker-demo:latest
aws ecr get-login-password | docker login --username AWS --password-stdin <accountID>.dkr.ecr.us-east-1.amazonaws.com
docker push <accountID>.dkr.ecr.<region>.amazonaws.com/lambda-docker-demo:latest
The output should look like this:
The push refers to repository [718679627680.dkr.ecr.ap-south-1.amazonaws.com/lambda-docker-demo]
787f8ab2d1e8: Pushed
e0fd0b7ddcc7: Pushed
5bff738ecfb2: Pushed
9104caec20a8: Pushed
d6fa53d6caa6: Pushed
b0c5f6ff5d8a: Pushed
6cd95e8f3d80: Pushed
fe6098a9ee94: Pushed
latest: digest: sha256:6f30e39d7a8a372e6dd1377a741cb5bede8de0315b82d4caaa3666643b555b4d size: 1998
The next step will create an image in the ECR console:

Create Function to create and deploy the function, as shown below:
Following successful deployment, the lambda function can be tested the same way as regular lambda functions. Move to the Test tab, choose a new event and invoke it with the empty object and you will see the output from faker.js. This looks like:

We can automate the whole image build and deployment process using AWS Serverless Application Model(SAM). For this, AWS SAM CLI has to be installed locally. In this walkthrough, the same function will be deployed but using SAM CLI. SAM CLI's init command will create a basic function and a Dockerfile. Next, you can use SAM's build and deploy command to automate building, creating, and publishing the image on lambda, as explained.
To start off, follow these instructions from the local terminal:
sam init in the project directory to start SAM Wizard.1 – AWS Quick Start Templates’ from the options provided.2 - Image.1 – amazon/nodejs12.x-base.
lambda-docker-demo as the project name, this will create a sample project with README and unit tests.hello-world/app.js and add the following code to generate a fake response:const faker = require("faker");
module.exports.handler = async (event, context) => {
return faker.helpers.createCard();
};
npm install to install faker.js.sam build
bash deploy --guided

lambda-docker-demo, and then enter your respective AWS region ; In the case of Image Repository, we can give the same URL as of the previous implementation from the AWS ECR console. This will create a function and add an API endpoint using AWS API Gateway, as shown below:
The AWS-provided RIE(Runtime Interface Emulator) can be used for testing lambda functions locally before deployment. To test, you need to open two terminals, wherein, from one terminal we'll start the docker container, and from the other terminal we've to send a POST request to that docker for testing. To process, run the following code:
# Starting docker container and publishing port 9000.
docker run -p 9000:8080 lambda-docker-demo:latest
This will start an HTTP server where you can send a POST request, as shown:
# From other terminal use cURL to send a request.
curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"test": "value"}'

With the new docker integration, its power can be used in lambda's execution environment. The new 10GB size limit opens up many use cases for lambda which were simply too hard to achieve before. AWS SAM can also be used to reduce boilerplate code and handle the building and deployment process.
Thank you so much for reading 😁.