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.
AWS base images for Python
AWS provides the following base images for Python:
Tags | Runtime | Operating system | Dockerfile |
---|---|---|---|
3, 3.8 |
Python 3.8 | Amazon Linux 2 | Dockerfile for Python 3.8 on GitHub |
3.7 |
Python 3.7 | Amazon Linux 2018.03 | Dockerfile for Python 3.7 on GitHub |
3.6 |
Python 3.6 | Amazon Linux 2018.03 | Dockerfile for Python 3.6 on GitHub |
2, 2.7 |
Python 2.7 | Amazon Linux 2018.03 | Dockerfile for Python 2.7 on GitHub |
Docker Hub repository: amazon/aws-lambda-python
Amazon ECR repository: gallery.ecr.aws/lambda/python
Python runtime interface clients
Install the runtime interface client for Python using the pip package manager:
pip install awslambdaric
For package details, see Lambda RIC
You can also download the Python runtime interface client
Deploying Python with an AWS base image
When you build a container image for Python using an AWS base image, you only need to copy the python app to the container and install any dependencies.
To build and deploy a Python function with the python:3.8
base image.
-
On your local machine, create a project directory for your new function.
-
In your project directory, add a file named
app.py
containing your function code. The following example shows a simple Python handler.import sys def handler(event, context): return 'Hello from AWS Lambda using Python' + sys.version + '!'
-
Use a text editor to create a Dockerfile in your project directory. The following example shows the Dockerfile for the handler that you created in the previous step.
FROM public.ecr.aws/lambda/python:3.8 COPY app.py ./ CMD ["app.handler"]
-
To create the container image, follow steps 4 through 7 in Create an image from an AWS base image for Lambda.
Create a Python image from an alternative base image
For an example of how to create a Python image from an Alpine base image, see Container image support for Lambda