How to package and deploy a Lambda function as a container image

How to package and deploy a Lambda function as a container image


AWS Lambda allows you to run your code inside Docker containers! This feature opens up a lot of possibilities. You can use any programming language or framework by packaging it into a container image.
There are no limitations from Lambda’s built-in runtimes. Deploying Lambda functions also becomes much easier. 

Instead of zipping up code and dependencies, we can build a Docker image with everything our function needs. Just push the image to Amazon’s container registry and configure Lambda to use it.

In this blog post, we’ll show you how to create a Lambda function that runs in a Docker container.

Amazon ECR:

Amazon Elastic Container Registry (ECR) is a fully managed container registry provided by AWS. It allows you to store, manage, and deploy container images securely. 

Key points about ECR:

  • Eliminates the need to operate your own container repository.
  • Integrates natively with other AWS services like ECS, EKS, and Lambda.
  • Provides high-performance hosting for your images.
  • Automatically encrypts images at rest and transfers them over HTTPS.
  • Offers resource-based permissions and lifecycle policies for images.

Using ECR with AWS Lambda:

Traditionally, you deployed Lambda functions by uploading a ZIP file containing your code and dependencies. However, AWS now allows you to package your Lambda function code as a container image and deploy it from ECR.

The main benefits of using container images for Lambda functions include:

  • Use any programming language, framework, or base image by customizing the container.
  • Larger deployment packages up to 10GB compared to 50MB zipped archives.
  • Easier dependency management and code organization within the container.
  • Consistent build and deployment process across different environments.

To use a container image for your Lambda function, you first build and push the Docker image to an ECR repository. Then, you create the Lambda function and specify the ECR image URI as the deployment package.

Image description

Hands-On walkthrough:

To show how this works in practice, this walkthrough uses an environment with the AWS CLI, python, and Docker installed.

Create a Dockerfile file and paste the following code:

FROM amazon/aws-lambda-python:3.10 COPY ./ CMD ["app.handler"]
Enter fullscreen mode
Exit fullscreen mode

Create as lambda handler:

import sys
def handler(event, context): return 'Hello from AWS Lambda using Python container image' + sys.version + '!'
Enter fullscreen mode
Exit fullscreen mode

Next, run the following command in the console to authenticate to the ECR registry.

Replace with the account ID of the AWS account you are using and with the code of the region you are using.

aws ecr get-login-password --region <Region> | docker login --username AWS --password-stdin <Account-ID>.dkr.ecr.<Region>
Enter fullscreen mode
Exit fullscreen mode

Create a new repository in ECR and push the Docker image to the repo.

aws ecr create-repository --repository-name demo-lambda --image-scanning-configuration scanOnPush=true
Enter fullscreen mode
Exit fullscreen mode

Discover more from Coursity

Subscribe to get the latest posts to your email.


No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *