Skip to main content

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 https://github.com/aws/jsii/issues/826
import aws_cdk.aws_sns as sns

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

Subscriptions

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 https://github.com/aws/jsii/issues/826
import aws_cdk.aws_sns_subscriptions as subs

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

my_topic.add_subscription(subs.UrlSubscription("https://foobar.com/"))

Subscribe a queue to the topic:

# Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
my_topic.add_subscription(subs.SqsSubscription(queue))

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 https://github.com/aws/jsii/issues/826
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
topic.add_subscription(subs.LambdaSubscription(fn,
    filter_policy={
        "color": sns.SubscriptionFilter.string_filter(
            whitelist=["red", "orange"],
            match_prefixes=["bl"]
        ),
        "size": sns.SubscriptionFilter.string_filter(
            blacklist=["small", "medium"]
        ),
        "price": sns.SubscriptionFilter.numeric_filter(
            between={"start": 100, "stop": 200},
            greater_than=300
        ),
        "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 https://github.com/aws/jsii/issues/826
topic = sns.Topic(stack, "Topic")
dl_queue = Queue(stack, "DeadLetterQueue",
    queue_name="MySubscription_DLQ",
    retention_period=cdk.Duration.days(14)
)

sns.Subscription(stack, "Subscription",
    endpoint="endpoint",
    protocol=sns.SubscriptionProtocol.LAMBDA,
    topic=topic,
    dead_letter_queue=dl_queue
)

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 https://github.com/aws/jsii/issues/826
import aws_cdk.aws_events_targets as targets

code_commit_repository.on_commit(targets.SnsTopic(my_topic))

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.

Release files for aws-cdk.aws-sns 1.51.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for aws-cdk.aws-sns 1.51.0
File Size Uploaded
aws-cdk.aws-sns-1.51.0.tar.gz 87.6 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for aws-cdk.aws-sns 1.51.0
File Interpreter ABI Platform
aws_cdk.aws_sns-1.51.0-py3-none-any.whl Python 3 none any Details

Total release size: 172.9 kB

Release files / aws-cdk.aws-sns-1.51.0.tar.gz

Download URL aws-cdk.aws-sns-1.51.0.tar.gz
Size 87.6 kB
Tags Source
SHA-256 checksum
How to use checksums
2e3704333bbde537ad355ed54f406872fc229e32c2ab2e8eb48f5075e9ba86dd
BLAKE2b-256 checksum
How to use checksums
4ea1d2072cb7fc4ea6ee4cc06779d774667cb02ae9f094636c8f24f3cdbc9978
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.5

Release files / aws_cdk.aws_sns-1.51.0-py3-none-any.whl

Download URL aws_cdk.aws_sns-1.51.0-py3-none-any.whl
Size 85.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
935b47c3fe3e14e782147a12e6e1a3138a902cf8eb48567bdeda0fae751e3038
BLAKE2b-256 checksum
How to use checksums
ff2b4b01f9ba48bf52ea9e7a7978c29b2690d40f8e056b72d43228260ff0de30
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.6.5

Release history Release notifications | RSS feed

1.99.0

2 release files

1.98.0

2 release files

1.95.1

2 release files

1.95.0

2 release files

1.94.1

2 release files

1.94.0

2 release files

1.93.0

2 release files

1.91.0

2 release files

1.90.1

2 release files

1.90.0

2 release files

1.87.1

2 release files

1.87.0

2 release files

1.86.0

2 release files

1.85.0

2 release files

1.84.0

2 release files

1.81.0

2 release files

1.80.0

2 release files

1.79.0

2 release files

1.78.0

2 release files

1.75.0

2 release files

1.74.0

2 release files

1.73.0

2 release files

1.71.0

2 release files

1.70.0

2 release files

1.69.0

2 release files

1.68.0

2 release files

1.64.1

2 release files

1.64.0

2 release files

1.63.0

2 release files

1.61.1

2 release files

1.61.0

2 release files

1.60.0

2 release files

1.59.0

2 release files

1.58.0

2 release files

1.55.0

2 release files

1.54.0

2 release files

1.53.0

2 release files

1.52.0

2 release files

This release

1.51.0 This release

2 release files

1.47.1

2 release files

1.47.0

2 release files

1.46.0

2 release files

1.42.0

2 release files

1.41.0

2 release files

1.40.0

2 release files

1.39.0

2 release files

1.36.1

2 release files

1.36.0

2 release files

1.35.0

2 release files

1.34.1

2 release files

1.34.0

2 release files

1.33.1

2 release files

1.33.0

2 release files

1.32.2

2 release files

1.31.0

2 release files

1.30.0

2 release files

1.29.0

2 release files

1.28.0

2 release files

1.26.0

2 release files

1.25.0

2 release files

1.24.0

2 release files

1.22.0

2 release files

1.21.1

2 release files

1.21.0

2 release files

1.19.0

2 release files

1.18.0

2 release files

1.17.1

2 release files

1.17.0

2 release files

1.16.3

2 release files

1.16.2

2 release files

1.16.1

2 release files

1.16.0

2 release files

1.15.0

2 release files

1.14.0

2 release files

1.13.1

2 release files

1.13.0

2 release files

1.10.0

2 release files

1.9.0

2 release files

1.8.0

2 release files

1.7.0

2 release files

1.6.1

2 release files

1.6.0

2 release files

1.5.0

2 release files

1.4.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.36.0

2 release files

0.35.0

2 release files

0.34.0

2 release files

0.33.0

2 release files

0.32.0

2 release files

0.29.0

2 release files

0.27.0

2 release files

0.26.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page