• Documentation
  • Pricing
© 2026 Serverless, Inc. All rights reserved.

Framework

  • Overview
  • Documentation
  • Plugins360
  • Pricing

Learn

  • Blog
  • GuidesUpdated
  • Examples240
  • Courses

Resources

  • Support
  • Security
  • Trust Center
  • Status

Community

  • Slack
  • GitHub47k
  • Forum
  • Meetups

Company

  • About
  • Careers
  • Contact
  • Partners

Legal

  • Terms of Service
  • Privacy Policy
  • Trademark
  • DMCA
Serverless Framework Logo

Serverless Framework

Intro
SetupUpgrading To V4ConceptsTutorialAWS CredentialsLicense KeysVerifying Downloads
DeployingPackagingBuildingTestingDev ModeServicesFunctions
OverviewHTTP (API Gateway v2)REST (API Gateway v1)ActiveMQApplication Load BalancerAlexa SkillAlexa Smart HomeCloudWatch EventCloudWatch LogCloudFrontCognito User PoolEventBridge EventIoTIoT Fleet ProvisioningKafkaKinesis & DynamoDBMSKRabbitMQS3ScheduleSNSSQSWebsocket
Layers
OverviewTroubleshooting
Managed InstancesWeb Apps (Lambda Web Functions)Sandboxes (Lambda MicroVMs)MCP ServersAlertsVersion PruningDomainsIAM Function PermissionsParameters
OverviewSelf-reference serverless.ymlServerless CoreEnvironment VariablesCLI OptionsExternal YAML/JSON FilesJavascript propertiesGitDoppler
OverviewS3 ObjectsSSM Parameter Store & Secrets ManagerCloudFormation Stack Outputs
OverviewVaultTerraform State Output
ResourcesComposing ServicesDeployment BucketStateAgent SkillsPython support
OverviewRuntimeGatewayMemoryBrowserCode InterpreterDev Mode
API Gateway Proxy
OverviewGeneral ConfigurationAuthenticationAPI KeysData SourcesResolversPipeline FunctionsSubstitutionsCachingDelta SyncCustom DomainWAFCLI Commands
Deploying SAM/CFN TemplatesWorkflow Tips
OverviewCreating PluginsCLI OutputCustom CommandsCustom VariablesExtending the Configuration schemaExtending and overriding configuration
OverviewDashboardAxiom
Overviewagent docsagent inspectagent setupagent skills installagent skills list / readdeploydeploy functiondeploy listdevdiffinfoinvokeinvoke localloginlogin awslogin aws ssologsmetricspackageplugin installplugin uninstallprintprunereconcileremoverollbackrollback functionusage
OverviewConcepts
OverviewMetricsTracesTroubleshoot
OverviewNode.jsPython
OutputsProviders
OverviewBranch DeploymentsPreview DeploymentsCustom ScriptsTestingPrivate PackagesNotificationsMono ReposDeploy in your own CI/CDBest PracticesTroubleshootingFAQ
OverviewSetupToolsAWS Integration
Serverless.yml Reference
Examples and TutorialsConfiguration Validation
  1. Usage
  2. Layers

AWS Lambda Layers

An AWS Lambda layer is a zip of code or dependencies that functions load alongside their own package. Each entry under the top-level layers key in serverless.yml publishes a layer version on deploy; functions in the service, or in other services and accounts, can then use it. To attach a layer that already exists, you only need the function's layers list (see Use layers in functions).

This service publishes the contents of layer-dir as a layer and attaches it to a function:

# serverless.yml
service: my-service

provider:
  name: aws
  runtime: nodejs24.x

layers:
  shared:
    path: layer-dir

functions:
  hello:
    handler: handler.hello
    layers:
      - !Ref SharedLambdaLayer

For a Node.js layer, put the modules under layer-dir/nodejs/node_modules; for Python, under layer-dir/python. Lambda extracts the layer to /opt, and each runtime looks in its own layer path.

Configuration

Each key under layers names a layer. Its settings:

layers:
  hello:
    path: layer-dir # required unless package.artifact is set; relative to the service directory
    name: ${sls:stage}-hello # default: the layer key
    description: Shared helpers # up to 256 characters
    compatibleRuntimes: # up to 15 runtimes
      - python3.14
    compatibleArchitectures: # up to 2
      - x86_64
      - arm64
    licenseInfo: MIT # up to 512 characters
    allowedAccounts: # accounts that may use the layer
      - '123456789012'
    retain: false # default false
