AWS CDK SNS Construct Library (v1)
The `aws-cdk-aws-sns` library provides AWS Cloud Development Kit (CDK) constructs specifically for AWS Simple Notification Service (SNS) resources. This package is part of AWS CDK v1, which is currently in maintenance mode. For new projects, it is strongly recommended to use AWS CDK v2 and the consolidated `aws-cdk-lib` package. AWS CDK generally has a rapid release cadence, with minor versions released frequently, and major versions (like v2) introducing significant changes.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk_lib'
cause You are trying to import AWS CDK v2 modules (`aws_cdk_lib`) but have AWS CDK v1 packages (like `aws-cdk-aws-sns`) installed, or vice-versa.fixDecide whether to use AWS CDK v1 or v2. If v2, uninstall v1 packages and install `aws-cdk-lib`. If v1, ensure all CDK related packages are v1 and imports match `from aws_cdk import ...`. -
jsii.errors.JavaScriptError: Error: Stack 'MySnsStack' is not bootstrapped. Please run 'cdk bootstrap' in the account/region.
cause The AWS environment (account and region) where you are trying to deploy has not been prepared for AWS CDK deployments. CDK needs to provision a few resources (e.g., S3 bucket, IAM roles) to manage deployments.fixRun `cdk bootstrap aws://YOUR_ACCOUNT_ID/YOUR_REGION` from your terminal, replacing with your actual AWS account ID and desired region. -
AttributeError: module 'aws_cdk' has no attribute 'aws_sns'
cause The `aws-cdk.aws-sns` package is either not installed, or there's a version mismatch between `aws-cdk.core` and `aws-cdk.aws-sns`, or you're attempting a v1 import with a v2 setup.fixEnsure both `aws-cdk.core` and `aws-cdk.aws-sns` are installed and their versions are compatible (ideally the same v1 version, e.g., `pip install aws-cdk.core==1.204.0 aws-cdk.aws-sns==1.204.0`). Verify your imports are `from aws_cdk import aws_sns`.
Warnings
- breaking This `aws-cdk-aws-sns` package is specifically for AWS CDK v1. AWS CDK v2 consolidated all constructs into a single `aws-cdk-lib` package and uses different import paths and sometimes different construct properties. Mixing v1 and v2 packages or import styles will lead to errors.
- gotcha AWS CDK requires the AWS CLI to be installed and configured with credentials for synthesizing and deploying stacks. Your AWS account and region must also be 'bootstrapped' for CDK.
- gotcha Changing the logical ID (the `construct_id` in `super().__init__(scope, construct_id, ...)` or the second argument in `sns.Topic(self, 'MyTopic', ...)`) of a construct after initial deployment will cause CloudFormation to replace the resource, leading to potential data loss or downtime.
- gotcha CDK applications can generate large CloudFormation templates, especially with many resources or complex patterns. This can hit CloudFormation template size limits (50KB or 200 parameters/resources).
Install
-
pip install aws-cdk.core==1.204.0 aws-cdk.aws-sns==1.204.0
Imports
- Topic
from aws_cdk_aws_sns import Topic
from aws_cdk import aws_sns
- Topic
from aws_cdk.aws_sns import Topic
from aws_cdk import aws_sns
Quickstart
import os
from aws_cdk import core as cdk
from aws_cdk import aws_sns as sns
class MySnsStack(cdk.Stack):
def __init__(self, scope: cdk.Construct, construct_id: str, **kwargs) -> None:
super().__init__(scope, construct_id, **kwargs)
# Create an SNS Topic
topic = sns.Topic(self, "MyCdkSnsTopic",
display_name="My First CDK SNS Topic"
)
cdk.CfnOutput(self, "TopicName", value=topic.topic_name)
cdk.CfnOutput(self, "TopicArn", value=topic.topic_arn)
app = cdk.App()
# Ensure AWS account and region are set via env vars or AWS CLI config for cdk synth/deploy
# Example: MySnsStack(app, "MySnsStack", env=cdk.Environment(account=os.environ.get('CDK_DEFAULT_ACCOUNT'), region=os.environ.get('CDK_DEFAULT_REGION')))
MySnsStack(app, "MySnsStack")
app.synth()