As a preparation to the investigation of how the usage of the Lambda layers with Lambda function and Java 21 runtime affect cold (with and without enabling SnapStart) and warm start times I'd like to give an introduction about how to create, publish and use layers for Java (21) Lambda function in the SAM template.
A Lambda layer is a .zip file archive that contains supplementary code or data. Layers usually contain library dependencies, a custom runtime, or configuration files.
There are multiple reasons why you might consider using layers:
For the sake of exploration we'll use the sample application for creating the Lambda layer with Java 21 runtime packaging the following dependencies into the layer :
To create the Lambda layer we need:
Lambda layer requires dependencies to be built into a single uber-jar. For this purpose, we use two plugins in the pom.xml. The maven-compiler-plugin compiles the source code. The maven-shade-plugin packages our artifacts into a single uber-jar. Then we need to execute
mvn clean package
to build our application.
When we add a layer to a Lambda function with Java runtime, Lambda loads the layer content into the /opt directory of that execution environment. For each Lambda runtime, the PATH variable already includes specific folder paths within the /opt directory. To ensure that the PATH variable picks up our layer content, our layer .zip file should have its dependencies in the following folder paths: java/lib
For example, the resulting layer .zip file that we create with our sample application has the following directory structure:
aws-pure-java-21-common-lambda-layer-content.zip └ java └ lib └ aws-pure-java-21-common-lambda-layer-1.0.0-SNAPSHOT.jar
which can be achieved by executing following commands on Linux:
To publish this Lambda layer with Java 21 runtime we need to execute the following command with AWS CLI v2:
aws lambda publish-layer-version --layer-name aws-pure-java-21-common-lambda-layer --zip-file fileb://aws-pure-java-21-common-lambda-layer-content.zip --compatible-runtimes java21
With additional parameter --compatible-architectures "x86" we can define the compatible hardware architectures like x86 (default) or arm64.
As the response AWS will deliver Lambda layer ARN that we'll need to reference later, which looks similar to this one:
arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:aws-pure-java-21-common-lambda-layer:1
Please note that the last parameter is the Lambda layer version which is always 1 when we first publish the layer and will increment by one with subsequent updates of the existing Lambda layer.
In order to attach the layer to your function we can do the following:
Type: AWS::Serverless::Function Properties: FunctionName: GetProductByIdWithPureJava21LambdaWithCommonLayer AutoPublishAlias: liveVersion Layers: - !Sub arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:aws-pure-java-21-common-lambda-layer:1 Handler: software.amazonaws.example.product.handler.GetProductByIdHandler::handleRequest
In this article I gave an introduction about how to create, publish and use layers for Java 21 Lambda functions AWS CLI v2 and the SAM template. In the next article published under the AWS Lambda SnapStart series I'll explore how the usage of the (different) Lambda layers with function having Java 21 runtime affects the cold (with and without enabling SnapStart) and warm start times.
The above is the detailed content of How to create, publish and use layers for Java Lambda functions. For more information, please follow other related articles on the PHP Chinese website!