package
1.204.0-devpreview
Repository: https://github.com/aws/aws-cdk-go.git
Documentation: pkg.go.dev

# README

Amazon Simple Notification Service Construct Library

Add an SNS Topic to your stack:

topic := sns.NewTopic(this, jsii.String("Topic"), &TopicProps{
	DisplayName: jsii.String("Customer subscription topic"),
})

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

topic := sns.NewTopic(this, jsii.String("Topic"), &TopicProps{
	ContentBasedDeduplication: jsii.Boolean(true),
	DisplayName: jsii.String("Customer subscription topic"),
	Fifo: jsii.Boolean(true),
	TopicName: jsii.String("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:

myTopic := sns.NewTopic(this, jsii.String("MyTopic"))

myTopic.AddSubscription(subscriptions.NewUrlSubscription(jsii.String("https://foobar.com/")))

Subscribe a queue to the topic:

var queue queue

myTopic := sns.NewTopic(this, jsii.String("MyTopic"))

myTopic.AddSubscription(subscriptions.NewSqsSubscription(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 lambda "github.com/aws/aws-cdk-go/awscdk"
var fn function


myTopic := sns.NewTopic(this, jsii.String("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(subscriptions.NewLambdaSubscription(fn, &LambdaSubscriptionProps{
	FilterPolicy: map[string]subscriptionFilter{
		"color": sns.*subscriptionFilter_stringFilter(&StringConditions{
			"allowlist": []*string{
				jsii.String("red"),
				jsii.String("orange"),
			},
			"matchPrefixes": []*string{
				jsii.String("bl"),
			},
		}),
		"size": sns.*subscriptionFilter_stringFilter(&StringConditions{
			"denylist": []*string{
				jsii.String("small"),
				jsii.String("medium"),
			},
		}),
		"price": sns.*subscriptionFilter_numericFilter(&NumericConditions{
			"between": &BetweenCondition{
				"start": jsii.Number(100),
				"stop": jsii.Number(200),
			},
			"greaterThan": jsii.Number(300),
		}),
		"store": sns.*subscriptionFilter_existsFilter(),
	},
}))

Example of Firehose Subscription

import "github.com/aws/aws-cdk-go/awscdk"
var stream deliveryStream


topic := sns.NewTopic(this, jsii.String("Topic"))

sns.NewSubscription(this, jsii.String("Subscription"), &SubscriptionProps{
	Topic: Topic,
	Endpoint: stream.DeliveryStreamArn,
	Protocol: sns.SubscriptionProtocol_FIREHOSE,
	SubscriptionRoleArn: jsii.String("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.

topic := sns.NewTopic(this, jsii.String("Topic"))
dlQueue := sqs.NewQueue(this, jsii.String("DeadLetterQueue"), &QueueProps{
	QueueName: jsii.String("MySubscription_DLQ"),
	RetentionPeriod: awscdk.Duration_Days(jsii.Number(14)),
})

sns.NewSubscription(this, jsii.String("Subscription"), &SubscriptionProps{
	Endpoint: jsii.String("endpoint"),
	Protocol: sns.SubscriptionProtocol_LAMBDA,
	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:

import codecommit "github.com/aws/aws-cdk-go/awscdk"
import targets "github.com/aws/aws-cdk-go/awscdk"

var repo repository

myTopic := sns.NewTopic(this, jsii.String("Topic"))

repo.onCommit(jsii.String("OnCommit"), &OnCommitOptions{
	Target: targets.NewSnsTopic(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.

topic := sns.NewTopic(this, jsii.String("Topic"))
topicPolicy := sns.NewTopicPolicy(this, jsii.String("TopicPolicy"), &TopicPolicyProps{
	Topics: []iTopic{
		topic,
	},
})

topicPolicy.Document.AddStatements(iam.NewPolicyStatement(&PolicyStatementProps{
	Actions: []*string{
		jsii.String("sns:Subscribe"),
	},
	Principals: []iPrincipal{
		iam.NewAnyPrincipal(),
	},
	Resources: []*string{
		topic.TopicArn,
	},
}))

A policy document can also be passed on TopicPolicy construction

topic := sns.NewTopic(this, jsii.String("Topic"))
policyDocument := iam.NewPolicyDocument(&PolicyDocumentProps{
	AssignSids: jsii.Boolean(true),
	Statements: []policyStatement{
		iam.NewPolicyStatement(&PolicyStatementProps{
			Actions: []*string{
				jsii.String("sns:Subscribe"),
			},
			Principals: []iPrincipal{
				iam.NewAnyPrincipal(),
			},
			Resources: []*string{
				topic.TopicArn,
			},
		}),
	},
})

topicPolicy := sns.NewTopicPolicy(this, jsii.String("Policy"), &TopicPolicyProps{
	Topics: []iTopic{
		topic,
	},
	PolicyDocument: PolicyDocument,
})

# Functions

No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
No description provided by the author
Returns `true` if a construct is a stack element (i.e.
Check whether the given construct is a CfnResource.
Return whether the given object is a Construct.
Create a new `AWS::SNS::Subscription`.
Create a new `AWS::SNS::Subscription`.
Create a new `AWS::SNS::Topic`.
Create a new `AWS::SNS::Topic`.
Create a new `AWS::SNS::TopicPolicy`.
Create a new `AWS::SNS::TopicPolicy`.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Experimental.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Returns a subscription filter for attribute key matching.
Returns a subscription filter for a numeric attribute.
Returns a subscription filter for a string attribute.
Import an existing SNS topic provided an ARN.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.
Return whether the given object is a Construct.
Check whether the given construct is a Resource.

# Constants

JSON-encoded notifications are sent to a mobile app endpoint.
Notifications are sent via email.
Notifications are JSON-encoded and sent via mail.
Notifications put records into a firehose delivery stream.
JSON-encoded message is POSTED to an HTTP url.
JSON-encoded message is POSTed to an HTTPS url.
Notifications trigger a Lambda function.
Notification is delivered by SMS.
Notifications are enqueued into an SQS queue.

# Structs

Between condition for a numeric attribute.
Properties for defining a `CfnSubscription`.
`Subscription` is an embedded property that describes the subscription endpoints of an Amazon SNS topic.
Properties for defining a `CfnTopicPolicy`.
Properties for defining a `CfnTopic`.
Conditions that can be applied to numeric attributes.
Conditions that can be applied to string attributes.
Options for creating a new subscription.
Properties for creating a new subscription.
Properties to associate SNS topics with a policy.
Properties for a new SNS topic.
Subscription configuration.

# Interfaces

A CloudFormation `AWS::SNS::Subscription`.
A CloudFormation `AWS::SNS::Topic`.
A CloudFormation `AWS::SNS::TopicPolicy`.
Represents an SNS topic.
Topic subscription.
A new subscription.
A subscription filter for an attribute.
A new SNS topic.
Either a new or imported Topic.
The policy for an SNS Topic.

# Type aliases

The type of subscription, controlling the type of the endpoint parameter.