Package software.amazon.awscdk.services.sns


@Stability(Stable) @Deprecated package software.amazon.awscdk.services.sns
Deprecated.

Amazon Simple Notification Service Construct Library

---

End-of-Support

AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2.

For more information on how to migrate, see the Migrating to AWS CDK v2 guide.


Add an SNS Topic to your stack:

 Topic topic = Topic.Builder.create(this, "Topic")
         .displayName("Customer subscription topic")
         .build();
 

Add a FIFO SNS topic with content-based de-duplication to your stack:

 Topic topic = Topic.Builder.create(this, "Topic")
         .contentBasedDeduplication(true)
         .displayName("Customer subscription topic")
         .fifo(true)
         .topicName("customerTopic")
         .build();
 

Note that FIFO topics require a topic name to be provided. The required .fifo suffix will be automatically added to the topic name if it is not explicitly provided.

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:

 Topic myTopic = new Topic(this, "MyTopic");
 
 myTopic.addSubscription(new UrlSubscription("https://foobar.com/"));
 

Subscribe a queue to the topic:

 Queue queue;
 
 Topic myTopic = new Topic(this, "MyTopic");
 
 myTopic.addSubscription(new 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:

 import software.amazon.awscdk.services.lambda.*;
 Function fn;
 
 
 Topic myTopic = new Topic(this, "MyTopic");
 
 // 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
 myTopic.addSubscription(LambdaSubscription.Builder.create(fn)
         .filterPolicy(Map.of(
                 "color", SubscriptionFilter.stringFilter(StringConditions.builder()
                         .allowlist(List.of("red", "orange"))
                         .matchPrefixes(List.of("bl"))
                         .build()),
                 "size", SubscriptionFilter.stringFilter(StringConditions.builder()
                         .denylist(List.of("small", "medium"))
                         .build()),
                 "price", SubscriptionFilter.numericFilter(NumericConditions.builder()
                         .between(BetweenCondition.builder().start(100).stop(200).build())
                         .greaterThan(300)
                         .build()),
                 "store", SubscriptionFilter.existsFilter()))
         .build());
 

Example of Firehose Subscription

 import software.amazon.awscdk.services.kinesisfirehose.DeliveryStream;
 DeliveryStream stream;
 
 
 Topic topic = new Topic(this, "Topic");
 
 Subscription.Builder.create(this, "Subscription")
         .topic(topic)
         .endpoint(stream.getDeliveryStreamArn())
         .protocol(SubscriptionProtocol.FIREHOSE)
         .subscriptionRoleArn("SAMPLE_ARN")
         .build();
 

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.

 Topic topic = new Topic(this, "Topic");
 Queue dlQueue = Queue.Builder.create(this, "DeadLetterQueue")
         .queueName("MySubscription_DLQ")
         .retentionPeriod(Duration.days(14))
         .build();
 
 Subscription.Builder.create(this, "Subscription")
         .endpoint("endpoint")
         .protocol(SubscriptionProtocol.LAMBDA)
         .topic(topic)
         .deadLetterQueue(dlQueue)
         .build();
 

CloudWatch Event Rule Target

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

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

 import software.amazon.awscdk.services.codecommit.*;
 import software.amazon.awscdk.services.events.targets.*;
 
 Repository repo;
 
 Topic myTopic = new Topic(this, "Topic");
 
 repo.onCommit("OnCommit", OnCommitOptions.builder()
         .target(new SnsTopic(myTopic))
         .build());
 

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.

Topic Policy

A topic policy is automatically created when addToResourcePolicy is called, if one doesn't already exist. Using addToResourcePolicy is the simplest way to add policies, but a TopicPolicy can also be created manually.

 Topic topic = new Topic(this, "Topic");
 TopicPolicy topicPolicy = TopicPolicy.Builder.create(this, "TopicPolicy")
         .topics(List.of(topic))
         .build();
 
 topicPolicy.document.addStatements(PolicyStatement.Builder.create()
         .actions(List.of("sns:Subscribe"))
         .principals(List.of(new AnyPrincipal()))
         .resources(List.of(topic.getTopicArn()))
         .build());
 

A policy document can also be passed on TopicPolicy construction

 Topic topic = new Topic(this, "Topic");
 PolicyDocument policyDocument = PolicyDocument.Builder.create()
         .assignSids(true)
         .statements(List.of(
             PolicyStatement.Builder.create()
                     .actions(List.of("sns:Subscribe"))
                     .principals(List.of(new AnyPrincipal()))
                     .resources(List.of(topic.getTopicArn()))
                     .build()))
         .build();
 
 TopicPolicy topicPolicy = TopicPolicy.Builder.create(this, "Policy")
         .topics(List.of(topic))
         .policyDocument(policyDocument)
         .build();
 
Deprecated: AWS CDK v1 has reached End-of-Support on 2023-06-01. This package is no longer being updated, and users should migrate to AWS CDK v2. For more information on how to migrate, see https://docs.aws.amazon.com/cdk/v2/guide/migrating-v2.html