AWS CDK v1 CloudFormation Constructs
The `aws-cdk.aws-cloudformation` library provides AWS CDK v1 constructs for interacting with AWS CloudFormation, allowing users to define CloudFormation stacks, custom resources, and stack sets in Python. While functional, version 1.204.0 is part of AWS CDK v1 which is now in maintenance mode, with active development focused on AWS CDK v2 (`aws-cdk-lib`). It receives critical bug fixes but no new features, with releases aligning with the broader AWS CDK v1 maintenance cycle.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk_lib'
cause You are attempting to use AWS CDK v2 (`aws_cdk_lib`) imports, but only AWS CDK v1 packages like `aws-cdk.aws-cloudformation` are installed.fixEither install AWS CDK v2 via `pip install aws-cdk-lib` (and adapt your code) or stick to v1 imports (e.g., `from aws_cdk import core`, `from aws_cdk.aws_cloudformation import ...`). -
ModuleNotFoundError: No module named 'aws_cdk.aws_cloudformation'
cause The `aws-cdk.aws-cloudformation` package is not installed in your Python environment or you are trying to import it in a v2 context without the correct v2 import path.fixFor AWS CDK v1, install the package with `pip install aws-cdk.aws-cloudformation`. For AWS CDK v2, the `aws_cloudformation` module is part of `aws_cdk_lib`, so use `from aws_cdk_lib import aws_cloudformation`. -
TypeError: Expected type str, got None
cause A required property for a CDK construct, such as `template_url` for `CfnStack`, was not provided or evaluated to `None`.fixEnsure all required properties for the construct are explicitly provided with valid string values. Double-check environment variables or configuration values if they are used to populate properties. -
jsii.errors.JavaScriptError: Error: Stack 'MyCfnStackApp' cannot be deployed, as its 'templateUrl' does not point to a valid template in S3.
cause The `template_url` provided to `CfnStack` is either incorrect, inaccessible, or does not point to a well-formed CloudFormation template in an S3 bucket.fixVerify the S3 URL is correct and publicly accessible, or that your CDK deployment role has `s3:GetObject` permissions for that bucket and object. Ensure the S3 object is a valid CloudFormation template.
Warnings
- breaking AWS CDK v1 (`aws-cdk.aws-cloudformation`) is in maintenance mode. Active development, new features, and most bug fixes are concentrated on AWS CDK v2 (`aws-cdk-lib`). Migrating to v2 is highly recommended as v1 will eventually reach end-of-life.
- gotcha When using `CfnCustomResource`, you are responsible for providing the full implementation details, including the Lambda function code, execution role, and service token. This is a lower-level construct.
- gotcha The `CfnStack` construct deploys a CloudFormation template. The AWS account and IAM role used for CDK deployment must have permissions to read the `template_url` (if from S3) and to deploy all resources defined within that CloudFormation template.
- gotcha Mixing raw CloudFormation definitions via `CfnStack` with high-level CDK constructs in the same stack can sometimes lead to unexpected behavior or resource ownership conflicts if not carefully managed.
Install
-
pip install aws-cdk.aws-cloudformation
Imports
- CfnStack
from aws_cdk_lib.aws_cloudformation import CfnStack
from aws_cdk.aws_cloudformation import CfnStack
- CfnCustomResource
from aws_cdk.aws_cloudformation import CfnCustomResource
Quickstart
import os
from aws_cdk import App, Stack, Environment
from aws_cdk.aws_cloudformation import CfnStack
from constructs import Construct
class MyCfnStackExample(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
# This construct deploys an existing CloudFormation template from an S3 URL.
# Replace with a valid S3 URL to your CloudFormation template.
# Ensure the deployment role has permissions to read from this S3 location.
template_url = os.environ.get('CFN_TEMPLATE_URL', 'https://s3.amazonaws.com/your-bucket/your-template.yaml')
if not template_url.startswith('https://'):
print("WARNING: CFN_TEMPLATE_URL is not set or invalid. Using a placeholder.")
cfn_stack = CfnStack(self, "MyExistingCloudFormationStack",
template_url=template_url,
parameters={
"EnvironmentName": "Dev",
"Project": "MyCDKApp"
},
tags={
"ManagedBy": "CDK"
}
)
app = App()
MyCfnStackExample(app, "MyCfnStackApp",
env=Environment(account=os.environ.get("CDK_DEFAULT_ACCOUNT"),
region=os.environ.get("CDK_DEFAULT_REGION"))
)
app.synth()