Home > Java > javaTutorial > How to create, publish and use layers for Java Lambda functions

How to create, publish and use layers for Java Lambda functions

WBOY
Release: 2024-07-18 13:59:57
Original
712 people have browsed it

How to create, publish and use layers for Java Lambda functions

Introduction

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.

What is Lambda layer

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.

Benefits of using Lambda layer

There are multiple reasons why you might consider using layers:

  • To reduce the size of your deployment packages. Instead of including all of your function dependencies along with your function code in your deployment package, put them in a layer. This keeps deployment packages small and organized.
  • To separate core function logic from dependencies. With layers, you can update your function dependencies independent of your function code, and vice versa. This promotes separation of concerns and helps you focus on your function logic.
  • To share dependencies across multiple functions. After you create a layer, you can apply it to any number of functions in your account. Without layers, you need to include the same dependencies in each individual deployment package.
  • To use the Lambda console code editor. The code editor is a useful tool for testing minor function code updates quickly. However, you can’t use the editor if your deployment package size is too large. Using layers reduces your package size and can unlock usage of the code editor.

How to create, publish and use layers for Java 21 Lambda functions with AWS CLI v2 and the SAM template

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 :

  • aws-lambda-java-core
  • aws-lambda-java-events
  • org-crac
  • slf4j-simple
  • jackson-dataformat-xml

To create the Lambda layer we need:

  • Java 21 runtime
  • Maven version (3.8.6 or later) capable of building and packaging Java 21 applications
  • AWS CLI v2

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
Copy after login

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
Copy after login

which can be achieved by executing following commands on Linux:

  • mkdir java
  • mkdir java/lib
  • cp -r target/aws-pure-java-21-common-lambda-layer-1.0.0-SNAPSHOT.jar java/lib/
  • zip -r aws-pure-java-21-common-lambda-layer-content.zip java

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
Copy after login

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:

  • when using AWS CLI v2 and invoking aws lambda create-function command add --layers paramater with layer ARN like arn:aws:lambda:${AWS::Region}:${AWS::AccountId}:layer:aws-pure-java-21-common-lambda-layer:1
  • when using AWS SAM template like we'll explore in our next article (here is the concrete example) add Layers parameter to the Lambda function like this:
    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
Copy after login

Conclusion

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!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template