How do I use container images with Lambda?

3 minute read
0

I want to create AWS Lambda container images with dependencies. How do I do that?

Resolution

Verify that you have permissions configured for the IAM user or role that creates the Lambda container images. Then, follow these steps to use Docker to deploy your container images:

1.    Go to the Get Docker webpage. Choose the Docker Desktop application that meets your requirements. Follow the directions provided to install Docker Desktop.
Note: Docker is a third-party website.

2.    On your local machine, create one folder with three files: Dockerfile, requirements.txt with libraries, and app.py with import statements. Note: Use the latest version of Python for Amazon Elastic Container Registry (Amazon ECR) public registry.

This example Dockerfile uses Python 3.8:

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" ]

This example app.py file includes an example import statement:

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

3.    Build your Docker image with the docker build command and an image name.

This is a docker build command with a “hello-world” example:

docker build -t hello-world .

4.    Start the Docker image with the docker run command.

This is a docker run command with a “hello-world” example:

docker run -p 9000:8080 hello-world

5.    Test your application by using the Lambda Runtime Interface Emulator (RIE). From a new terminal window, post an event to the following endpoint using a curl command:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}'

This command invokes the function running in the container image and then returns a response.

6.    Authenticate the Docker CLI to your Amazon ECR registry. Change the account ID and the AWS Region in the command to make sure that it meets your requirements.

This is an example of authenticating the Docker CLI to the Amazon ECR registry:

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 123456789012.dkr.ecr.us-east-1.amazonaws.com

7.    Create a repository in Amazon ECR using the create-repository command.

This is an example create-repository command:

aws ecr create-repository --repository-name hello-world --image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLE

8.    Tag your image to match your repository name using the docker tag command. Then, deploy the image to Amazon ECR using the docker push command.

Change the account ID and the AWS Region in these commands to ensure that they meet your requirements.

This is an example docker tag command:

docker tag hello-world:latest 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest

This is an example docker push command:

docker push 123456789012.dkr.ecr.us-east-1.amazonaws.com/hello-world:latest

9.    With your container image in the Amazon ECR container registry, create and run a Lambda function. For more information, see Creating the function.


AWS OFFICIAL
AWS OFFICIALUpdated a year ago
3 Comments

For an unauthenticated pull from an Amazon ECR Public repository :

docker logout public.ecr.aws

Troubleshooting reference : https://docs.aws.amazon.com/AmazonECR/latest/public/public-troubleshooting.html

replied 8 months ago

The Sample Dockerfile should be updated

From

COPY requirements.txt . RUN pip3 install -r requirements.txt —target "${LAMBDA_TASK_ROOT}"

To

COPY requirements.txt . RUN pip3 install -r requirements.txt —target "${LAMBDA_TASK_ROOT}"

replied 8 months ago

Thank you for your comment. We'll review and update the Knowledge Center article as needed.

profile pictureAWS
MODERATOR
replied 8 months ago