Sign inSign up

aleph0io/aws-lambda-java

By aleph0io

•Updated about 3 years ago

Unofficial community Java 17+ base images for AWS Lambda functions packaged as container images.

Image
0

956

aleph0io/aws-lambda-java repository overview

Unofficial community base image for Lambda that contains all the required components to run your functions packaged as container images on AWS Lambda. This base image contains the Amazon Linux Base operating system, the runtime for Java, 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 this base image you can visit aleph0io/aws-lambda-java-base-images⁠ on GitHub.

These images are also available on the ECR Public Gallery⁠.

Maintained by @sigpwned⁠. Drop me a line!

⁠Maintenance policy

These images will be updated periodically to pick up new security patches. The components and dependencies included in the image may change from time to time, but they will always work for deploying images to AWS Lambda.

⁠Usage

You can find an entire example AWS Lambda function implementation on GitHub at aleph0io/example-java-lambda-function⁠.

Otherwise, 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.

FROM public.ecr.aws/aleph0io/lambda/java:17

# 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/aleph0io/lambda/java:17

# 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>
             
           </configuration>
         </execution>
       </executions>
     </plugin>
   </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!"}'

Tag summary

Content type

Image

Digest

sha256:67abc7f58…

Size

225.2 MB

Last updated

about 3 years ago

docker pull aleph0io/aws-lambda-java:21.0.0