aws-cdk-lib.aws_sns.Topic

class Topic (construct)

LanguageType name
.NETAmazon.CDK.AWS.SNS.Topic
Gogithub.com/aws/aws-cdk-go/awscdk/v2/awssns#Topic
Javasoftware.amazon.awscdk.services.sns.Topic
Pythonaws_cdk.aws_sns.Topic
TypeScript (source)aws-cdk-lib » aws_sns » Topic

Implements IConstruct, IDependable, IResource, ITopic, INotificationRuleTarget

A new SNS topic.

Example

import * as sns from 'aws-cdk-lib/aws-sns';

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

const topicRule = new iot.TopicRule(this, 'TopicRule', {
  sql: iot.IotSql.fromStringAsVer20160323(
    "SELECT topic(2) as device_id, year, month, day FROM 'device/+/data'",
  ),
  actions: [
    new actions.SnsTopicAction(topic, {
      messageFormat: actions.SnsActionMessageFormat.JSON, // optional property, default is SnsActionMessageFormat.RAW
    }),
  ],
});

Initializer

new Topic(scope: Construct, id: string, props?: TopicProps)

Parameters

  • scope Construct
  • id string
  • props TopicProps

Construct Props

NameTypeDescription
contentBasedDeduplication?booleanEnables content-based deduplication for FIFO topics.
displayName?stringA developer-defined string that can be used to identify this SNS topic.
fifo?booleanSet to true to create a FIFO topic.
masterKey?IKeyA KMS Key, either managed by this CDK app, or imported.
topicName?stringA name for the topic.

contentBasedDeduplication?

Type: boolean (optional, default: None)

Enables content-based deduplication for FIFO topics.


displayName?

Type: string (optional, default: None)

A developer-defined string that can be used to identify this SNS topic.


fifo?

Type: boolean (optional, default: None)

Set to true to create a FIFO topic.


masterKey?

Type: IKey (optional, default: None)

A KMS Key, either managed by this CDK app, or imported.


topicName?

Type: string (optional, default: Generated name)

A name for the topic.

If you don't specify a name, AWS CloudFormation generates a unique physical ID and uses that ID for the topic name. For more information, see Name Type.

Properties

NameTypeDescription
autoCreatePolicybooleanControls automatic creation of policy objects.
envResourceEnvironmentThe environment this resource belongs to.
fifobooleanWhether this topic is an Amazon SNS FIFO queue.
nodeNodeThe tree node.
stackStackThe stack in which this resource is defined.
topicArnstringThe ARN of the topic.
topicNamestringThe name of the topic.

autoCreatePolicy

Type: boolean

Controls automatic creation of policy objects.

Set by subclasses.


env

Type: ResourceEnvironment

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.


fifo

Type: boolean

Whether this topic is an Amazon SNS FIFO queue.

If false, this is a standard topic.


node

Type: Node

The tree node.


stack

Type: Stack

The stack in which this resource is defined.


topicArn

Type: string

The ARN of the topic.


topicName

Type: string

The name of the topic.

Methods

NameDescription
addSubscription(topicSubscription)Subscribe some endpoint to this topic.
addToResourcePolicy(statement)Adds a statement to the IAM resource policy associated with this topic.
applyRemovalPolicy(policy)Apply the given removal policy to this resource.
bindAsNotificationRuleTarget(_scope)Represents a notification target That allows SNS topic to associate with this rule target.
grantPublish(grantee)Grant topic publishing permissions to the given identity.
metric(metricName, props?)Return the given named metric for this Topic.
metricNumberOfMessagesPublished(props?)The number of messages published to your Amazon SNS topics.
metricNumberOfNotificationsDelivered(props?)The number of messages successfully delivered from your Amazon SNS topics to subscribing endpoints.
metricNumberOfNotificationsFailed(props?)The number of messages that Amazon SNS failed to deliver.
metricNumberOfNotificationsFilteredOut(props?)The number of messages that were rejected by subscription filter policies.
metricNumberOfNotificationsFilteredOutInvalidAttributes(props?)The number of messages that were rejected by subscription filter policies because the messages' attributes are invalid.
metricNumberOfNotificationsFilteredOutNoMessageAttributes(props?)The number of messages that were rejected by subscription filter policies because the messages have no attributes.
metricPublishSize(props?)Metric for the size of messages published through this topic.
metricSMSMonthToDateSpentUSD(props?)The charges you have accrued since the start of the current calendar month for sending SMS messages.
metricSMSSuccessRate(props?)The rate of successful SMS message deliveries.
toString()Returns a string representation of this construct.
static fromTopicArn(scope, id, topicArn)Import an existing SNS topic provided an ARN.

