Sharing Code and Dependencies with Layers
This lesson explains how to share libraries and other dependencies between functions. A layer is a .zip archive of libraries, a custom runtime, configuration or an extension that is published once and used by many functions, keeping each function's own package small.
Learning outcomes
After completing this lesson, you will be able to:
- Explain how a layer is made available to a function and the folder structure each runtime expects.
- Describe how layers are versioned and shared across functions and accounts.
- Decide when a layer helps and when a container image is the better way to package dependencies.
How layers work
A layer is published as a .zip archive and then attached to any number of functions. When a function starts, Lambda extracts each of its layers into /opt in the execution environment, in the order they are listed, so a later layer can overwrite files from an earlier one. The function's code can then load libraries from /opt as if they were part of its own package.
Layers come with limits that shape how they are used. A function can use up to five layers, and the function package and all its layers together must stay within the 250 MB unzipped limit, so layers share code but do not add space. Each layer is referenced by the ARN of a specific version, which keeps a function's dependencies fixed until it is updated. Container image functions cannot use layers at all. The following list summarises these rules.
- A function can use up to 5 layers.
- The function package and all its layers together must stay within the 250 MB unzipped limit.
- Each layer is referenced by a version ARN, such as
arn:aws:lambda:eu-west-1:111122223333:layer:requests:3. - Container image functions cannot use layers; dependencies are built into the image instead.
How layers behave
Folder structure
Libraries are found only if the layer places them where the runtime looks. The paths below are relative to the root of the layer's .zip file.
| Runtime | Path inside the .zip | Available in the function at |
|---|---|---|
| Python | python/ | /opt/python |
| Node.js | nodejs/node_modules/ | /opt/nodejs/node_modules |
| Java | java/lib/ | /opt/java/lib |
| All runtimes (binaries) | bin/ | /opt/bin (on the PATH) |
A layer with the wrong structure deploys without error, but the function then fails with Runtime.ImportModuleError or a missing-module error.
Where layer files must go
Publishing, versioning and sharing
Lambda layers are versioned resources that can be shared between functions and accounts. The following list describes how layers are published, versioned and shared.
- Layer versions are immutable. To change a layer, publish a new version and update each function to reference it; existing functions keep using the version they name.
- Native dependencies must be built for Amazon Linux and the function's architecture (x86_64 or arm64), and
--compatible-runtimesand--compatible-architecturesrecord what the layer supports. add-layer-version-permissionshares a layer version with another account, an organization or every account.- In AWS SAM,
AWS::Serverless::LayerVersionbuilds and publishes a layer, and functions reference it with!Ref.
Example. A Python layer must put packages under python/ in the .zip so they appear on the import path at /opt/python. The layer is then attached to a function and shared with a second account.
mkdir -p layer/python && pip install requests -t layer/python
(cd layer && zip -r ../requests-layer.zip python)
aws lambda publish-layer-version --layer-name requests \
--zip-file fileb://requests-layer.zip --compatible-runtimes python3.13 --compatible-architectures arm64
aws lambda update-function-configuration --function-name orders-api \
--layers arn:aws:lambda:eu-west-1:111122223333:layer:requests:1
aws lambda add-layer-version-permission --layer-name requests --version-number 1 \
--statement-id share-with-444455556666 --action lambda:GetLayerVersion --principal 444455556666When layers help, and when they do not
A layer is most useful when the same content is needed by many functions. A shared library, such as an internal data-access package, can be published once as a layer and referenced by every function that needs it, which keeps each deployment package small and makes the library's version visible in each function's configuration. Layers are also the delivery mechanism for extensions, such as monitoring agents and the Parameters and Secrets Lambda extension.
Layers do not increase the size a function can reach. A function and all its layers together must stay within the 250 MB unzipped limit, so a package that is too large needs a container image instead. Layers also add a dependency that is managed separately from the function: each layer version is immutable, but if functions reference different versions, or a team updates a layer without retesting the functions that use it, behaviour can differ between environments. When dependencies must be tested together with every release, bundling them in the package, or pinning a specific layer version in infrastructure as code, keeps releases reproducible.
The following table summarises whether layers are appropriate in several common situations.
| Situation | Layers? |
|---|---|
| Several functions share the same library or internal code | Yes: publish it once |
| A monitoring agent or secrets cache must run beside the function | Yes: extensions are delivered as layers |
| The package exceeds 250 MB unzipped | No: layers count towards the limit; use a container image |
| Dependencies must be pinned and tested with each release | Bundle them in the package, or pin a specific layer version |
Is a layer the right tool?
A layer the function cannot find
A team moves the requests library into a layer shared by six functions. After the change, every function fails with Runtime.ImportModuleError: No module named 'requests'.
| Check | Finding |
|---|---|
| Is the layer attached? | Yes: get-function-configuration lists the layer version ARN |
| What is inside the .zip? | requests/__init__.py at the root, not under python/ |
| Where does Python look? | /opt/python, so files extracted to /opt/requests are invisible |
The developer rebuilds the archive with the packages under python/, publishes version 2 of the layer and updates the six functions to reference it. Because layer versions are immutable, functions still pinned to version 1 keep failing until they are updated, which is why the update is made in the SAM template for all functions at once.
A function's unzipped deployment package is over 250 MB. Why does moving some libraries into layers not solve the problem?
Show answer
Layers count towards the same 250 MB unzipped limit as the function code. Large dependencies need a container image instead, which can be up to 10 GB.
Exam Tips
Remember for the exam
- To share a library across functions or accounts, publish it as a layer and grant layer permissions.
- A function can use up to five layers, and their unzipped size counts towards the 250 MB limit.
- Python layers place packages under
python/; Node.js layers undernodejs/node_modules/. - Layers do not solve an oversized package; a container image of up to 10 GB does.
- Container image functions cannot use layers.