AWS Lambda
Advanced Topics
Lambda Containers Demonstration
In this tutorial, we’ll guide you through building, pushing, and deploying an AWS Lambda function packaged as a container image, using the AWS Management Console.
Environment Setup
Before you begin, ensure you have the following:
- A local development workstation (Windows, macOS, or Linux)
- An IDE such as Visual Studio Code
- Docker Desktop installed and running
- The AWS CLI configured with proper credentials
- An active AWS account logged into the AWS Management Console
Project Structure
Create a new directory for your demo and add the following files:
File | Purpose |
---|---|
app.py | Python Lambda handler |
requirements.txt | Python dependencies |
Dockerfile | Container build instructions |
app.py
import sys
def handler(event, context):
return 'Hello from KodeKloud with AWS Lambda using Python ' + sys.version + '!'
requirements.txt
Leave this file empty for no external dependencies, or list any libraries your function needs.
Note
If you require additional packages, add them to requirements.txt
before building the image.
Dockerfile
FROM public.ecr.aws/lambda/python:3.8
# Install dependencies
COPY requirements.txt .
RUN pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
# Copy function code
COPY app.py ${LAMBDA_TASK_ROOT}
# Define the handler
CMD [ "app.handler" ]
Create an ECR Repository
The AWS Elastic Container Registry (ECR) hosts your container images. In the AWS Management Console, navigate to Elastic Container Registry.
Click Create repository, set visibility to Private, and name it KodeKloudDemo
. Optionally enable image scanning on push, then click Create repository.
Once the repository appears, select View push commands.
Build and Push the Container Image
Open your terminal and follow these steps, replacing <region>
and <account-id>
with your AWS Region and account number.
Warning
Keep your AWS credentials secure. Do not hard-code them in scripts.
Authenticate Docker to ECR:
aws ecr get-login-password --region <region> \ | docker login --username AWS --password-stdin <account-id>.dkr.ecr.<region>.amazonaws.com
Build your image locally:
docker build -t kodeklouddemo .
Tag the image for your repository:
docker tag kodeklouddemo:latest \ <account-id>.dkr.ecr.<region>.amazonaws.com/kodeklouddemo:latest
Push the image to ECR:
docker push <account-id>.dkr.ecr.<region>.amazonaws.com/kodeklouddemo:latest
Example response after login:
Login Succeeded
Successful push output:
... latest: digest: sha256:573b0a9049137d606c681c973b197727ace46f6ebbed4bff7eb2e61f0f1 size: 2205
Refresh the ECR console to verify the latest tag.
Deploy the Lambda Function
- Go to AWS Lambda in the AWS Management Console.
- Click Create function, choose Container image, and enter
KodeKloudDemo
as the function name. - Under Container image, click Browse images, select your
kodeklouddemo:latest
image, and click Select image. - Keep the remaining settings at their defaults and click Create function.
After provisioning, you’ll see your new function listed in the console.
Test the Lambda Function
- In your Lambda function console, click Test.
- Choose Create new test event (the default template is fine) and save.
- Click Test again to invoke the function.
- Confirm the Execution results show your greeting message and Python version.
Summary
In this demo, you learned how to:
- Structure a simple Python Lambda project
- Create a Dockerfile compatible with AWS Lambda
- Build and push a container image to Amazon ECR
- Deploy a Lambda function from your container image
- Invoke and test the Lambda function via the AWS Console
Links and References
Watch Video
Watch video content