Home > Blockchain >  Using Gradle's shadowJar in AWS Lambda Container Image
Using Gradle's shadowJar in AWS Lambda Container Image

Time:10-16

A year ago AWS announced support for Cotainer Images. The idea is that you could run your Docker image in Lambda. The image could be created based on one of the provided base images, or completely from scratch. With the first approach one just have to implement a handler, just like with regular Lambda function, while image build with the second approach need to implement Lambda Runtime API.

Building images based on the provided base images is simpler, and the documentation gives an example for Gradle:

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" ]
task copyRuntimeDependencies(type: Copy) {
    from configurations.runtimeClasspath
    into 'build/dependency'
}

build.dependsOn copyRuntimeDependencies

But I'm already using Gradle Shadow Plugin and I don't like the idea of introducing a task just to copy dependencies. It looks like a hack.

Can I use that shadow JAR to build Java AWS Lambda Container image?

CodePudding user response:

It turns out you can! Skip copying the classes to $LAMBDA_TASK_ROOT completely, and copy only a single fat shadow jar to the $LAMBDA_TASK_ROOT/lib:

FROM amazon/aws-lambda-java:latest

COPY build/libs/build-all.jar ${LAMBDA_TASK_ROOT}/lib/

CMD [ "com.example.LambdaHandler::handleRequest" ]
  • Related