AWS CDK AWS Lambda Constructs (v1)
This package provides the AWS Cloud Development Kit (CDK) constructs for defining AWS Lambda functions using Python. It belongs to the *deprecated* AWS CDK v1 ecosystem, specifically version 1.204.0. While functional, users are strongly encouraged to migrate to AWS CDK v2 (`aws-cdk-lib`) for ongoing support, new features, and a simplified module structure. The AWS CDK has a rapid release cadence, often multiple times a week for bug fixes and minor features, with larger feature releases typically weekly or bi-weekly.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.core'
cause When using AWS CDK v1, core constructs like `App` and `Stack` reside in the `aws-cdk.core` package, which must be installed separately from `aws-cdk-aws-lambda`.fixEnsure both `aws-cdk-aws-lambda` and `aws-cdk.core` are installed: `pip install aws-cdk-aws-lambda==1.204.0 aws-cdk.core==1.204.0`. Also, verify your imports use `from aws_cdk import core as cdk`. -
TypeError: Parameter 'runtime' for Function must be of type Runtime
cause The `runtime` parameter for `lambda_.Function` expects an enum member from `lambda_.Runtime` (e.g., `lambda_.Runtime.PYTHON_3_9`), not a plain string.fixReplace string literals with the correct enum value: `runtime=lambda_.Runtime.PYTHON_3_9`. -
SynthError: Stack 'MyV1LambdaStack' contains no assets. Run 'cdk synth' to create assets for deployment.
cause This typically means the CDK could not locate the source code for your Lambda function (`code` property), or there was an issue bundling local dependencies into the deployment package.fixVerify that the `code` property correctly points to your Lambda's source (e.g., `lambda_.Code.from_asset('./lambda_src')` or `lambda_.Code.from_inline(...)`). If using `from_asset`, ensure the specified path exists and contains the handler file. Check for any errors during the `cdk synth` command that might indicate bundling issues.
Warnings
- breaking AWS 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.
- gotcha Python'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.
- gotcha Managing 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.
- gotcha The 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.
Install
-
pip install aws-cdk-aws-lambda==1.204.0 aws-cdk.core==1.204.0 -
npm install -g aws-cdk@1
Imports
- Function
from aws_cdk_lib import aws_lambda as lambda_
from aws_cdk import aws_lambda as lambda_
- Stack
from aws_cdk import Stack
from aws_cdk import core as cdk
Quickstart
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()