Deploy Java Lambda functions with container images
You can deploy your Lambda function code as a container image. To help you build a container image for your Java function, AWS provides the following resources:
-
AWS base images for Lambda
These base images are preloaded with a language runtime and other components that are required to run the image on Lambda. AWS provides a Dockerfile for each of the base images to help with building your container image.
-
Open-source runtime interface clients
If you use a community or private enterprise base image, you must add a runtime interface client to the base image to make it compatible with Lambda.
-
Open-source runtime interface emulator
Lambda provides a runtime interface emulator (RIE) for you to test your function locally. The base images for Lambda and base images for custom .runtimes include the RIE. For other base images, you can download the RIE for testing your image locally.
The workflow for a function defined as a container image includes these steps:
-
Build your container image using the resources listed in this topic.
-
Upload the image to your Amazon Elastic Container Registry (Amazon ECR) container registry.
-
Create the function or update the function code to deploy the image to an existing function.
AWS base images for Java
AWS provides the following base images for Java:
Tags | Runtime | Operating system | Dockerfile | Deprecation |
---|---|---|---|---|
17 |
Java 17 | Amazon Linux 2 | Dockerfile
for Java 17 on GitHub |
|
11 |
Java 11 | Amazon Linux 2 | Dockerfile
for Java 11 on GitHub |
|
8.al2 |
Java 8 | Amazon Linux 2 | Dockerfile
for Java 8 on GitHub |
|
8 |
Java 8 | Amazon Linux | Dockerfile
for Java 8 on GitHub |
Amazon ECR repository: gallery.ecr.aws/lambda/java
Using a Java base image
To complete the steps in this section, you must have the following:
-
Java (for example, Amazon Corretto
)
To upload the image to Amazon ECR and create the Lambda function
-
Run the get-login-password
command to authenticate the Docker CLI to your Amazon ECR registry. -
Set the
--region
value to the AWS Region where you want to create the Amazon ECR repository. -
Replace
111122223333
with your AWS account ID.
aws ecr get-login-password --region
us-east-1
| docker login --username AWS --password-stdin111122223333
.dkr.ecr.us-east-1
.amazonaws.com -
-
Create a repository in Amazon ECR using the create-repository
command. aws ecr create-repository --repository-name
hello-world
--image-scanning-configuration scanOnPush=true --image-tag-mutability MUTABLEIf successful, you see a response like this:
{ "repository": { "repositoryArn": "arn:aws:ecr:us-east-1:111122223333:repository/hello-world", "registryId": "111122223333", "repositoryName": "hello-world", "repositoryUri": "111122223333.dkr.ecr.us-east-1.amazonaws.com/hello-world", "createdAt": "2023-03-09T10:39:01+00:00", "imageTagMutability": "MUTABLE", "imageScanningConfiguration": { "scanOnPush": true }, "encryptionConfiguration": { "encryptionType": "AES256" } } }
-
Copy the
repositoryUri
from the output in the previous step. -
Run the docker tag
command to tag your local image into your Amazon ECR repository as the latest version. In this command: -
Replace
docker-image:test
with the name and tagof your Docker image. -
Replace the Amazon ECR repository URI with the
repositoryUri
that you copied. Make sure to include:latest
at the end of the URI.
docker tag
docker-image
:test
111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest -
-
Run the docker push
command to deploy your local image to the Amazon ECR repository. Make sure to include :latest
at the end of the repository URI.docker push
111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest -
Create an execution role for the function, if you don't already have one. You need the Amazon Resource Name (ARN) of the role in the next step.
-
Create the Lambda function. For
ImageUri
, specify the repository URI from earlier. Make sure to include:latest
at the end of the URI.aws lambda create-function \ --function-name
hello-world
\ --package-type Image \ --code ImageUri=111122223333
.dkr.ecr.us-east-1
.amazonaws.com/hello-world
:latest \ --rolearn:aws:iam::111122223333:role/lambda-ex
-
Invoke the function.
aws lambda invoke --function-name
hello-world
response.jsonYou should see a response like this:
{ "ExecutedVersion": "$LATEST", "StatusCode": 200 }
-
To see the output of the function, check the
response.json
file.
To update the function code, you must create a new image version and store the image in the Amazon ECR repository. For more information, see Updating function code.
Java runtime interface clients
Install the runtime interface client for Java using the Apache Maven package manager. Add the following to
your pom.xml
file:
<dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-lambda-java-runtime-interface-client</artifactId> <version>2.3.1</version> </dependency>
For package details, see AWS Lambda Java Runtime Interface Client
You can also view the Java client source code in the AWS Lambda Java Support Libraries