or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bundling-configuration.mdindex.mdpackaging-systems.mdpython-functions.mdpython-layers.md
tile.json

python-layers.mddocs/

Python Lambda Layers

Shared dependency layers for Python Lambda functions with multi-runtime and multi-architecture support.

Capabilities

PythonLayerVersion Class

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);
}

PythonLayerVersion Properties

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;
}

Usage Examples

Basic Layer with Requirements

Directory structure:

layer/
├── requirements.txt  # Dependencies to include in layer
└── python/          # Optional: additional Python modules
    └── shared.py
import * 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],
});

Layer with Poetry Dependencies

Directory structure:

layer/
├── pyproject.toml   # Poetry project configuration
├── poetry.lock      # Poetry lock file
└── python/         # Optional: additional modules
    └── utils.py
const poetryLayer = new python.PythonLayerVersion(this, 'PoetryLayer', {
  entry: './layer',
  compatibleRuntimes: [Runtime.PYTHON_3_9],
  bundling: {
    poetryIncludeHashes: true,
    poetryWithoutUrls: false,
  },
});

Layer with Pipenv Dependencies

Directory structure:

layer/
├── Pipfile         # Pipenv dependencies
├── Pipfile.lock    # Pipenv lock file
└── python/
    └── helpers.py
const pipenvLayer = new python.PythonLayerVersion(this, 'PipenvLayer', {
  entry: './layer',
  compatibleRuntimes: [Runtime.PYTHON_3_8],
});

Multi-Architecture Layer

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,
  ],
});

Layer with Custom Bundling

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',
      ],
    },
  },
});

Using Layers in Functions

// 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],
});

Layer with No Dependencies

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()
});

Layer with Custom Output Path

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
  },
});

Layer Structure

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:

  1. Detects dependency files in the entry directory
  2. Installs dependencies to the python/ directory structure
  3. Copies any additional Python files from the entry directory
  4. Creates a Lambda-compatible layer package

Runtime Validation

Layer 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
  ],
});

Best Practices

Layer Organization

  • Shared Dependencies: Put common packages like requests, boto3 in layers
  • Utility Modules: Include shared utility functions and classes
  • Size Limits: Keep layers under 250MB (unzipped) to avoid deployment issues
  • Versioning: Use descriptive layer names and maintain multiple versions

Dependency Management

  • Use Lock Files: Always include lock files (poetry.lock, Pipfile.lock) for reproducible builds
  • Minimize Dependencies: Only include necessary packages to reduce layer size
  • Architecture Compatibility: Ensure dependencies work across your target architectures

Performance Optimization

  • Layer Caching: Layers are cached across function invocations
  • Cold Start Impact: Layers add minimal cold start time compared to bundling with each function
  • Shared State: Layers are loaded once per container, improving performance for frequently used dependencies

Common Patterns

Database Connectivity Layer

// Layer with database connection utilities
const dbLayer = new python.PythonLayerVersion(this, 'DatabaseLayer', {
  entry: './layers/database',  // Contains psycopg2, pymongo, etc.
  compatibleRuntimes: [Runtime.PYTHON_3_8],
});

AWS SDK Extensions Layer

// 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],
});

Data Processing Layer

// 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
});

Error Handling

Common errors and their solutions:

  • "Only PYTHON runtimes are supported": All compatible runtimes must be Python family
  • Layer size too large: Reduce dependencies or split into multiple layers
  • Architecture mismatch: Ensure dependencies are available for target architectures
  • Import errors in functions: Verify layer is included in function's layers array