KeyTypeDefaultAllowed values
pathstringRequired unless package.artifactA directory, relative to the service directory
namestringThe layer key1 to 140 letters, numbers, - or _, or a layer ARN prefix
descriptionstringNoneUp to 256 characters
compatibleRuntimeslistNoneUp to 15 Lambda runtime identifiers, or custom for customer-managed runtimes
compatibleArchitectureslistNonex86_64, arm64
licenseInfostringNoneUp to 512 characters
allowedAccountslistNone12-digit account IDs, arn:aws:iam::<account>:root, or '*'
retainbooleanfalseKeep earlier layer versions when a new one is published
packageobjectNonepatterns, artifact (and the deprecated include, exclude)

A service can define any number of layers. Lambda lets one function use at most five.

Layer versions and retain

Lambda layer versions can't be changed. When a deploy changes a layer's content or settings, CloudFormation publishes a new version and, by default, deletes the previous one. With retain: true, previous versions stay published, so functions in other services that still reference them keep working. When nothing about a layer changed since the last deploy, the deploy reuses the zip it uploaded before instead of uploading it again.

Package a layer

A layer is zipped from its path. The service's package.patterns apply to it, followed by the layer's own package.patterns; all of them match paths relative to the layer's path, not to the service directory:

package:
  patterns:
    - '!**/*.tar.gz' # applies to functions and layers

layers:
  shared:
    path: layer-dir
    package:
      patterns:
        - '!tests/**' # applies to this layer only

The service's development dependency exclusion doesn't apply to a layer, unless the layer's path is the service directory itself: the layer ships its path as it is on disk. Function and service zips leave out the layers' path directories.

To publish a zip you built yourself, set package.artifact instead of path:

layers:
  shared:
    package:
      artifact: build/shared-layer.zip

Share a layer with other accounts

allowedAccounts gives other AWS accounts permission to use the layer version (lambda:GetLayerVersion). It takes 12-digit account IDs, account root ARNs, or '*':

layers:
  shared:
    path: layer-dir
    allowedAccounts:
      - '123456789012' # or arn:aws:iam::123456789012:root

Warning: '*' makes the layer version usable by every AWS account. Use it only for layers you mean to publish.

Use layers in functions

A function's layers lists the layer version ARNs it uses:

functions:
  hello:
    handler: handler.hello
    layers:
      - arn:aws:lambda:us-east-1:123456789012:layer:shared-utils:3

provider.layers sets layers for every function. A function's own layers replaces provider.layers for that function; the two lists are not merged.

A function attached to Cloud Extensions also gets the Cloud Extensions runtime layer, added after its own layers. That layer counts toward Lambda's limit of 5 layers per function, so such a function can list at most 4 layers of its own.

provider:
  name: aws
  runtime: python3.15
  layers:
    - arn:aws:lambda:us-east-1:123456789012:layer:mylayer1:1
    - arn:aws:lambda:us-east-1:123456789012:layer:mylayer2:3

functions:
  hello1:
    handler: handler.hello1 # both provider layers
  hello2:
    handler: handler.hello2
    layers: # only this layer
      - arn:aws:lambda:us-east-1:123456789012:layer:mylayer3:1

Reference a layer from the same service

To use a layer defined in the same service, reference its CloudFormation logical ID with Ref, which resolves to the current layer version ARN. The logical ID is the layer key with its first letter uppercased, - replaced with Dash and _ with Underscore, followed by LambdaLayer: shared becomes SharedLambdaLayer, my-layer becomes MyDashlayerLambdaLayer.

layers:
  test:
    path: layer

functions:
  hello:
    handler: handler.hello
    layers:
      - !Ref TestLambdaLayer

A layer with retain: true gets a content hash appended to its logical ID (TestLambdaLayer<hash>), so every version is a separate resource; such a layer can't be referenced with a static Ref.

Reference a layer from another service

The Framework adds a stack output <LogicalId>QualifiedArn (for example TestLambdaLayerQualifiedArn) that holds the current layer version ARN. Other services read it with the cf variable:

functions:
  hello:
    handler: handler.hello
    layers:
      - ${cf:my-service-dev.TestLambdaLayerQualifiedArn}

Related pages

  • AWS Lambda functions: the function layers setting.
  • Packaging: patterns and artifacts.
  • Creating layers in the AWS Lambda Developer Guide.
Edit this page
Prev WebsocketNextOverview

Contents

  • AWS Lambda Layers
  • Configuration
  • Layer versions and retain
  • Package a layer
  • Share a layer with other accounts
  • Use layers in functions
  • Reference a layer from the same service
  • Reference a layer from another service
  • Related pages

Related

GuidesPluginsExamplesSlack CommunitySupport