{"id":8860,"library":"aws-cdk-aws-lambda","title":"AWS CDK AWS Lambda Constructs (v1)","description":"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.","status":"deprecated","version":"1.204.0","language":"en","source_language":"en","source_url":"https://github.com/aws/aws-cdk.git","tags":["aws","cdk","lambda","infrastructure-as-code","cloud","deprecated","v1"],"install":[{"cmd":"pip install aws-cdk-aws-lambda==1.204.0 aws-cdk.core==1.204.0","lang":"bash","label":"Install specific v1 Python packages"},{"cmd":"npm install -g aws-cdk@1","lang":"bash","label":"Install the AWS CDK v1 CLI (required for deployment)"}],"dependencies":[{"reason":"Provides core CDK constructs like App and Stack for v1 projects.","package":"aws-cdk.core","optional":false},{"reason":"Foundational library for CDK constructs, required by all CDK versions.","package":"constructs","optional":false}],"imports":[{"note":"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.","wrong":"from aws_cdk_lib import aws_lambda as lambda_","symbol":"Function","correct":"from aws_cdk import aws_lambda as lambda_"},{"note":"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.","wrong":"from aws_cdk import Stack","symbol":"Stack","correct":"from aws_cdk import core as cdk"}],"quickstart":{"code":"import os\nfrom aws_cdk import (\n    core as cdk, # For App, Stack\n    aws_lambda as lambda_,\n)\nfrom constructs import Construct\n\nclass MyLambdaStack(cdk.Stack):\n    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:\n        super().__init__(scope, construct_id, **kwargs)\n\n        lambda_.Function(\n            self, \"MyHelloWorldFunction\",\n            runtime=lambda_.Runtime.PYTHON_3_9,\n            handler=\"main.handler\",\n            code=lambda_.Code.from_inline(\n                \"import json\\n\"\n                \"def handler(event, context):\\n\"\n                \"    return {\\n\"\n                \"        'statusCode': 200,\\n\"\n                \"        'body': json.dumps('Hello from CDK Lambda (v1)!')\\n\"\n                \"    }\"\n            ),\n            environment={\"GREETING\": os.environ.get('LAMBDA_GREETING', 'World')}\n        )\n\n# Instantiate the app and stack\napp = cdk.App()\nMyLambdaStack(app, \"MyV1LambdaStack\", env=cdk.Environment(\n    account=os.environ.get('CDK_DEFAULT_ACCOUNT'),\n    region=os.environ.get('CDK_DEFAULT_REGION')\n))\napp.synth()","lang":"python","description":"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`)."},"warnings":[{"fix":"Plan 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).","message":"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.","severity":"breaking","affected_versions":"All versions of aws-cdk-aws-lambda (v1)"},{"fix":"Use `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.","message":"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.","severity":"gotcha","affected_versions":"All AWS CDK versions (v1 and v2) using Python"},{"fix":"For 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`.","message":"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.","severity":"gotcha","affected_versions":"All AWS CDK versions (v1 and v2)"},{"fix":"Always 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.","message":"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.","severity":"gotcha","affected_versions":"All AWS CDK versions (v1 and v2)"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Ensure 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`.","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`.","error":"ModuleNotFoundError: No module named 'aws_cdk.core'"},{"fix":"Replace string literals with the correct enum value: `runtime=lambda_.Runtime.PYTHON_3_9`.","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.","error":"TypeError: Parameter 'runtime' for Function must be of type Runtime"},{"fix":"Verify 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.","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.","error":"SynthError: Stack 'MyV1LambdaStack' contains no assets. Run 'cdk synth' to create assets for deployment."}]}