Deploy Python Lambda functions with container images - AWS Lambda

Deploy Python Lambda functions with container images

Note

End of support for the Python 2.7 runtime starts on July 15, 2021. For more information, see Runtime support policy.

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.

The workflow for a function defined as a container image includes these steps:

  1. Build your container image using the resources listed in this topic.

  2. Upload the image to your Amazon ECR container registry. See steps 7-9 in Create image.

  3. Create the Lambda function and deploy the image.

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 on the Python Package Index (PyPI) website.

You can also download the Python runtime interface client from GitHub.

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.

  1. On your local machine, create a project directory for your new function.

  2. 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 + '!'
  3. 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"]
  4. To create the container image, follow steps 4 through 7 in Create an image from an AWS base image for Lambda.

Adding dependencies when you create a Python image

If your Lambda function depends on external Python libraries, modify the previous procedure as follows:

  1. In your project directory, add a file named requirements.txt. List each required library as a separate line in this file.

  2. Modify your Dockerfile to add the required libraries to the container image. The following example copies the requirements file and installs the required libraries into the app directory. Do not install the dependencies globally or in user space.

    FROM public.ecr.aws/lambda/python:3.8 # Create function directory WORKDIR /app # Install the function's dependencies # Copy file requirements.txt from your project folder and install # the requirements in the app directory. COPY requirements.txt . RUN pip3 install -r requirements.txt # Copy handler function (from the local app directory) COPY app.py . # Overwrite the command by providing a different command directly in the template. CMD ["/app/app.handler"]

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 on the AWS Blog.