Packaging your layer content - Amazon Lambda
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Packaging your layer content

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.

This section explains how to properly package your layer content. For more conceptual information about layers and why you might consider using them, see Managing Lambda dependencies with layers.

The first step to creating a layer is to bundle all of your layer content into a .zip file archive. Because Lambda functions run on Amazon Linux, your layer content must be able to compile and build in a Linux environment.

To ensure that your layer content works properly in a Linux environment, we recommend creating your layer content using a tool like Docker or Amazon Cloud9. Amazon Cloud9 is a cloud-based integrated development environment (IDE) that provides built-in access to a Linux server for running and testing code. For more information, see Using Lambda layers to simplify your development process on the Amazon Compute Blog.

Layer paths for each Lambda runtime

When you add a layer to a function, 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 your layer content, your layer .zip file should have its dependencies in the following folder paths:

Runtime Path

Node.js

nodejs/node_modules

nodejs/node16/node_modules (NODE_PATH)

nodejs/node18/node_modules (NODE_PATH)

nodejs/node20/node_modules (NODE_PATH)

Python

python

python/lib/python3.x/site-packages (site directories)

Java

java/lib (CLASSPATH)

Ruby

ruby/gems/3.2.0 (GEM_PATH)

ruby/lib (RUBYLIB)

All runtimes

bin (PATH)

lib (LD_LIBRARY_PATH)

The following examples show how you can structure the folders in your layer .zip archive.

Node.js
Example file structure for the Amazon X-Ray SDK for Node.js
xray-sdk.zip └ nodejs/node_modules/aws-xray-sdk
Python
Example file structure for the Requests library
layer_content.zip └ python └ lib └ python3.11 └ site-packages └ requests └ <other_dependencies> (i.e. dependencies of the requests package) └ ...
Ruby
Example file structure for the JSON gem
json.zip └ ruby/gems/2.7.0/ | build_info | cache | doc | extensions | gems | └ json-2.1.0 └ specifications └ json-2.1.0.gemspec
Java
Example file structure for the Jackson JAR file
layer_content.zip └ java └ lib └ jackson-core-2.17.0.jar └ <other potential dependencies> └ ...
All
Example file structure for the jq library
jq.zip └ bin/jq

For language-specific instructions on packaging, creating, and adding a layer, refer to the following pages:

We recommend against using layers for the following languages. The linked pages contain more information.