Lambda layer

On this page 7

A Lambda layer is a versioned ZIP archive containing libraries, custom runtimes, configuration files or other dependencies that AWS Lambda extracts into the execution environment of any function that references it. In serverless computing on AWS, layers let teams share common code across many functions without copying it into each deployment package.

Each function can attach up to five layers, and every layer version is immutable once published. Layers reduce duplication and keep function packages small, but they also introduce shared dependencies that must be versioned and tested with the same discipline as application code.

How a Lambda layer reaches the function

When AWS Lambda prepares an execution environment, it extracts each layer into the /opt directory before the function code starts. Layers are applied in the order listed in the function configuration, and a file from a later layer overwrites a file with the same path from an earlier one.

The runtime finds layer content only if it sits in the expected folder structure. Python looks in python or python/lib/python3.x/site-packages, Node.js looks in nodejs/node_modules, and executables placed in bin are added to the PATH. A layer with the wrong folder layout uploads without error but fails at import time.

RuntimeFolder inside the ZIPExtracted location
Pythonpython or python/lib/python3.x/site-packages/opt/python
Node.jsnodejs/node_modules/opt/nodejs/node_modules
Javajava/lib/opt/java/lib
Rubyruby/gems/3.x/opt/ruby/gems
Binariesbin/opt/bin, added to PATH
Shared librarieslib/opt/lib, added to LD_LIBRARY_PATH
Expected layer paths by runtime

Versions, limits and permissions

Every publish creates a new numbered layer version with its own Amazon Resource Name (ARN), such as arn:aws:lambda:eu-west-1:123456789012:layer:shared-utils:7. A function references one exact version, so updating a layer never changes running functions until their configuration is explicitly updated to the new ARN.

Layers are subject to the same packaging quotas as the function itself. The most important constraint is the 250 MB unzipped limit, which counts the function code and all attached layers together.

ParameterValueWhat it affects
Layers per function5How finely dependencies can be split
Unzipped size250 MB including function codeLarge frameworks and machine learning libraries
Direct upload size50 MB zippedLarger archives must be uploaded via Amazon S3
ScopeOne AWS RegionMulti-region systems must publish per region
Compatibility metadataRuntimes and architecturesx86_64 versus arm64 builds of native code
Key Lambda layer limits

Access is controlled by a layer version permission policy. A layer is private to its account by default but can be shared with specific accounts, an AWS Organization or all accounts. Public layers from vendors are common for monitoring agents, yet each one is third-party code running inside the function and deserves the same review as any other dependency.

Lambda layers compared with alternative packaging

A Lambda layer is one of several ways to deliver code to a function, and it is not always the best one. The main alternatives are bundling dependencies into the deployment package, using a container image, or using a Lambda extension, which is itself delivered as a layer but runs as a separate process.

OptionSize limitBest suited forMain drawback
Bundled ZIP package250 MB unzippedFunction-specific dependenciesDuplication across functions
Lambda layer250 MB with functionShared libraries and runtimesHidden coupling between functions
Container image10 GBLarge libraries and existing build toolingLayers cannot be attached
Lambda extensionCounts toward layer limitsMonitoring, secrets caching, telemetryAdds start-up and memory overhead
Packaging options for AWS Lambda

Layers do not reduce cold start time by themselves. The execution environment still downloads and extracts the same total amount of code, so a large layer slows initialization exactly as a large deployment package would. Their real benefits are smaller deployment artifacts, faster uploads and centralized dependency management.

When to use a Lambda layer

Layers pay off when many functions need the same stable content and that content changes less often than the function code. They are a weak fit for dependencies that differ between functions or change with every release.

  • Shared internal libraries, such as logging, authentication helpers or data models used by dozens of functions.
  • Custom runtimes delivered through the provided.al2023 runtime with a bootstrap file in the layer.
  • Native binaries such as FFmpeg, ImageMagick or database drivers compiled for Amazon Linux.
  • Vendor agents and extensions for observability or secrets retrieval.
  • Large static data, such as reference tables or certificates, that rarely changes.

Infrastructure tools treat layers as first-class resources. AWS SAM, the Serverless Framework, the AWS CDK and Terraform can all build, publish and attach layer versions, which keeps the layer ARN under version control instead of being pasted manually into function settings.

Risks and common mistakes with layers

Most layer problems come from the indirection layers introduce: the code a function runs is no longer fully visible in its own repository or package. The following table lists frequent failures and their remedies.

SymptomCauseFix
Module not found at runtimeWrong folder structure in the ZIPFollow the runtime path convention
Invalid ELF header errorNative code built on macOS or WindowsBuild inside an Amazon Linux container
Function works locally, fails in LambdaLocal tests ignore layer contentsTest with AWS SAM or the real layer
Unexpected behavior after an updateFile overwritten by a later layerAvoid overlapping paths between layers
Deployment fails on sizeCombined unzipped size above 250 MBTrim dependencies or use a container image
Common Lambda layer mistakes

Version sprawl is a quieter risk. When functions pin different versions of the same layer, security fixes do not reach all of them, and deleting an old version does not remove it from functions that already use it. A regular audit of layer ARNs across the account keeps the dependency landscape visible.

In plain words

A Lambda layer is like a shared toolbox for small cloud programs. Instead of every program carrying its own copy of the same tools, the tools are placed in one box, and each program simply says which box it wants to use.

Each version of the box is sealed, so a program keeps working with the exact tools it was tested with. When the tools improve, a new box is made and programs switch to it one by one. The main point is less copying and easier updates, with the duty to keep track of which program uses which box.

A real-world example

A logistics company runs about 60 Python functions on AWS Lambda that process shipment events, generate labels and send notifications. Every function used the same in-house validation library and a PDF rendering toolkit, so each deployment package was about 45 MB and the library existed in 60 slightly different copies.

The platform team moves the validation library into one layer and the PDF toolkit, compiled inside an Amazon Linux container for arm64, into a second layer. AWS SAM publishes both and passes the layer version ARNs to each function, and a pipeline test confirms that all functions import the new version correctly.

Function packages shrink to under 2 MB, deployments finish in seconds rather than minutes, and a security fix in the validation library now reaches every function through a single layer update and a coordinated rollout. Cold start times stay roughly the same, which matches expectations, because the total code loaded is unchanged.