Deploy Python Lambda functions with container images
Deploy Python Lambda functions with container images

Deploy Python Lambda functions with container images

You can deploy your Lambda function code as a container image. AWS provides the following resources to help you build a container image for your Python function:

  • AWS base images for Lambda
  • These base images are preloaded with a language runtime and other components that are required to run the image on Lambda. AWS provides a Dockerfile for each of the base images to help with building your container image.
  • Open-source runtime interface clients
  • If you use a community or private enterprise base image, add a runtime interface client to the base image to make it compatible with Lambda.

What’s In This Document

🚀 Create Docker image container

1. Create an image from an alternative base image

Dockerfile.alter

FROM python:3.8-buster

RUN apt-get update && \
apt-get install -y python3-pip && \
pip3 install awslambdaric

COPY app.py ./

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD ["app.handler"]

2. Create an image from AWS base image

Dockerfile.aws

FROM amazon/aws-lambda-python:3.8

COPY app.py ./

CMD ["app.handler"]

🚀 Create app.py as lambda handler

import sys


def handler(event, context):
return 'Hello from AWS Lambda using Python' + sys.version + '!'

🚀 Build and push the image to ECR

aws ecr get-login-password --region ap-southeast-1 | docker login --username AWS --password-stdin 111111111111.dkr.ecr.ap-southeast-1.amazonaws.com
docker build -t pythonlambda .
docker tag pythonlambda:latest 111111111111.dkr.ecr.ap-southeast-1.amazonaws.com/pythonlambda:latest
docker push 111111111111.dkr.ecr.ap-southeast-1.amazonaws.com/pythonlambda:latest

🚀 Create Lambda container image

Alt Text
Alt Text

🚀 Test the lambda function

1. Invoke lambda function

  • Start container
docker rm -f lambdaimagetest
docker run -d --name lambdaimagetest -it pythonlambda:latest bash
docker exec -it lambdaimagetest bash
  • Invoke lambda function
⚡ $ aws lambda invoke --function-name python-lambda-image --region ap-southeast-1 outfile                                                                                                                          
{
"StatusCode": 200,
"ExecutedVersion": $LATEST"
}
⚡ $ cat outfile
"Hello from AWS Lambda using Python3.8.5 (default, Aug 5 2020, 08:22:02) \n[GCC 8.3.0]!"

2. Call lambda API from local container

  • This should be performed from AWS image base
  • Run container
⚡ $ docker run -d --name testapi -p 9000:8080 pythonlambda:latest
  • Call API
⚡ $ curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'
"Hello from AWS Lambda using Python3.8.6 (default, Dec 16 2020, 01:05:15) \n[GCC 7.3.1 20180712 (Red Hat 7.3.1-11)]!"
  • Deploy new image
Alt Text
Alt Text
  • Invoke lambda function
⚡ $ aws lambda invoke --function-name python-lambda-image --region ap-southeast-1 outfile                                                                                                                          
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}
⚡ $ cat outfile
"Hello from AWS Lambda using Python3.8.6 (default, Dec 16 2020, 01:05:15) \n[GCC 7.3.1 20180712 (Red Hat 7.3.1-11)]!"

🌠 Blog · Web · Linkedin · Group · Page · Twitter 🌠

--

--

--

Awesome Devops || CloudOpz

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Working part time in an Agile organisation

Why should we use Kotlin instead of Java

Embedded Linux 101

My daughters are in ballet class. Every Saturday, I

Useful Pandas Tricks — Part 1

In-depth review of Power Virtual Agents & CRM Bot — Part 3 (Channels, Fulfilment & Pricing)

How to Find Right Blogging Platform for Your Business in 2019

Python and Pandas

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vu Dao

Vu Dao

Awesome Devops || CloudOpz

More from Medium

Migrating MySQL timestamp from non-UTC Database to RDS with AWS DMS

CDK Example — How to create a S3 secure Bucket resource

Logging and Monitoring Elastic Beanstalk with New Relic