Skip to main content

CDK Constructs for AWS SNS

Project description

Amazon Simple Notification Service Construct Library


cfn-resources: Stable

cdk-constructs: Stable

Add an SNS Topic to your stack:

# Example automatically generated without compilation. See
import aws_cdk.aws_sns as sns

topic = sns.Topic(self, "Topic",
    display_name="Customer subscription topic"


Various subscriptions can be added to the topic by calling the .addSubscription(...) method on the topic. It accepts a subscription object, default implementations of which can be found in the @aws-cdk/aws-sns-subscriptions package:

Add an HTTPS Subscription to your topic:

# Example automatically generated without compilation. See
import aws_cdk.aws_sns_subscriptions as subs

my_topic = sns.Topic(self, "MyTopic")


Subscribe a queue to the topic:

# Example automatically generated without compilation. See

Note that subscriptions of queues in different accounts need to be manually confirmed by reading the initial message from the queue and visiting the link found in it.

Filter policy

A filter policy can be specified when subscribing an endpoint to a topic.

Example with a Lambda subscription:

# Example automatically generated without compilation. See
my_topic = sns.Topic(self, "MyTopic")
fn = lambda.Function(self, "Function", ...)

# Lambda should receive only message matching the following conditions on attributes:
# color: 'red' or 'orange' or begins with 'bl'
# size: anything but 'small' or 'medium'
# price: between 100 and 200 or greater than 300
# store: attribute must be present
        "color": sns.SubscriptionFilter.string_filter(
            whitelist=["red", "orange"],
        "size": sns.SubscriptionFilter.string_filter(
            blacklist=["small", "medium"]
        "price": sns.SubscriptionFilter.numeric_filter(
            between={"start": 100, "stop": 200},
        "store": sns.SubscriptionFilter.exists_filter()

DLQ setup for SNS Subscription

CDK can attach provided Queue as DLQ for your SNS subscription. See the SNS DLQ configuration docs for more information about this feature.

Example of usage with user provided DLQ.

# Example automatically generated without compilation. See
topic = sns.Topic(stack, "Topic")
dl_queue = Queue(stack, "DeadLetterQueue",

sns.Subscription(stack, "Subscription",

CloudWatch Event Rule Target

SNS topics can be used as targets for CloudWatch event rules.

Use the @aws-cdk/aws-events-targets.SnsTopic:

# Example automatically generated without compilation. See
import aws_cdk.aws_events_targets as targets


This will result in adding a target to the event rule and will also modify the topic resource policy to allow CloudWatch events to publish to the topic.

Project details

Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for, version 1.51.0
Filename, size File type Python version Upload date Hashes
Filename, size aws_cdk.aws_sns-1.51.0-py3-none-any.whl (85.3 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size (87.6 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page