Skip to content

Latest commit

 

History

History
111 lines (69 loc) · 6.49 KB

python-image.md

File metadata and controls

111 lines (69 loc) · 6.49 KB

Deploy Python Lambda functions with container images

Note
End of support for the Python 2.7 runtime started 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.

    For runtimes that use the Amazon Linux 2 operating system, AWS provides base images for x86_64 architecture and arm64 architecture.

  • Open-source runtime interface clients (RIC)

    If you use a community or private enterprise base image, you must add a Runtime interface client to the base image to make it compatible with Lambda.

  • Open-source runtime interface emulator (RIE)

    Lambda provides a runtime interface emulator for you to test your function locally. The base images for Lambda and base images for custom runtimes include the RIE. For other base images, you can download the RIE for testing your image locally.

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 or update the function code to deploy the image to an existing function.

Topics

AWS base images for Python

AWS provides the following base images for Python:

Tags Runtime Operating system Dockerfile
3, 3.9 Python 3.9 Amazon Linux 2 Dockerfile for Python 3.9 on GitHub
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

Create a Python image from 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.

If your function has dependencies, your local Python environment must match the version in the base image that you specify in the Dockerfile.

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. In your project directory, add a file named requirements.txt. List each required library as a separate line in this file. Leave the file empty if there are no dependencies.

  4. 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. Install any dependencies under the ${LAMBDA_TASK_ROOT} directory alongside the function handler to ensure that the Lambda runtime can locate them when the function is invoked.

    FROM public.ecr.aws/lambda/python:3.8
    
    # Copy function code
    COPY app.py ${LAMBDA_TASK_ROOT}
    
    # Install the function's dependencies using file requirements.txt
    # from your project folder.
    
    COPY requirements.txt  .
    RUN  pip3 install -r requirements.txt --target "${LAMBDA_TASK_ROOT}"
    
    # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
    CMD [ "app.handler" ]
    
  5. 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

When you use an alternative base image, you need to install the Python runtime interface client

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.

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.

Deploy the container image

For a new function, you deploy the Python image when you create the function. For an existing function, if you rebuild the container image, you need to redeploy the image by updating the function code.