What is Lambda Layers and how to use it with Java runtime?

Contents

When developing serverless functions, you often need additional dependencies or libraries for the function to work. For Lambda functions in java, you need amazon dependencies of the services used in the function. Then you zip the function and its dependencies into a fat jar and upload it to the Lambda function. If you are not familiar with creating lambda function in java then refer to this post for a quick start.

The fat jar approach works fine but if you have multiple functions that use the same dependencies then creating a fat jar of each function is not a good approach and maintenance of such code is also difficult. A change in the version of a jar requires a change in each of the individual lambda functions.

A more elegant solution is to use Lambda layers.

What is Lambda Layers?

A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package. Layers let you keep your deployment package small, which makes development easier.

Layers are extracted to the /opt directory in the function execution environment. Each runtime looks for libraries in a different location under /opt, depending on the language.

You can specify up to 5 layers in your function’s configuration, during or after function creation. You choose a specific version of a layer to use. If you want to use a different version later, update your function’s configuration.

Let’s see it in action

We will write a basic Lambda function that would just print lambda environment variables and event details. We will create 2 maven projects. In the first project, we include amazon and other essential dependencies. This project will serve as our Lambda Layer. In the second project, we include our first project as a dependency and will write the Lambda handler.

Create the first maven project called Lambda-Layer-Base and update pom as below: This project contains all common dependencies required by the functions. In our case, we added lamdba-core and gson dependency. We also added maven-shade-plugin to create fat jar.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65   4.0.0 net.rajanpanchal lambda-layer-base 1 Lambda-Layer-Base Lambda base 1.8 1.8 2.14.11 software.amazon.awssdk bom ${aws.java.sdk.version} pom import com.amazonaws aws-lambda-java-core 1.2.0 com.google.code.gson gson 2.8.6 org.apache.maven.plugins maven-compiler-plugin${maven.compiler.source} ${maven.compiler.target} org.apache.maven.plugins maven-shade-plugin 3.2.4 package shade  This project will only contain dependencies. Now execute maven clean install and it should generate a jar (lambda-layer-base-1.jar) in the target folder. Create another maven project and update pom with the below contents. We included our first project as a dependency (lambda-layer-base). Also note, we no longer use maven shade plugin since we don’t want dependencies to be included in the jar.   1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36   4.0.0 net.rajanpanchal lambda-java-demo 1 jar Lambda-Layers-Demo HelloWorld Lambda Layers Demo 1.8 1.8 2.14.11 net.rajanpanchal lambda-layer-base 1 org.apache.maven.plugins maven-compiler-plugin${maven.compiler.source} \${maven.compiler.target} 

Add a class file in src/main/java that will contain Lambda logic. Here we are just logging some stuff.

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28  package net.rajanpanchal.handlers; import java.util.Map; import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.LambdaLogger; import com.amazonaws.services.lambda.runtime.RequestHandler; import com.google.gson.Gson; import com.google.gson.GsonBuilder; public class HelloWorldHandler implements RequestHandler, String>{ public String handleRequest(Map event, Context context) { LambdaLogger logger = context.getLogger(); String response = new String("200 OK"); logger.log("ENVIRONMENT:"+System.getenv()); // log execution details Gson gson = new GsonBuilder().setPrettyPrinting().create(); logger.log("ENVIRONMENT VARIABLES: " + gson.toJson(System.getenv())); logger.log("CONTEXT: " + gson.toJson(context)); // process event logger.log("EVENT: " + gson.toJson(event)); logger.log("EVENT TYPE: " + event.getClass().toString()); return response; } } 

Execute maven package command to create the jar for the project. A jar (lambda-java-demo-1.jar) should get created in target folder.

Creating Lamdba Layer

Before we head to the console to create the layer, let’s complete one important step. Remember that layers gets extracted to a folder in /opt directory. You need to package the layer in folder structure as per your runtime. For java, you need to package jar in java/lib folder. The final folder structure, in the lambda runtime, would be /opt/java/lib/<your jar>. So now create folders java/lib and copy jar from the first project and zip the folder. Now we are ready to upload the jar as lambda layer.

Creating Lambda function

Now create the lambda function and specify the layer that we just created. Also, upload the jar from the second project as our function code.

Upload function code jar and update runtime settings

Create a test event and execute the test.

Conclusion

Lambda layer makes your development easy and error-free. It reduces the size of the function deployment package. You can use the same layer in multiple functions.