By using AWS re:Post, you agree to the AWS re:Post Terms of Use

How do I use container images with Lambda?

3 minute read
0

I want to create AWS Lambda container images with dependencies.

Resolution

Verify that you have permissions configured for the AWS Identity and Access Management (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 page on the Docker website. Choose the Docker Desktop application that meets your requirements. Follow the directions provided to install Docker Desktop.

  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.

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

    The following example app.py file includes an example import statement:

    import sysdef 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 .

    Note: If you get an authentication error when pulling from the Amazon ECR public repository, then see Amazon ECR Public troubleshooting.

  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 with the Lambda Runtime Interface Emulator (RIE) from AWS on the GitHub website. From a new terminal window, run a curl command to post an event to the following endpoint:

    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 your account ID and Region.

    This is an example command to authenticate 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 with the docker tag command. Then, deploy the image to Amazon ECR with the docker push command.
    Change the account ID and the AWS Region in the command to your account ID and Region.

    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 Create a Lambda function using a container image.

AWS OFFICIAL
AWS OFFICIALUpdated 4 months 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 a year 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 a year ago

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

profile pictureAWS
EXPERT
replied a year ago