Master AWS Lambda Deployment Using Containers: From Zero to Hero

Introduction
AWS Lambda is a serverless compute service that allows you to run code without provisioning or managing servers. It automatically scales and executes your code in response to events, such as changes in data or system state, or user actions. With Lambda, you only pay for the compute time you consume, making it a cost-effective solution for many workloads. Lambda supports multiple programming languages and integrates seamlessly with other AWS services, enabling rapid development and deployment of scalable applications.
Types of Options Available While Deploying Lambda
When deploying a Lambda function, AWS provides several options to suit different use cases:
Author from Scratch
Start with a simple “Hello World” example by writing your own code directly in the Lambda console or uploading a ZIP file.Use a Blueprint
Build a Lambda application from sample code and configuration presets designed for common use cases, helping you get started quickly with best practices.Container Image
Select and deploy a container image for your Lambda function. This option allows you to package your code and dependencies using Docker images, making it easier to manage complex dependencies and leverage existing container workflows.
Moving Forward
Today, we are going to focus on how to run a Lambda function using a container image. This approach is particularly useful when your application has complex dependencies or you want to standardize your deployment process using containerization.

Steps to Follow
You can follow these steps to deploy a Lambda function with a container image:
Prepare Your Container Image
Create a Dockerfile that implements the AWS Lambda Runtime Interface, ensuring your application code can interact with Lambda’s event-driven model.
Sample Dockerfile
FROM public.ecr.aws/lambda/python:3.12
# Install dependencies
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
# Copy lambda code
COPY lambda_function.py .
# Set handler: <filename>.<function_name>
CMD ["lambda_function.lambda_handler"]
2. Build the Container Image
Use Docker to build your image locally, packaging all necessary code and dependencies.
There is some recommendation for docker build for lambda function.
RECOMMENDED & PRODUCTION-SAFE)
Classic docker build (Docker v2 manifest – BEST for Lambda)
👉 Use this approach always for AWS Lambda
👉 Produces Docker manifest v2 (Lambda-supported)
👉 Avoids OCI index + provenance issues
Folder Structure
boat@DESKTOP-5TCOMOB:~/lambda-docker/lambda$ tree
.
├── Dockerfile
├── lambda_function.py
└── requirements.txt
1 directory, 3 files
docker build \
--platform=linux/amd64 \
--provenance=false \
--no-cache \
-t text-to-speech-lambda:v1.0.0 .

3. Publish the Image to Amazon ECR
Push your container image to Amazon Elastic Container Registry (ECR), which serves as a secure repository for your images.
ECR login and repository creation with tag and push commands below.
# Login to Private ECR
aws ecr get-login-password --region <AWS_REGION> \
| docker login \
--username AWS \
--password-stdin <AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com
# Create Private ECR Repository (One-Time)
aws ecr create-repository \
--repository-name <ECR_REPO_NAME> \
--region <AWS_REGION>
# Build Docker Image (Lambda-Compatible)
docker build \
--platform=linux/amd64 \
--provenance=false \
--no-cache \
-t <ECR_REPO_NAME>:<IMAGE_TAG> .
# Tag Docker Image for ECR
docker tag <ECR_REPO_NAME>:<IMAGE_TAG> \
<AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/<ECR_REPO_NAME>:<IMAGE_TAG>
# Push Image to Private ECR
docker push \
<AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/<ECR_REPO_NAME>:<IMAGE_TAG>
# Verify image in ECR
aws ecr describe-images \
--repository-name <ECR_REPO_NAME> \
--region <AWS_REGION>
# Verify Manifest Type (Lambda Safe)
docker manifest inspect \
<AWS_ACCOUNT_ID>.dkr.ecr.<AWS_REGION>.amazonaws.com/<ECR_REPO_NAME>:<IMAGE_TAG>
# Expected output
# application/vnd.docker.distribution.manifest.v2+json



4. Create the Lambda Function
In the AWS Lambda console, select the option to deploy from a container image, and provide the ECR image URI.
Follow below approach to create lambda function
We will:
Create an IAM role for Lambda
Attach required permissions
Create the Lambda function from an ECR image
Test the Lambda function
Prerequisites
AWS CLI v2 configured (
aws configure)Docker image already pushed to ECR
Image must be linux/amd64, single-manifest
STEP 1: Create IAM Role for Lambda
1.1 Create Trust Policy
Create a file trust-policy.json:
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
1.2 Create the IAM Role
aws iam create-role \
--role-name lambda-execution-role \
--assume-role-policy-document file://trust-policy.json

1.3 Attach Basic Execution Policy
aws iam attach-role-policy \
--role-name lambda-execution-role \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
STEP 2: (Optional) Wait for IAM Role Propagation
IAM roles take a few seconds to propagate.
sleep 10
STEP 3: Create Lambda Function from ECR Image
aws lambda create-function \
--function-name ai-tts-lambda \
--package-type Image \
--code ImageUri=<account-id>.dkr.ecr.ap-south-1.amazonaws.com/text-to-speech-lambda:v1.0.0 \
--role arn:aws:iam::<account-id>:role/lambda-execution-role \
--region ap-south-1

STEP 4: Verify Lambda Creation
aws lambda get-function \
--function-name ai-tts-lambda \
--region ap-south-1
If this returns metadata → Lambda is created successfully ✅

STEP 5: Create Test Event
Create event.json:
{
"text": "Testing Lambda version 1"
}

STEP 6: Invoke Lambda Function (IMPORTANT FLAG)
aws lambda invoke \
--function-name ai-tts-lambda \
--payload file://event.json \
--cli-binary-format raw-in-base64-out \
response.json \
--region ap-south-1
STEP 7: View Response
cat response.json
Output Response
{"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": "{\"message\": \"Text converted to speech successfully\", \"lambda_image_version\": \"v3.0.0\", \"audio_base64\": \"//OExAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA

STEP 8: From Text to MP3: Implementing Text-to-Speech
We have response.js now convert it into audio_base64
cat response.json \
| jq -r '.body' \
| jq -r '.audio_base64' > audio.b64
Decode base64 to mp3
base64 --decode audio.b64 > output.mp3

Now play the audio
mpg123 output.mp3

Final Thoughts
Thank you for following along with this hands-on guide to deploying AWS Lambda functions using container images. By using Docker, Amazon ECR, and AWS Lambda, you can build flexible and scalable serverless applications that support custom dependencies and complex workloads such as Text-to-Speech.
This approach simplifies application packaging and allows you to run and test your Lambda functions locally before deployment, ensuring better reliability and consistency across environments. Container-based Lambda deployments are especially useful for modern cloud applications where control over the runtime environment is essential.
In the coming days, we will explore how to automate this setup using CI/CD pipelines and further enhance deployment workflows. Stay tuned for more practical guides and real-world use cases around building efficient, cloud-native applications on AWS. 🚀
Follow for many such contents:
LinkedIn: linkedin.com/in/dushyant-kumar-dk
Blog: dashboard-blog-link



