Introducing our new CEO Don Johnson - Read More

amazon/aws-lambda-java

Verified Publisher

By Amazon Web Services

Updated 20 days ago

AWS Lambda base images for Java

Image
API Management
Languages & Frameworks
Integration & Delivery
14

1M+

About

AWS provided base images for Lambda contain all the required components to run your functions packaged as container images on AWS Lambda. These base images contain the Amazon Linux Base operating system, the runtime for a given language, dependencies and the Lambda Runtime Interface Client (RIC), which implements the Lambda Runtime API. The Lambda Runtime Interface Client allows your runtime to receive requests from and send requests to the Lambda service.

To learn more about the composition of the base images you can visit https://github.com/aws/aws-lambda-base-images.

Maintenance policy

AWS will regularly provide security patches and other updates for these base images. These images are similar to the AWS Lambda execution environment on the cloud to allow customers to easily packaging functions to the container image. However, we may choose to optimize the container images by changing the components or dependencies included. When deployed to AWS Lambda these images will be run as-is.

Usage

You can get started by using these images in your Dockerfile and coping your class files into the /var/task folder in your image. The runtime jar dependencies should be copied into /var/task/lib directory.

Example Dockerfile below:

FROM public.ecr.aws/lambda/java:11

# Copy function code and runtime dependencies from Gradle layout
COPY build/classes/java/main ${LAMBDA_TASK_ROOT}
COPY build/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.example.LambdaHandler::handleRequest" ]

Example Gradle task definition to prepare the runtime dependencies:

task copyRuntimeDependencies(type: Copy) {
    from configurations.runtimeClasspath
    into 'build/dependency'
}

build.dependsOn copyRuntimeDependencies

If you are using Maven, adjust COPY command in the Dockerfile accordingly:

FROM public.ecr.aws/lambda/java:11

# Copy function code and runtime dependencies from Maven layout
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.example.LambdaHandler::handleRequest" ]

You can use maven-dependency-plugin to collect runtime dependencies:

<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>
             <!-- configure the plugin here -->
           </configuration>
         </execution>
       </executions>
     </plugin>
     <!-- other plugins defined here -->
   </plugins>
</build>

Use mvn compile dependency:copy-dependencies -DincludeScope=runtime to compile the project and collect the runtime dependencies.

The resulting layout should look like:

/var/task/
├── com
│   └── example
│       ├── LambdaHandler.class
│       └── <your other class files here>
└── lib
    ├── aws-lambda-java-core-1.2.1.jar
    ├── aws-lambda-java-events-3.7.0.jar
    └── <your other JAR dependencies here>

You can then locally test your function using the docker build and docker run commands.

To build your image:

docker build -t <image name> .

To run your image locally:

docker run -p 9000:8080 <image name>

In a separate terminal, you can then locally invoke the function using cURL:

curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{"payload":"hello world!"}'

To learn more on deploying the function to ECR, check out the AWS documentation on Creating a repository and Pushing an image

Docker Pull Command

docker pull amazon/aws-lambda-java