Shared dependency layers for Python Lambda functions with multi-runtime and multi-architecture support.
Creates Lambda layers containing Python dependencies and libraries that can be shared across multiple functions.
/**
* A lambda layer version for Python dependencies
* Extends aws-cdk-lib/aws-lambda.LayerVersion with Python-specific bundling
*/
class PythonLayerVersion extends LayerVersion {
/** Uniquely identifies this class for property injection */
static readonly PROPERTY_INJECTION_ID: string;
/**
* Creates a new Python Lambda layer
* @param scope - Construct scope
* @param id - Construct identifier
* @param props - Python layer configuration properties
*/
constructor(scope: Construct, id: string, props: PythonLayerVersionProps);
}Configuration interface for Python layer deployment.
/**
* Properties for PythonLayerVersion
* Extends standard Lambda layer options with Python-specific settings
*/
interface PythonLayerVersionProps extends LayerVersionOptions {
/**
* The path to the root directory of the lambda layer
* Should contain dependency files (requirements.txt, poetry.lock, etc.)
*/
readonly entry: string;
/**
* The runtimes compatible with the python layer
* All runtimes must be from the Python family
* @default [Runtime.PYTHON_3_7]
*/
readonly compatibleRuntimes?: Runtime[];
/**
* The system architectures compatible with this layer
* @default [Architecture.X86_64]
*/
readonly compatibleArchitectures?: Architecture[];
/**
* Bundling options for custom Docker images, dependency management, and build configuration
* @default - Use default bundling with auto-detected dependency management
*/
readonly bundling?: BundlingOptions;
}Directory structure:
layer/
├── requirements.txt # Dependencies to include in layer
└── python/ # Optional: additional Python modules
└── shared.pyimport * as python from '@aws-cdk/aws-lambda-python-alpha';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
const layer = new python.PythonLayerVersion(this, 'MyLayer', {
entry: './layer',
compatibleRuntimes: [Runtime.PYTHON_3_8, Runtime.PYTHON_3_9],
});Directory structure:
layer/
├── pyproject.toml # Poetry project configuration
├── poetry.lock # Poetry lock file
└── python/ # Optional: additional modules
└── utils.pyconst poetryLayer = new python.PythonLayerVersion(this, 'PoetryLayer', {
entry: './layer',
compatibleRuntimes: [Runtime.PYTHON_3_9],
bundling: {
poetryIncludeHashes: true,
poetryWithoutUrls: false,
},
});Directory structure:
layer/
├── Pipfile # Pipenv dependencies
├── Pipfile.lock # Pipenv lock file
└── python/
└── helpers.pyconst pipenvLayer = new python.PythonLayerVersion(this, 'PipenvLayer', {
entry: './layer',
compatibleRuntimes: [Runtime.PYTHON_3_8],
});import { Architecture } from 'aws-cdk-lib/aws-lambda';
const multiArchLayer = new python.PythonLayerVersion(this, 'MultiArchLayer', {
entry: './layer',
compatibleRuntimes: [Runtime.PYTHON_3_9],
compatibleArchitectures: [
Architecture.X86_64,
Architecture.ARM_64,
],
});const customLayer = new python.PythonLayerVersion(this, 'CustomLayer', {
entry: './layer',
compatibleRuntimes: [Runtime.PYTHON_3_8],
bundling: {
assetExcludes: ['*.pyc', '__pycache__', '.git'],
commandHooks: {
beforeBundling: (inputDir, outputDir) => [
'pip install --upgrade pip',
],
afterBundling: (inputDir, outputDir) => [
'find . -name "*.pyc" -delete',
],
},
},
});// Create a layer with common dependencies
const dependenciesLayer = new python.PythonLayerVersion(this, 'DepsLayer', {
entry: './shared-dependencies',
compatibleRuntimes: [Runtime.PYTHON_3_8],
});
// Create functions that use the layer
const func1 = new python.PythonFunction(this, 'Function1', {
entry: './function1',
runtime: Runtime.PYTHON_3_8,
layers: [dependenciesLayer],
});
const func2 = new python.PythonFunction(this, 'Function2', {
entry: './function2',
runtime: Runtime.PYTHON_3_8,
layers: [dependenciesLayer],
});For layers containing only Python modules without external dependencies:
const utilsLayer = new python.PythonLayerVersion(this, 'UtilsLayer', {
entry: './utils', // Directory with only .py files
compatibleRuntimes: [Runtime.PYTHON_3_8, Runtime.PYTHON_3_9],
// No dependency files present - will use withNoPackaging()
});const customOutputLayer = new python.PythonLayerVersion(this, 'CustomOutput', {
entry: './layer',
compatibleRuntimes: [Runtime.PYTHON_3_8],
bundling: {
outputPathSuffix: 'python', // Default for layers
// This creates the standard layer structure expected by Lambda
},
});Python Lambda layers follow AWS Lambda's standard structure:
layer-package.zip
└── python/
├── installed_package/ # pip-installed packages
│ └── __init__.py
├── your_module.py # Your custom modules
└── requirements.txt # (if present in source)The bundling process automatically:
python/ directory structureLayer runtimes are validated to ensure only Python runtimes are specified:
// ✅ Valid - All Python runtimes
new python.PythonLayerVersion(this, 'Valid', {
entry: './layer',
compatibleRuntimes: [
Runtime.PYTHON_3_7,
Runtime.PYTHON_3_8,
Runtime.PYTHON_3_9,
],
});
// ❌ Invalid - Will throw error
new python.PythonLayerVersion(this, 'Invalid', {
entry: './layer',
compatibleRuntimes: [
Runtime.PYTHON_3_8,
Runtime.NODEJS_18_X, // Error: Only PYTHON runtimes are supported
],
});requests, boto3 in layers// Layer with database connection utilities
const dbLayer = new python.PythonLayerVersion(this, 'DatabaseLayer', {
entry: './layers/database', // Contains psycopg2, pymongo, etc.
compatibleRuntimes: [Runtime.PYTHON_3_8],
});// Layer with additional AWS SDK utilities
const awsLayer = new python.PythonLayerVersion(this, 'AwsUtilsLayer', {
entry: './layers/aws-utils', // Contains boto3 extensions, custom clients
compatibleRuntimes: [Runtime.PYTHON_3_8, Runtime.PYTHON_3_9],
});// Layer with data science libraries
const dataLayer = new python.PythonLayerVersion(this, 'DataLayer', {
entry: './layers/data', // Contains pandas, numpy, scipy
compatibleRuntimes: [Runtime.PYTHON_3_8],
compatibleArchitectures: [Architecture.X86_64], // Some data libraries are x86 only
});Common errors and their solutions: