skip to Main Content

I’m working on upgrading a Spring Cloud Functions lambda to run on JDK 17. Amazon does not provide base images for JDK 17, so instead of deploying a ZIP file I created a lambda that runs a Docker image. For running Java images my Dockerfile usually looks like this:

FROM amazoncorretto:17
VOLUME /tmp
COPY ./my-lambda-project/build/libs/my-lambda-project-1.0.jar app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

which runs the jar created first using bootRun Gradle task. My Application.java class has the main method which looks like this:

public static void main(String[] args) {
    FunctionalSpringApplication.run(Application.class, args);
}

The main method uses FunctionalSpringApplication instead of SpringApplication.run (for faster start) and in the lambda config I specify the function handler to be org.springframework.cloud.function.adapter.aws.FunctionInvoker::handleRequest.

With the Dockerfile approach, I can use some combination of ENTRYPOINT or CMD. Is there a way to make this docker image when pushed to ECR run the lambda using JDK 17?

2

Answers


  1. You can run alternative versions of Java on AWS Lambda by creating your own custom runtime. My lambda-java17-layer Github repo shows you how to do it.

    If you don’t like this approach and you want to stick with the container image then there are a couple of extra steps you need to take.

    You picked the standard amazoncorretto base image to work from, you need to use one of the managed Lambda base images (unless you want to do more work yourself)

    Login or Signup to reply.
  2. If you want to package your Java 17 lambda function as a docker container, then you’ll need to use a Java 17 base image, as @Mark Sailes said above. As of the time of this writing, there are officially supported AWS base images for Java 8 and Java 11, but none for Java 17. However, there is a community base image for Java 17, 18, and 19 available on ECR Public Gallery and DockerHub that you can use.

    Using the community base image is exactly like using the officially-supported base image. You should be able to adapt your above Dockerfile to use the community Java 17 base image without too much trouble. For example, the following is the Dockerfile from an example Java 17 lambda function:

    FROM public.ecr.aws/aleph0io/lambda/java:17.0.4-al2
    
    COPY target/hello-lambda.jar "${LAMBDA_TASK_ROOT}/lib/"
    
    CMD [ "com.sigpwned.lambda.hello.HelloLambda::handleRequest" ]
    

    Disclaimer: I created the community Java 17 base image and the above example lambda function. If you’re curious how the community base images are made, they are fully open source and available on GitHub.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search