AWS CDK SAM Construct Library

raw JSON →
1.204.0 verified Mon Apr 27 auth: no python maintenance

The CDK Construct Library for the AWS Serverless Application Model (SAM) resources. Version 1.204.0 is the latest release for CDK v1 (maintenance mode). This library is part of the AWS CDK v1 which entered maintenance on June 1, 2022 and will end support on June 1, 2025. Users are strongly encouraged to migrate to CDK v2 where SAM constructs are available via the 'aws-cdk-lib/aws-sam' module.

pip install aws-cdk-aws-sam==1.204.0
error ModuleNotFoundError: No module named 'aws_cdk.aws_sam'
cause Installed CDK v2 but tried to import SAM from the v1 package path.
fix
Install aws-cdk-aws-sam (v1) or use 'from aws_cdk.aws_sam import ...' from 'aws-cdk-lib' (v2).
error ImportError: cannot import name 'CfnServerlessFunction' from 'aws_cdk.aws_sam'
cause The SAM construct names may differ between CDK versions or the package is not installed.
fix
Ensure aws-cdk-aws-sam is installed and check that the class name exists in the installed version.
error 'module' object has no attribute 'aws_sam'
cause Trying to access aws_sam as a submodule directly (e.g., import aws_cdk; aws_cdk.aws_sam) without importing it first.
fix
Use 'from aws_cdk.aws_sam import ...' explicitly.
deprecated CDK v1 is in maintenance mode and will reach end-of-life on June 1, 2025. All active development has shifted to CDK v2.
fix Migrate to CDK v2: replace 'aws-cdk-aws-sam' with 'aws-cdk-lib/aws-sam'.
breaking In CDK v2, SAM constructs are no longer in a separate package. Import paths and class names may differ. The package 'aws-cdk-aws-sam' does not exist for CDK v2.
fix Use 'from aws_cdk.aws_sam import ...' via 'aws-cdk-lib' (v2) or install 'aws-cdk-lib' directly.
gotcha The SAM package is only compatible with CDK v1. If you install both v1 and v2 packages in the same environment, you may encounter import conflicts.
fix Use a virtual environment and install only one CDK version.
deprecated The 'aws-cdk-aws-sam' package will not receive new features; only critical bug fixes.
fix Evaluate migration to CDK v2 for ongoing support.
pip install aws-cdk-aws-sam

Basic CDK v1 SAM application with a Lambda function triggered by an API Gateway event.

from aws_cdk import core
from aws_cdk.aws_sam import CfnServerlessFunction

app = core.App()
stack = core.Stack(app, "MySamStack")

CfnServerlessFunction(stack, "MyFunction",
    code_uri="./",
    handler="index.handler",
    runtime="python3.9",
    events={
        "MyApi": {
            "Type": "Api",
            "Properties": {
                "Path": "/hello",
                "Method": "GET"
            }
        }
    }
)

app.synth()