or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

api-gateway.mddata-state.mdindex.mdlambda-layers.mdserverless-applications.mdserverless-functions.md
tile.json

lambda-layers.mddocs/

Lambda Layers

AWS SAM construct for creating Lambda layer versions that enable sharing code and dependencies across multiple Lambda functions.

Capabilities

CfnLayerVersion

Creates a Lambda layer version for sharing code, libraries, and dependencies.

/**
 * AWS::Serverless::LayerVersion - Lambda layer version
 */
class CfnLayerVersion extends cdk.CfnResource {
  constructor(
    scope: cdk.Construct,
    id: string,
    props: CfnLayerVersionProps
  );
}

interface CfnLayerVersionProps {
  /** Layer content location */
  contentUri?: any;
  /** Compatible Lambda runtimes */
  compatibleRuntimes?: string[];
  /** Layer description */
  description?: string;
  /** Layer name override */
  layerName?: string;
  /** License information */
  licenseInfo?: string;
  /** Retention policy for old versions */
  retentionPolicy?: string;
  /** Compatible processor architectures */
  compatibleArchitectures?: string[];
}

Usage Examples:

import * as cdk from '@aws-cdk/core';
import * as sam from '@aws-cdk/aws-sam';

const stack = new cdk.Stack();

// Basic layer with local content
new sam.CfnLayerVersion(stack, 'UtilsLayer', {
  contentUri: './layers/utils',
  compatibleRuntimes: ['nodejs14.x', 'nodejs16.x'],
  description: 'Common utility functions and libraries',
});

// Layer from S3 with multiple runtimes
new sam.CfnLayerVersion(stack, 'SharedLibraries', {
  contentUri: {
    bucket: 'my-layers-bucket',
    key: 'libraries/shared-libs-v1.2.0.zip',
  },
  compatibleRuntimes: [
    'python3.8',
    'python3.9',
    'python3.10',
  ],
  compatibleArchitectures: ['x86_64', 'arm64'],
  layerName: 'SharedPythonLibraries',
  description: 'Shared Python libraries including requests, pandas, and numpy',
  licenseInfo: 'MIT',
  retentionPolicy: 'Delete',
});

// Layer for AWS SDK and common dependencies
new sam.CfnLayerVersion(stack, 'AwsSdkLayer', {
  contentUri: './layers/aws-sdk',
  compatibleRuntimes: ['nodejs14.x', 'nodejs16.x', 'nodejs18.x'],
  description: 'Latest AWS SDK and common Node.js dependencies',
  layerName: 'AwsSdkCommon',
  licenseInfo: 'Apache-2.0',
});

// Database connection layer
new sam.CfnLayerVersion(stack, 'DatabaseLayer', {
  contentUri: {
    bucket: 'corporate-layers',
    key: 'database/db-connection-layer.zip',
    version: 'v2.1.0',
  },
  compatibleRuntimes: ['python3.9'],
  description: 'Database connection utilities and ORM libraries',
  layerName: 'CorporateDatabaseLayer',
  retentionPolicy: 'Retain',
});

Property Types

Content Location

The contentUri property accepts either a local path or S3 location:

// Local directory or zip file
contentUri: './layers/my-layer'
contentUri: './layers/my-layer.zip'

// S3 location
contentUri: {
  bucket: 'my-bucket',
  key: 'layers/my-layer.zip',
  version?: 'version-id' // Optional S3 object version
}

Runtime Compatibility

// Node.js runtimes
compatibleRuntimes: [
  'nodejs12.x',
  'nodejs14.x', 
  'nodejs16.x',
  'nodejs18.x'
]

// Python runtimes
compatibleRuntimes: [
  'python3.7',
  'python3.8',
  'python3.9',
  'python3.10',
  'python3.11'
]

// Java runtimes
compatibleRuntimes: [
  'java8',
  'java8.al2',
  'java11'
]

// .NET runtimes
compatibleRuntimes: [
  'dotnetcore3.1',
  'dotnet6'
]

// Other runtimes
compatibleRuntimes: [
  'go1.x',
  'ruby2.7',
  'provided',
  'provided.al2'
]

Architecture Compatibility

// x86_64 (Intel/AMD)
compatibleArchitectures: ['x86_64']

// ARM64 (Graviton2)
compatibleArchitectures: ['arm64']

// Both architectures
compatibleArchitectures: ['x86_64', 'arm64']

Retention Policy

// Automatically delete old versions
retentionPolicy: 'Delete'

// Keep old versions
retentionPolicy: 'Retain'

Layer Organization Patterns

Utility Libraries Layer

// Common utilities used across multiple functions
new sam.CfnLayerVersion(stack, 'CommonUtils', {
  contentUri: './layers/common-utils',
  compatibleRuntimes: ['nodejs16.x'],
  description: 'Common utility functions: validation, formatting, encryption',
  layerName: 'CommonUtilities',
});

