This project creates AWS Lambda Layers with a Java 17 (or newer Version) based on Corretto to allow Lambda Functions to be written using Java 17 or newer.
- Java 17 (Corretto recommended)
- AWS CDK
- Node.js as required by the AWS CDK
- SDKMAN! is recommended to install and manage Java versions (optional)
The pipeline that builds and deploys the runtimes automatically has to be deployed once. This is done by manually deploy the included CDK stack that includes a pipeline. The following command assumes that you have already authenticated to the AWS account and assumed the correct role.
./gradlew clean
./gradlew -PjdkVersion=17 buildLayer # On non-x86_64 machines this may take very long
./gradlew -PjdkVersion=19 buildLayer # On non-x86_64 machines this may take very long
./gradlew :infrastructure:deploy # Deploy the pipeline After deploying the pipeline you have to once approve the Codestar connection to the GitHub repository.
To build the layer locally you need to have Docker installed. Then you can run the following command:
./gradlew -PjdkVersion=17 clean buildLayerThis will build the layer for Java 17. To build the layer for Java 19 replace 17 with 19.
Whenever a new version of the Java runtime is deployed, the ARN of the layer is stored in a SSM parameter named
Java<version>RuntimeLayerArn.
To use the custom Java runtime in your application add the following code to your CDK stack:
- Import the current layer version of the Java runtime layer for the account
private ILayerVersion importJavaRuntimeLayer(){
var javaRuntimeLayerArn=StringParameter.valueForStringParameter(this,"Java17RuntimeLayerArn");
return LayerVersion.fromLayerVersionArn(this,"import-JavaLayer",javaRuntimeLayerArn);
}To use the Java 19 runtime replace Java17RuntimeLayerArn with Java19RuntimeLayerArn
- Use this layer as runtime for you function:
var java17Layer=importJavaRuntimeLayer();
Function.Builder.create(this,"Function")
.runtime(Runtime.PROVIDED_AL2)
.architecture(Architecture.X86_64)
// ...
.layers(List.of(java17Layer))
.build();This project uses AWS Lambda Layers to provide a stripped down distribution of Java 17 and 19 Amazon Corretto JDK.
The layer can be shared between all Lambda functions in the account. One important restriction to notice is that the size of all layers and the lambda code itself must not exceed 250MB.
To respect these quotas it's not possible to use the full JDK distribution. Instead this project contains a build script
that will create a stripped down JRE of the JDK using jlink. If you are interested in how to do that take a closer
look at the Dockerfile.
In addition to the JRE the layer, that is distributed as a ZIP file, contains a bootstrap script that
starts the Java runtime and includes the AWS Lambda runtime implementation along with two other dependencies in the
classpath and sets the classpath to $LAMBDA_TASK_ROOT, $LAMBDA_TASK_ROOT/*, $LAMBDA_TASK_ROOT/lib/* and
/opt/java/lib/*. Please note that Layers are extracted to /opt/ by default. Therefore any other layer that contains
dependencies in the java/lib folder of the ZIP archive will become available in the /opt/java/lib folder which is
picked-up bey the Lambda runtime.
The infrastructure to build the custom Java runtimes consist of
- a CodePipeline that will build and deploy both runtime distributions in the current AWS account.
- scripts that will build the distribution archives:
prepare-layer- download all dependencies of the AWS Lambda runtime and copy the bootstrap file tobuild/layer/<version>build-jre.sh- a script that uses Docker and the providedDockerfileto build the stripped down distribution of the JDK