Working with layers for Java Lambda functions
Use Lambda layers to package code and dependencies that you want to reuse across multiple functions. Layers usually contain library dependencies, a custom runtime, or configuration files. Creating a layer involves three general steps:
-
Package your layer content. This means creating a .zip file archive that contains the dependencies you want to use in your functions.
-
Create the layer in Lambda.
-
Add the layer to your functions.
Package your layer content
To create a layer, bundle your packages into a .zip file archive that meets the following requirements:
-
Ensure that the Java version that Maven or Gradle refers to is the same as the Java version of the function that you intend to deploy. For example, for a Java 21 function, the
mvn -v
command should list Java 21 in the output. -
Your dependencies must be stored in the
java/lib
directory, at the root of the .zip file. For more information, see Layer paths for each Lambda runtime. -
The packages in your layer must be compatible with Linux. Lambda functions run on Amazon Linux.
-
If your layer includes native binaries or executable files, they must target the same architecture (x86_64 or arm64) as your function.
You can create layers that contain either third-party Java libraries or your own Java modules and packages. The following procedure uses Maven. You can also use Gradle to package your layer content.
To create a layer using Maven dependencies
-
Create an Apache Maven project with a
pom.xml
file that defines your dependencies.The following example includes Jackson Databind
for JSON processing. The <build>
section uses the maven-dependency-pluginto create separate JAR files for each dependency instead of bundling them into a single uber-jar. If you want to create an uber-jar, use the maven-shade-plugin . Example pom.xml
<dependencies> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.17.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.13.0</version> <configuration> <source>21</source> <target>21</target> <release>21</release> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.6.1</version> <executions> <execution> <id>copy-dependencies</id> <phase>package</phase> <goals> <goal>copy-dependencies</goal> </goals> <configuration> <outputDirectory>${project.build.directory}/lib</outputDirectory> </configuration> </execution> </executions> </plugin> </plugins> </build>
-
Build the project. This command creates all dependency JAR files in the
target/lib/
directory.mvn clean package
-
Create the required directory structure for your layer:
mkdir -p java/lib
-
Copy the dependency JAR files to the
java/lib
directory:cp target/lib/*.jar java/lib/
-
Zip the layer content:
The directory structure of your .zip file should look like this:
java/ └── lib/ ├── jackson-databind-2.17.0.jar ├── jackson-core-2.17.0.jar └── jackson-annotations-2.17.0.jar
Note
Make sure your .zip file includes the
java
directory at the root level withlib
inside it. This structure ensures that Lambda can locate and import your libraries. Each dependency is kept as a separate JAR file rather than bundled into an uber-jar.
Create the layer in Lambda
You can publish your layer using either the Amazon CLI or the Lambda console.