# Amazon Simple Notification Service Construct Library

Add an SNS Topic to your stack:

```ts
const topic = new sns.Topic(this, 'Topic', {
  displayName: 'Customer subscription topic',
});
```

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

```ts
const topic = new sns.Topic(this, 'Topic', {
  contentBasedDeduplication: true,
  displayName: 'Customer subscription topic',
  fifo: true,
});
```

Add an SNS Topic to your stack with a specified signature version, which corresponds
to the hashing algorithm used while creating the signature of the notifications,
subscription confirmations, or unsubscribe confirmation messages sent by Amazon SNS.

The default signature version is `1` (`SHA1`).
SNS also supports signature version `2` (`SHA256`).

```ts
const topic = new sns.Topic(this, 'Topic', {
  signatureVersion: '2',
});
```

Note that FIFO topics require a topic name to be provided. The required `.fifo` suffix will be automatically generated and 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-lib/aws-sns-subscriptions` package:

Add an HTTPS Subscription to your topic:

```ts
const myTopic = new sns.Topic(this, 'MyTopic');

myTopic.addSubscription(new subscriptions.UrlSubscription('https://foobar.com/'));
```

Subscribe a queue to the topic:

```ts
declare const queue: sqs.Queue;
const myTopic = new sns.Topic(this, 'MyTopic');

