Ashish Jadhao
Journey to the Cloud

Journey to the Cloud

Create an AWS Lambda Function using a Container Image and Amazon ECR as Repository

Create an AWS Lambda Function using a Container Image and Amazon ECR as Repository

Ashish Jadhao
·Jan 11, 2021·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article


One of the major updates of AWS re:Invent 2020 on serverless was the ability to package and deploy a lambda function using a container image. This will be really helpful for organizations with container-based architectures in place and want to utilize the benefits of Lambda. In addition to packaging as a container image, AWS now supports package sizes up to 10GB.

In this article, we will see how to build a container image for your java code, push it to Amazon Elastic Container Registry(ECR), and create a Lambda function using the URI of your container image.

Getting Started

Create container image

You should have Docker installed on your machine to complete this tutorial.

  • I have written a simple lambda function in java . Few things to note here is the way dependencies are copied along with the package.


  • Create a Dockerfile
# Copy function code and runtime dependencies 
COPY target/classes ${LAMBDA_TASK_ROOT}
COPY target/dependency/* ${LAMBDA_TASK_ROOT}/lib/
# Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
CMD [ "" ]

We can either use AWS provided base images or Alternative base images. In this tutorial I have used AWS provided base image.

  • Next you need to package your code using
mvn clean package
  • Head over to your ECR console in AWS account and create a repository to store your container image


  • Once the repository is created, you can see the list of commands you can use to build an image and push it to ECR.


  • Enter those commands in your terminal
  1. Retrieve an authentication token and authenticate your Docker client to your registry.
aws ecr get-login-password --region us-east-2 | docker login --username AWS --password-stdin

2. Build your Docker image

docker build -t hello .


3. Tag your image

docker tag hello:latest

4. Push the image to your ECR repository

docker push


Create a lambda function

  • In the Lambda console, create a Lambda function as a container image and select the URI of the image you pushed to ECR in the previous steps.


  • Create an event and test your Lambda function.



AWS surely has taken a step forward to help organizations with container image support for Lambda with image sizes up-to 10GB. In this article, we saw how we can package and deploy our java lambda function as a container image stored in Amazon ECR.


Did you find this article valuable?

Support Ashish Jadhao by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this