AWS CDK Python Lambda Alpha Library
The CDK Construct Library for AWS Lambda in Python, providing constructs to define and manage Python Lambda functions. It simplifies packaging and dependency management by leveraging Docker for bundling. This is an alpha package, meaning its APIs are experimental and subject to non-backward compatible changes, with breaking changes announced in release notes rather than following strict semantic versioning. Current version is 2.248.0a0 and it is under active development.
Warnings
- breaking This library is an 'alpha' package, meaning its APIs are experimental and under active development. Breaking changes may occur in any future version without following semantic versioning, requiring code updates when upgrading.
- gotcha Bundling Python Lambda functions with this construct requires Docker to be installed and running on the machine where `cdk synth` or `cdk deploy` is executed. Without Docker, bundling will fail.
- gotcha For reproducible builds and explicit dependency management, always commit a lockfile (e.g., `requirements.txt`, `Pipfile.lock`, `uv.lock`, or `poetry.lock`) alongside your Lambda function's source code in the `entry` directory.
- breaking Mixing CDK v1 modules with CDK v2's `aws-cdk-lib` and experimental 'alpha' packages like this one is a common source of dependency resolution errors and unexpected behavior.
- gotcha Users have reported issues with bundling when using `podman` instead of `docker`, specifically related to user mapping parameters passed to the container, leading to permission denied errors.
- gotcha A `Runtime.ImportModuleError` can occur if the local file structure or dependencies of your Lambda code are not correctly resolved during bundling, or if the `index` and `handler` properties do not accurately point to your Python entry point.
Install
-
pip install aws-cdk.aws-lambda-python-alpha
Imports
- PythonFunction
from aws_cdk.aws_lambda_python_alpha import PythonFunction
- PythonLayerVersion
from aws_cdk.aws_lambda_python_alpha import PythonLayerVersion
Quickstart
import os
from aws_cdk import (
App,
Stack,
aws_lambda as _lambda,
)
from aws_cdk.aws_lambda_python_alpha import PythonFunction
from constructs import Construct
# Create a dummy lambda_code directory and handler file for the example
lambda_code_dir = os.path.join(os.path.dirname(__file__), "lambda_code")
os.makedirs(lambda_code_dir, exist_ok=True)
with open(os.path.join(lambda_code_dir, "lambda_handler.py"), "w") as f:
f.write("""
import json
def handler(event, context):
print("Lambda received event:", event)
return {
'statusCode': 200,
'body': json.dumps('Hello from Python Lambda Alpha!')
}
""")
# Add a dummy requirements.txt if you want to test dependency bundling
# with open(os.path.join(lambda_code_dir, "requirements.txt"), "w") as f:
# f.write("requests\n")
class MyPythonLambdaStack(Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
PythonFunction(
self,
"MyPythonLambda",
entry=lambda_code_dir, # Path to the directory containing your handler and dependencies
runtime=_lambda.Runtime.PYTHON_3_9, # Specify a supported Python runtime
index="lambda_handler.py", # Optional, defaults to 'index.py'
handler="handler", # Optional, defaults to 'handler'
# Set an environment variable (optional)
environment={
"MY_ENV_VAR": os.environ.get('MY_ENV_VAR', 'default_value')
}
)
app = App()
MyPythonLambdaStack(app, "MyPythonLambdaStack")
app.synth()