addSubscription(topicSubscription)

public addSubscription(topicSubscription: ITopicSubscription): Subscription

Parameters

  • topicSubscription ITopicSubscription

Returns

  • Subscription

Subscribe some endpoint to this topic.


addToResourcePolicy(statement)

public addToResourcePolicy(statement: PolicyStatement): AddToResourcePolicyResult

Parameters

  • statement PolicyStatement

Returns

  • AddToResourcePolicyResult

Adds a statement to the IAM resource policy associated with this topic.

If this topic was created in this stack (new Topic), a topic policy will be automatically created upon the first call to addToPolicy. If the topic is imported (Topic.import), then this is a no-op.


applyRemovalPolicy(policy)

public applyRemovalPolicy(policy: RemovalPolicy): void

Parameters

  • policy RemovalPolicy

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you've removed it from the CDK application or because you've made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).


bindAsNotificationRuleTarget(_scope)

public bindAsNotificationRuleTarget(_scope: Construct): NotificationRuleTargetConfig

Parameters

  • _scope Construct

Returns

  • NotificationRuleTargetConfig

Represents a notification target That allows SNS topic to associate with this rule target.


grantPublish(grantee)

public grantPublish(grantee: IGrantable): Grant

Parameters

  • grantee IGrantable

Returns

  • Grant

Grant topic publishing permissions to the given identity.


metric(metricName, props?)

public metric(metricName: string, props?: MetricOptions): Metric

Parameters

  • metricName string
  • props MetricOptions

Returns

  • Metric

Return the given named metric for this Topic.


metricNumberOfMessagesPublished(props?)

public metricNumberOfMessagesPublished(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages published to your Amazon SNS topics.

Sum over 5 minutes


metricNumberOfNotificationsDelivered(props?)

public metricNumberOfNotificationsDelivered(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages successfully delivered from your Amazon SNS topics to subscribing endpoints.

Sum over 5 minutes


metricNumberOfNotificationsFailed(props?)

public metricNumberOfNotificationsFailed(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages that Amazon SNS failed to deliver.

Sum over 5 minutes


metricNumberOfNotificationsFilteredOut(props?)

public metricNumberOfNotificationsFilteredOut(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages that were rejected by subscription filter policies.

Sum over 5 minutes


metricNumberOfNotificationsFilteredOutInvalidAttributes(props?)

public metricNumberOfNotificationsFilteredOutInvalidAttributes(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages that were rejected by subscription filter policies because the messages' attributes are invalid.

Sum over 5 minutes


metricNumberOfNotificationsFilteredOutNoMessageAttributes(props?)

public metricNumberOfNotificationsFilteredOutNoMessageAttributes(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The number of messages that were rejected by subscription filter policies because the messages have no attributes.

Sum over 5 minutes


metricPublishSize(props?)

public metricPublishSize(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

Metric for the size of messages published through this topic.

Average over 5 minutes


metricSMSMonthToDateSpentUSD(props?)

public metricSMSMonthToDateSpentUSD(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The charges you have accrued since the start of the current calendar month for sending SMS messages.

Maximum over 5 minutes


metricSMSSuccessRate(props?)

public metricSMSSuccessRate(props?: MetricOptions): Metric

Parameters

  • props MetricOptions

Returns

  • Metric

The rate of successful SMS message deliveries.

Sum over 5 minutes


toString()

public toString(): string

Returns

  • string

Returns a string representation of this construct.


static fromTopicArn(scope, id, topicArn)

public static fromTopicArn(scope: Construct, id: string, topicArn: string): ITopic

Parameters

  • scope Construct — The parent creating construct.
  • id string — The construct's name.
  • topicArn string — topic ARN (i.e. arn:aws:sns:us-east-2:444455556666:MyTopic).

Returns

  • ITopic

Import an existing SNS topic provided an ARN.