Create an AWS Lambda Function using a Container Image and Amazon ECR as Repository
Introduction
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.
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.2</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
- Create a Dockerfile
FROM public.ecr.aws/lambda/java:8
# 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 [ "com.serverless.dev.hello.App::handleRequest" ]
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
- 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 292602749294.dkr.ecr.us-east-2.amazonaws.com
2. Build your Docker image
docker build -t hello .
3. Tag your image
docker tag hello:latest 292602749294.dkr.ecr.us-east-2.amazonaws.com/hello:latest
4. Push the image to your ECR repository
docker push 292602749294.dkr.ecr.us-east-2.amazonaws.com/hello:latest
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.
Conclusion
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.