myTopic.addSubscription(new subscriptions.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.

 The `topic.grants.subscribe` method adds a policy statement to the topic's resource policy, allowing the specified principal to perform the `sns:Subscribe` action.
 It's useful when you want to allow entities, such as another AWS account or resources created later, to subscribe to the topic at their own pace, separating permission granting from the actual subscription process.

```ts
declare const accountPrincipal: iam.AccountPrincipal;
const myTopic = new sns.Topic(this, 'MyTopic');

myTopic.grants.subscribe(accountPrincipal);
```

### Filter policy

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

Example with a Lambda subscription:

```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

const myTopic = new sns.Topic(this, 'MyTopic');
declare const fn: lambda.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
myTopic.addSubscription(new subscriptions.LambdaSubscription(fn, {
  filterPolicy: {
    color: sns.SubscriptionFilter.stringFilter({
      allowlist: ['red', 'orange'],
      matchPrefixes: ['bl'],
      matchSuffixes: ['ue'],
    }),
    size: sns.SubscriptionFilter.stringFilter({
      denylist: ['small', 'medium'],
    }),
    price: sns.SubscriptionFilter.numericFilter({
      between: { start: 100, stop: 200 },
      greaterThan: 300,
    }),
    store: sns.SubscriptionFilter.existsFilter(),
  },
}));
```

#### Payload-based filtering

To filter messages based on the payload or body of the message, use the `filterPolicyWithMessageBody` property. This type of filter policy supports creating filters on nested objects.

Example with a Lambda subscription:

```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

const myTopic = new sns.Topic(this, 'MyTopic');
declare const fn: lambda.Function;

// Lambda should receive only message matching the following conditions on message body:
// color: 'red' or 'orange'
// store: property must not be present
myTopic.addSubscription(new subscriptions.LambdaSubscription(fn, {
  filterPolicyWithMessageBody: {
    background: sns.FilterOrPolicy.policy({
      color: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.stringFilter({
        allowlist: ['red', 'orange'],
      })),
    }),
    store: sns.FilterOrPolicy.filter(sns.SubscriptionFilter.notExistsFilter()),
  },
}));
```

### Example of Firehose Subscription

```ts
import * as firehose from 'aws-cdk-lib/aws-kinesisfirehose';

const topic = new sns.Topic(this, 'Topic');
declare const stream: firehose.DeliveryStream;

new sns.Subscription(this, 'Subscription', {
  topic,
  endpoint: stream.deliveryStreamArn,
  protocol: sns.SubscriptionProtocol.FIREHOSE,
  subscriptionRoleArn: "SAMPLE_ARN", //role with permissions to send messages to a firehose delivery stream
});
```

## DLQ setup for SNS Subscription

CDK can attach provided Queue as DLQ for your SNS subscription.
See the [SNS DLQ configuration docs](https://docs.aws.amazon.com/sns/latest/dg/sns-configure-dead-letter-queue.html) for more information about this feature.

Example of usage with user provided DLQ.

```ts
const topic = new sns.Topic(this, 'Topic');
const dlQueue = new sqs.Queue(this, 'DeadLetterQueue', {
  queueName: 'MySubscription_DLQ',
  retentionPeriod: Duration.days(14),
});

new sns.Subscription(this, 'Subscription', {
  endpoint: 'endpoint',
  protocol: sns.SubscriptionProtocol.LAMBDA,
  topic,
  deadLetterQueue: dlQueue,
});
```

## CloudWatch Event Rule Target

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

Use the `aws-cdk-lib/aws-events-targets.SnsTopic`:

```ts
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
import * as targets from 'aws-cdk-lib/aws-events-targets';

declare const repo: codecommit.Repository;
const myTopic = new sns.Topic(this, 'Topic');

repo.onCommit('OnCommit', {
  target: new targets.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`](/v2/docs/aws-cdk-lib.aws_sns.TopicPolicy) can also be created manually.

```ts
const topic = new sns.Topic(this, 'Topic');
const topicPolicy = new sns.TopicPolicy(this, 'TopicPolicy', {
  topics: [topic],
});

topicPolicy.document.addStatements(new iam.PolicyStatement({
  actions: ["sns:Subscribe"],
  principals: [new iam.AnyPrincipal()],
  resources: [topic.topicArn],
}));
```

A policy document can also be passed on [`TopicPolicy`](/v2/docs/aws-cdk-lib.aws_sns.TopicPolicy) construction

```ts
const topic = new sns.Topic(this, 'Topic');
const policyDocument = new iam.PolicyDocument({
  assignSids: true,
  statements: [
    new iam.PolicyStatement({
      actions: ["sns:Subscribe"],
      principals: [new iam.AnyPrincipal()],
      resources: [topic.topicArn],
    }),
  ],
});

const topicPolicy = new sns.TopicPolicy(this, 'Policy', {
  topics: [topic],
  policyDocument,
});
```

A simpler and more general way of achieving the same result is to use the
[`TopicGrants`](/v2/docs/aws-cdk-lib.aws_sns.TopicGrants) class:

```ts
const topic = new sns.Topic(this, 'Topic');

// This would work the same way if topic was a CfnTopic (L1)
sns.TopicGrants.fromTopic(topic).subscribe(new iam.AnyPrincipal());
```

For convenience, if you are using an L2, you can also call `grants` on the topic:

```ts
const topic = new sns.Topic(this, 'Topic');
topic.grants.subscribe(new iam.AnyPrincipal());
```

### Enforce encryption of data in transit when publishing to a topic

You can enforce SSL when creating a topic policy by setting the `enforceSSL` flag:

```ts
const topic = new sns.Topic(this, 'Topic');
const policyDocument = new iam.PolicyDocument({
  assignSids: true,
  statements: [
    new iam.PolicyStatement({
      actions: ["sns:Publish"],
      principals: [new iam.ServicePrincipal('s3.amazonaws.com')],
      resources: [topic.topicArn],
    }),
  ],
});

const topicPolicy = new sns.TopicPolicy(this, 'Policy', {
  topics: [topic],
  policyDocument,
  enforceSSL: true,
});
```

Similiarly you can enforce SSL by setting the `enforceSSL` flag on the topic:

```ts
const topic = new sns.Topic(this, 'TopicAddPolicy', {
  enforceSSL: true,
});

topic.addToResourcePolicy(new iam.PolicyStatement({
  principals: [new iam.ServicePrincipal('s3.amazonaws.com')],
  actions: ['sns:Publish'],
  resources: [topic.topicArn],
}));
```

## Delivery status logging

Amazon SNS provides support to log the delivery status of notification messages sent to topics with the following Amazon SNS endpoints:

- HTTP
- Amazon Data Firehose
- AWS Lambda
- Platform application endpoint
- Amazon Simple Queue Service

Example with a delivery status logging configuration for SQS:

```ts
declare const role: iam.Role;
const topic = new sns.Topic(this, 'MyTopic', {
  loggingConfigs: [
    {
      protocol: sns.LoggingProtocol.SQS,
      failureFeedbackRole: role,
      successFeedbackRole: role,
      successFeedbackSampleRate: 50,
    },
  ],
});
```

A delivery status logging configuration can also be added to your topic by `addLoggingConfig` method:

```ts
declare const role: iam.Role;
const topic = new sns.Topic(this, 'MyTopic');

topic.addLoggingConfig({
  protocol: sns.LoggingProtocol.SQS,
  failureFeedbackRole: role,
  successFeedbackRole: role,
  successFeedbackSampleRate: 50,
});
```

Note that valid values for `successFeedbackSampleRate` are integer between 0-100.

## Archive Policy

Message archiving provides the ability to archive a single copy of all messages published to your topic.
You can store published messages within your topic by enabling the message archive policy on the topic, which enables message archiving for all subscriptions linked to that topic.
Messages can be archived for a minimum of one day to a maximum of 365 days.

Example with an archive policy:

```ts
const topic = new sns.Topic(this, 'MyTopic', {
  fifo: true,
  messageRetentionPeriodInDays: 7,
});
```

**Note**: The `messageRetentionPeriodInDays` property is only available for FIFO topics.

## TracingConfig

Tracing mode of an Amazon SNS topic.

If PassThrough, the topic passes trace headers received from the Amazon SNS publisher to its subscription.
If set to Active, Amazon SNS will vend X-Ray segment data to topic owner account if the sampled flag in the tracing header is true.

The default TracingConfig is `TracingConfig.PASS_THROUGH`.

Example with a tracingConfig set to Active:

```ts
const topic = new sns.Topic(this, 'MyTopic', {
  tracingConfig: sns.TracingConfig.ACTIVE,
});
```

## High-throughput mode for Amazon SNS FIFO Topics

High throughput FIFO topics in Amazon SNS efficiently manage high message throughput while maintaining strict message order, ensuring reliability and scalability for applications processing numerous messages.
This solution is ideal for scenarios demanding both high throughput and ordered message delivery.

To improve message throughput using high throughput FIFO topics, increasing the number of message groups is recommended.

For more information, see [High throughput FIFO topics in Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/fifo-high-throughput.html).

You can configure high-throughput mode for your FIFO topics by setting the `fifoThroughputScope` property:

```ts
const topic = new sns.Topic(this, 'MyTopic', {
  fifo: true,
  fifoThroughputScope: sns.FifoThroughputScope.TOPIC,
});
```

**Note**: The `fifoThroughputScope` property is only available for FIFO topics.

## Types

| Name | Kind | Summary |
| --- | --- | --- |
| [`Subscription`](/v2/docs/aws-cdk-lib.aws_sns.Subscription) | Construct (L2) | A new subscription. |
| [`Topic`](/v2/docs/aws-cdk-lib.aws_sns.Topic) | Construct (L2) | A new SNS topic. |
| [`TopicBase`](/v2/docs/aws-cdk-lib.aws_sns.TopicBase) | Construct (L2) | Either a new or imported Topic. |
| [`TopicPolicy`](/v2/docs/aws-cdk-lib.aws_sns.TopicPolicy) | Construct (L2) | The policy for an SNS Topic. |
| [`CfnSubscription`](/v2/docs/aws-cdk-lib.aws_sns.CfnSubscription) | Construct (L1) | The `AWS::SNS::Subscription` resource subscribes an endpoint to an Amazon topic. |
| [`CfnTopic`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopic) | Construct (L1) | The `AWS::SNS::Topic` resource creates a topic to which notifications can be published. |
| [`CfnTopicInlinePolicy`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopicInlinePolicy) | Construct (L1) | The `AWS::SNS::TopicInlinePolicy` resource associates one Amazon topic with one policy. |
| [`CfnTopicPolicy`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopicPolicy) | Construct (L1) | The `AWS::SNS::TopicPolicy` resource associates Amazon topics with a policy. |
| [`BackoffFunction`](/v2/docs/aws-cdk-lib.aws_sns.BackoffFunction) | Enum | Algorithms which can be used by SNS to calculate the delays associated with all of the retry attempts between the first and last retries in the backoff phase. |
| [`BetweenCondition`](/v2/docs/aws-cdk-lib.aws_sns.BetweenCondition) | Struct | Between condition for a numeric attribute. |
| [`CfnSubscriptionProps`](/v2/docs/aws-cdk-lib.aws_sns.CfnSubscriptionProps) | Struct | Properties for defining a `CfnSubscription`. |
| [`CfnTopic.LoggingConfigProperty`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopic.LoggingConfigProperty) | Struct | The `LoggingConfig` property type specifies the `Delivery` status logging configuration for an [`AWS::SNS::Topic`](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-sns-topic.html) . |
| [`CfnTopic.SubscriptionProperty`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopic.SubscriptionProperty) | Struct | `Subscription` is an embedded property that describes the subscription endpoints of an Amazon topic. |
| [`CfnTopicInlinePolicyProps`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopicInlinePolicyProps) | Struct | Properties for defining a `CfnTopicInlinePolicy`. |
| [`CfnTopicPolicyProps`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopicPolicyProps) | Struct | Properties for defining a `CfnTopicPolicy`. |
| [`CfnTopicProps`](/v2/docs/aws-cdk-lib.aws_sns.CfnTopicProps) | Struct | Properties for defining a `CfnTopic`. |
| [`DeliveryPolicy`](/v2/docs/aws-cdk-lib.aws_sns.DeliveryPolicy) | Struct | Options for customising the delivery of SNS messages to HTTP/S endpoints. |
| [`FifoThroughputScope`](/v2/docs/aws-cdk-lib.aws_sns.FifoThroughputScope) | Enum | The throughput quota and deduplication behavior to apply for the FIFO topic. |
| [`Filter`](/v2/docs/aws-cdk-lib.aws_sns.Filter) | Class | Filter implementation of FilterOrPolicy. |
| [`FilterOrPolicy`](/v2/docs/aws-cdk-lib.aws_sns.FilterOrPolicy) | Class | Class for building the FilterPolicy by avoiding union types. |
| [`FilterOrPolicyType`](/v2/docs/aws-cdk-lib.aws_sns.FilterOrPolicyType) | Enum | The type of the MessageBody at a given key value pair. |
| [`HealthyRetryPolicy`](/v2/docs/aws-cdk-lib.aws_sns.HealthyRetryPolicy) | Struct | Options for customising the retry policy of the delivery of SNS messages to HTTP/S endpoints. |
| [`ITopic`](/v2/docs/aws-cdk-lib.aws_sns.ITopic) | Interface | Represents an SNS topic. |
| [`ITopicSubscription`](/v2/docs/aws-cdk-lib.aws_sns.ITopicSubscription) | Interface | Topic subscription. |
| [`LoggingConfig`](/v2/docs/aws-cdk-lib.aws_sns.LoggingConfig) | Struct | A logging configuration for delivery status of messages sent from SNS topic to subscribed endpoints. |
| [`LoggingProtocol`](/v2/docs/aws-cdk-lib.aws_sns.LoggingProtocol) | Enum | The type of supported protocol for delivery status logging. |
| [`NumericConditions`](/v2/docs/aws-cdk-lib.aws_sns.NumericConditions) | Struct | Conditions that can be applied to numeric attributes. |
| [`Policy`](/v2/docs/aws-cdk-lib.aws_sns.Policy) | Class | Policy Implementation of FilterOrPolicy. |
| [`RequestPolicy`](/v2/docs/aws-cdk-lib.aws_sns.RequestPolicy) | Struct | Options for customising aspects of the content sent in AWS SNS HTTP/S requests. |
| [`StringConditions`](/v2/docs/aws-cdk-lib.aws_sns.StringConditions) | Struct | Conditions that can be applied to string attributes. |
| [`SubscriptionFilter`](/v2/docs/aws-cdk-lib.aws_sns.SubscriptionFilter) | Class | A subscription filter for an attribute. |
| [`SubscriptionOptions`](/v2/docs/aws-cdk-lib.aws_sns.SubscriptionOptions) | Struct | Options for creating a new subscription. |
| [`SubscriptionProps`](/v2/docs/aws-cdk-lib.aws_sns.SubscriptionProps) | Struct | Properties for creating a new subscription. |
| [`SubscriptionProtocol`](/v2/docs/aws-cdk-lib.aws_sns.SubscriptionProtocol) | Enum | The type of subscription, controlling the type of the endpoint parameter. |
| [`ThrottlePolicy`](/v2/docs/aws-cdk-lib.aws_sns.ThrottlePolicy) | Struct | Options for customising AWS SNS HTTP/S delivery throttling. |
| [`TopicAttributes`](/v2/docs/aws-cdk-lib.aws_sns.TopicAttributes) | Struct | Represents an SNS topic defined outside of this stack. |
| [`TopicGrants`](/v2/docs/aws-cdk-lib.aws_sns.TopicGrants) | Facade | Collection of grant methods for a ITopicRef. |
| [`TopicPolicyProps`](/v2/docs/aws-cdk-lib.aws_sns.TopicPolicyProps) | Struct | Properties to associate SNS topics with a policy. |
| [`TopicProps`](/v2/docs/aws-cdk-lib.aws_sns.TopicProps) | Struct | Properties for a new SNS topic. |
| [`TopicSubscriptionConfig`](/v2/docs/aws-cdk-lib.aws_sns.TopicSubscriptionConfig) | Struct | Subscription configuration. |
| [`TracingConfig`](/v2/docs/aws-cdk-lib.aws_sns.TracingConfig) | Enum | The tracing mode of an Amazon SNS topic. |
