The CDK Construct Library for AWS Lambda in Python
npx @tessl/cli install tessl/npm-aws-cdk--aws-lambda-python-alpha@2.214.0The AWS CDK Construct Library for AWS Lambda in Python provides constructs for deploying Python Lambda functions and layers with advanced packaging and bundling capabilities. It supports multiple Python dependency management systems and provides sophisticated Docker-based bundling for Lambda-compatible deployment packages.
npm install @aws-cdk/aws-lambda-python-alphaimport * as python from '@aws-cdk/aws-lambda-python-alpha';Individual imports:
import { PythonFunction, PythonLayerVersion, BundlingOptions } from '@aws-cdk/aws-lambda-python-alpha';import * as python from '@aws-cdk/aws-lambda-python-alpha';
import { Runtime } from 'aws-cdk-lib/aws-lambda';
import { Construct } from 'constructs';
// Basic Python function
const func = new python.PythonFunction(this, 'MyFunction', {
entry: '/path/to/my/python/code', // Directory containing your Python code
runtime: Runtime.PYTHON_3_8,
index: 'main.py', // Optional, defaults to 'index.py'
handler: 'lambda_handler', // Optional, defaults to 'handler'
});
// Python layer with dependencies
const layer = new python.PythonLayerVersion(this, 'MyLayer', {
entry: '/path/to/layer/code', // Directory with requirements.txt or other dependency files
compatibleRuntimes: [Runtime.PYTHON_3_8, Runtime.PYTHON_3_9],
});
// Function using the layer
const funcWithLayer = new python.PythonFunction(this, 'FunctionWithLayer', {
entry: '/path/to/function/code',
runtime: Runtime.PYTHON_3_8,
layers: [layer],
});The library is built around several key components:
The bundling process automatically:
Core functionality for deploying Python Lambda functions with automatic dependency bundling and cross-platform compatibility.
class PythonFunction extends Function {
static readonly PROPERTY_INJECTION_ID: string;
constructor(scope: Construct, id: string, props: PythonFunctionProps);
}
interface PythonFunctionProps extends FunctionOptions {
readonly entry: string;
readonly runtime: Runtime;
readonly index?: string;
readonly handler?: string;
readonly bundling?: BundlingOptions;
}Shared dependency layers for Python Lambda functions with multi-runtime and multi-architecture support.
class PythonLayerVersion extends LayerVersion {
static readonly PROPERTY_INJECTION_ID: string;
constructor(scope: Construct, id: string, props: PythonLayerVersionProps);
}
interface PythonLayerVersionProps extends LayerVersionOptions {
readonly entry: string;
readonly compatibleRuntimes?: Runtime[];
readonly compatibleArchitectures?: Architecture[];
readonly bundling?: BundlingOptions;
}Comprehensive bundling options for custom Docker images, private repositories, and complex deployment scenarios.
interface BundlingOptions extends DockerRunOptions {
readonly poetryIncludeHashes?: boolean;
readonly poetryWithoutUrls?: boolean;
readonly assetExcludes?: string[];
readonly outputPathSuffix?: string;
readonly image?: DockerImage;
readonly buildArgs?: { [key: string]: string };
readonly assetHashType?: AssetHashType;
readonly assetHash?: string;
readonly commandHooks?: ICommandHooks;
readonly bundlingFileAccess?: BundlingFileAccess;
}
interface ICommandHooks {
beforeBundling(inputDir: string, outputDir: string): string[];
afterBundling(inputDir: string, outputDir: string): string[];
}interface PoetryPackagingProps {
readonly poetryIncludeHashes?: boolean;
readonly poetryWithoutUrls?: boolean;
}enum DependenciesFile {
PIP = 'requirements.txt',
POETRY = 'poetry.lock',
PIPENV = 'Pipfile.lock',
UV = 'uv.lock',
NONE = '',
}
interface PackagingProps {
readonly dependenciesFile: DependenciesFile;
readonly exportCommand?: string;
}
const DEPENDENCY_EXCLUDES: string[];
const BUNDLER_DEPENDENCIES_CACHE: string;Note: The library uses automatic dependency detection and bundling. For advanced packaging scenarios and detailed information about the packaging system, see Packaging Systems.