Install & Compatibility
Where this runs
tested against v1.204.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.000s · 55.2MB
glibcpy 3.10–3.920 runs
installs and imports cleanly · install 6.7s · import 0.000s · 56MB
57MB installed
● package 57MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Function
✓ from aws_cdk import aws_lambda as lambda_
✗ from aws_cdk_lib import aws_lambda as lambda_
This import style (`from aws_cdk import aws_lambda`) is for AWS CDK v1. The `aws_cdk.core` module is also often imported as `cdk` alongside. For v2, you import `aws_lambda` directly from the unified `aws_cdk_lib` package.
Stack
✓ from aws_cdk import core as cdk
✗ from aws_cdk import Stack
In AWS CDK v1, `Stack` and `App` are located within the `aws_cdk.core` module, typically imported as `cdk`. Importing `Stack` directly from `aws_cdk` is the convention for AWS CDK v2, which uses the `aws-cdk-lib` package.
This quickstart demonstrates how to define a basic AWS Lambda function using `aws-cdk-aws-lambda` (v1) in Python. It creates a `cdk.Stack` and then adds a `lambda_.Function` with inline code. Ensure you have the AWS CDK v1 CLI and Python packages installed, and your AWS credentials configured, then run `cdk deploy` from your terminal in the directory where this code is saved (e.g., in `app.py`).
import os
from aws_cdk import (
core as cdk, # For App, Stack
aws_lambda as lambda_,
)
from constructs import Construct
class MyLambdaStack(cdk.Stack):
def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
lambda_.Function(
self, "MyHelloWorldFunction",
runtime=lambda_.Runtime.PYTHON_3_9,
handler="main.handler",
code=lambda_.Code.from_inline(
"import json\n"
"def handler(event, context):\n"
" return {\n"
" 'statusCode': 200,\n"
" 'body': json.dumps('Hello from CDK Lambda (v1)!')\n"
" }"
),
environment={"GREETING": os.environ.get('LAMBDA_GREETING', 'World')}
)
# Instantiate the app and stack
app = cdk.App()
MyLambdaStack(app, "MyV1LambdaStack", env=cdk.Environment(
account=os.environ.get('CDK_DEFAULT_ACCOUNT'),
region=os.environ.get('CDK_DEFAULT_REGION')
))
app.synth()
cdk --version
Debug
Known issues
breakingAWS CDK v1 is officially deprecated and no longer receives new features or critical updates. All new development should use AWS CDK v2 (`aws-cdk-lib`). Migrating from v1 to v2 involves significant import changes and potential refactoring.fixPlan a migration to AWS CDK v2 (`pip install aws-cdk-lib` and `npm install -g aws-cdk`). Refer to the official AWS CDK migration guide for detailed steps (docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html).
affects: All versions of aws-cdk-aws-lambda (v1)
gotchaPython's built-in `lambda` keyword conflicts with the `aws_lambda` module name. Always alias `aws_lambda` to `lambda_` (with an underscore) to avoid syntax errors and ensure your code is readable.fixUse `from aws_cdk import aws_lambda as lambda_` (for v1) or `from aws_cdk_lib import aws_lambda as lambda_` (for v2) when importing the Lambda constructs.
affects: All AWS CDK versions (v1 and v2) using Python
gotchaManaging dependencies for Lambda functions requires specific packaging strategies. Simply `pip install` in your CDK project's environment will not bundle external libraries into the Lambda's deployment package.fixFor local dependencies, place them in the same directory as your handler or use `lambda_.Code.from_asset('path/to/my_lambda_code', bundling={...})`. For common libraries, consider using `lambda_.LayerVersion`. affects: All AWS CDK versions (v1 and v2)
gotchaThe Python runtime versions supported by AWS Lambda constructs are tied to actual AWS Lambda service support. Using an older `Runtime` enum (e.g., `PYTHON_3_7` for a new deployment) might lead to deprecation warnings or prevent future updates as AWS phases out older runtimes.fixAlways use the latest stable and supported Python runtime for your Lambda functions (e.g., `lambda_.Runtime.PYTHON_3_9` or `PYTHON_3_10` depending on the CDK version and current AWS support) unless there's a strong reason for an older one.
affects: All AWS CDK versions (v1 and v2)
Upgrade
Version history
1.204.0latest on PyPI · released Jun 19, 2023
Audit
Dependencies
aws-cdk.corerequiredProvides core CDK constructs like App and Stack for v1 projects.
constructsrequiredFoundational library for CDK constructs, required by all CDK versions.