// Directory structure for ./layers/common-utils:
// └── nodejs/
//     └── node_modules/
//         └── utils/
//             ├── validation.js
//             ├── formatting.js
//             └── encryption.js

Third-Party Dependencies Layer

// Heavy dependencies layer to reduce function package size
new sam.CfnLayerVersion(stack, 'HeavyDependencies', {
  contentUri: './layers/heavy-deps',
  compatibleRuntimes: ['python3.9'],
  description: 'Heavy dependencies: pandas, numpy, scipy, matplotlib',
  layerName: 'DataScienceLibraries',
});

// Directory structure for ./layers/heavy-deps:
// └── python/
//     └── lib/
//         └── python3.9/
//             └── site-packages/
//                 ├── pandas/
//                 ├── numpy/
//                 ├── scipy/
//                 └── matplotlib/

Configuration and Secrets Layer

// Shared configuration and connection utilities
new sam.CfnLayerVersion(stack, 'ConfigLayer', {
  contentUri: './layers/config',
  compatibleRuntimes: ['python3.9'],
  description: 'Shared configuration, database connections, and API clients',
  layerName: 'SharedConfiguration',
});

// Directory structure for ./layers/config:
// └── python/
//     └── lib/
//         └── python3.9/
//             └── site-packages/
//                 └── shared_config/
//                     ├── __init__.py
//                     ├── database.py
//                     ├── api_clients.py
//                     └── config.py

Usage in Functions

Referencing Layers in Functions

import * as sam from '@aws-cdk/aws-sam';

// Create the layer
const utilsLayer = new sam.CfnLayerVersion(stack, 'UtilsLayer', {
  contentUri: './layers/utils',
  compatibleRuntimes: ['nodejs16.x'],
  layerName: 'UtilityFunctions',
});

// Use layer in function
new sam.CfnFunction(stack, 'MyFunction', {
  codeUri: './src',
  runtime: 'nodejs16.x',
  handler: 'index.handler',
  layers: [utilsLayer.ref], // Reference the layer
});

// Multiple layers
const dbLayer = new sam.CfnLayerVersion(stack, 'DatabaseLayer', {
  contentUri: './layers/database',
  compatibleRuntimes: ['nodejs16.x'],
});

new sam.CfnFunction(stack, 'DataProcessor', {
  codeUri: './src/processor',
  runtime: 'nodejs16.x',
  handler: 'processor.handler',
  layers: [
    utilsLayer.ref,
    dbLayer.ref,
  ],
});

Layer ARN References

// Reference existing layer by ARN
new sam.CfnFunction(stack, 'FunctionWithExistingLayer', {
  codeUri: './src',
  runtime: 'python3.9',
  handler: 'app.handler',
  layers: [
    'arn:aws:lambda:us-east-1:123456789012:layer:MyExistingLayer:1',
    'arn:aws:lambda:us-east-1:580247275435:layer:LambdaInsightsExtension:14', // AWS managed layer
  ],
});

Best Practices

Layer Size and Performance

// Keep layers focused and lightweight
new sam.CfnLayerVersion(stack, 'OptimizedLayer', {
  contentUri: './layers/optimized',
  compatibleRuntimes: ['nodejs16.x'],
  description: 'Optimized layer - only essential dependencies',
  // Tip: Layer should be < 50MB unzipped for best performance
});

Version Management

// Use consistent naming and versioning
new sam.CfnLayerVersion(stack, 'UtilsLayerV2', {
  contentUri: './layers/utils-v2.0.0',
  compatibleRuntimes: ['nodejs16.x'],
  layerName: 'UtilityFunctions', // Same name, new version
  description: 'Utility functions v2.0.0 - Added async/await support',
});

Multi-Runtime Support

// Layer that supports multiple language runtimes
new sam.CfnLayerVersion(stack, 'MonitoringLayer', {
  contentUri: './layers/monitoring',
  compatibleRuntimes: [
    'nodejs16.x',
    'python3.9',
    'java11',
  ],
  description: 'Cross-language monitoring and logging utilities',
  // Directory structure should include subdirectories for each runtime:
  // └── monitoring-layer/
  //     ├── nodejs/node_modules/monitoring/
  //     ├── python/lib/python3.9/site-packages/monitoring/
  //     └── java/lib/monitoring.jar
});

Corporate/Shared Layers

// Corporate standard layer
new sam.CfnLayerVersion(stack, 'CorporateStandards', {
  contentUri: {
    bucket: 'corporate-lambda-layers',
    key: 'standards/corporate-utilities-v3.1.0.zip',
  },
  compatibleRuntimes: ['nodejs16.x', 'python3.9'],
  layerName: 'CorporateStandardUtilities',
  description: 'Corporate standard utilities, logging, and security libraries',
  licenseInfo: 'Proprietary - Internal Use Only',
  retentionPolicy: 'Retain', // Keep for compliance
});