Skip navigation links

Package software.amazon.awscdk.services.sns

Amazon Simple Notification Service Construct Library

See: Description

Package software.amazon.awscdk.services.sns 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 https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.services.sns.*;
 
 Topic topic = new Topic(this, "Topic", new TopicProps()
         .displayName("Customer subscription topic"));
 

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

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.services.sns.*;
 
 Topic topic = new Topic(this, "Topic", new TopicProps()
         .contentBasedDeduplication(true)
         .displayName("Customer subscription topic")
         .fifo(true)
         .topicName("customerTopic"));
 

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:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.services.sns.subscriptions.*;
 
 Object myTopic = new Topic(this, "MyTopic");
 
 myTopic.addSubscription(new UrlSubscription("https://foobar.com/"));
 

Subscribe a queue to the topic:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 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:

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Topic myTopic = new Topic(this, "MyTopic");
 Function fn = new Function(this, "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.addSubscription(LambdaSubscription.Builder.create(fn)
         .filterPolicy(Map.of(
                 "color", sns.SubscriptionFilter.stringFilter(new StringConditions()
                         .allowlist(asList("red", "orange"))
                         .matchPrefixes(asList("bl"))),
                 "size", sns.SubscriptionFilter.stringFilter(new StringConditions()
                         .denylist(asList("small", "medium"))),
                 "price", sns.SubscriptionFilter.numericFilter(new NumericConditions()
                         .between(new BetweenCondition().start(100).stop(200))
                         .greaterThan(300)),
                 "store", sns.SubscriptionFilter.existsFilter()))
         .build());
 

Example of Firehose Subscription

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 import software.amazon.awscdk.services.sns.Subscription;
 import software.amazon.awscdk.services.sns.SubscriptionProtocol;
 import software.amazon.awscdk.services.sns.Topic;
 import software.amazon.awscdk.services.kinesisfirehose.DeliveryStream;
 Topic topic = new Topic(stack, "Topic");
 DeliveryStream stream = new DeliveryStream(stack, "DeliveryStream", ...);
 new Subscription(stack, "Subscription", new SubscriptionProps()
         .endpoint(stream.getDeliveryStreamArn())
         .protocol(SubscriptionProtocol.getFIREHOSE())
         .subscriptionRoleArn("SAMPLE_ARN"));
 

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 topic = new Topic(stack, "Topic");
 Object dlQueue = Queue.Builder.create(stack, "DeadLetterQueue")
         .queueName("MySubscription_DLQ")
         .retentionPeriod(cdk.Duration.days(14))
         .build();
 
 new Subscription(stack, "Subscription", new SubscriptionProps()
         .endpoint("endpoint")
         .protocol(sns.SubscriptionProtocol.getLAMBDA())
         .topic(topic)
         .deadLetterQueue(dlQueue));
 

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 software.amazon.awscdk.services.events.targets.*;
 
 codeCommitRepository.onCommit(new SnsTopic(myTopic));
 

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.

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Topic topic = new Topic(stack, "Topic");
 TopicPolicy topicPolicy = new TopicPolicy(stack, "TopicPolicy", new TopicPolicyProps()
         .topics(asList(topic)));
 
 topicPolicy.document.addStatements(new PolicyStatement(new PolicyStatementProps()
         .actions(asList("sns:Subscribe"))
         .principals(asList(new AnyPrincipal()))
         .resources(asList(topic.getTopicArn()))));
 

A policy document can also be passed on TopicPolicy construction

 // Example automatically generated without compilation. See https://github.com/aws/jsii/issues/826
 Topic topic = new Topic(stack, "Topic");
 PolicyDocument policyDocument = new PolicyDocument(new PolicyDocumentProps()
         .assignSids(true)
         .statements(asList(
             new PolicyStatement(new PolicyStatementProps()
                     .actions(asList("sns:Subscribe"))
                     .principals(asList(new AnyPrincipal()))
                     .resources(asList(topic.getTopicArn()))))));
 
 TopicPolicy topicPolicy = new TopicPolicy(this, "Policy", new TopicPolicyProps()
         .topics(asList(topic))
         .policyDocument(policyDocument));
 
Skip navigation links