AWS Amazon SNS Topic Subscription
This page shows how to write Terraform and CloudFormation for Amazon SNS Topic Subscription and write them securely.
aws_sns_topic_subscription (Terraform)
The Topic Subscription in Amazon SNS can be configured in Terraform with the resource name aws_sns_topic_subscription
. The following sections describe 3 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_sns_topic_subscription" "us_east_1" {
count = contains(var.enabled_regions, "us-east-1") ? 1 : 0
topic_arn = aws_sns_topic.us_east_1[0].arn
protocol = "sqs"
endpoint = aws_sqs_queue.this.arn
resource "aws_sns_topic_subscription" "ecom-events-payments-target" {
topic_arn = aws_sns_topic.sns-ecom-events.arn
protocol = "sqs"
endpoint = aws_sqs_queue.extraction-payments-events.arn
filter_policy = <<EOF
resource "aws_sns_topic_subscription" "ServiceA_Event" {
topic_arn = "ServiceA_Event"
protocol = "sqs"
endpoint = ServiceD
}
Parameters
-
arn
optional computed - string -
confirmation_timeout_in_minutes
optional - number -
confirmation_was_authenticated
optional computed - bool -
delivery_policy
optional - string -
endpoint
required - string -
endpoint_auto_confirms
optional - bool -
filter_policy
optional - string -
id
optional computed - string -
owner_id
optional computed - string -
pending_confirmation
optional computed - bool -
protocol
required - string -
raw_message_delivery
optional - bool -
redrive_policy
optional - string -
subscription_role_arn
optional - string -
topic_arn
required - string
Explanation in Terraform Registry
Provides a resource for subscribing to SNS topics. Requires that an SNS topic exist for the subscription to attach to. This resource allows you to automatically place messages sent to SNS topics in SQS queues, send them as HTTP(S) POST requests to a given endpoint, send SMS messages, or notify devices / applications. The most likely use case for Terraform users will probably be SQS queues.
NOTE: If the SNS topic and SQS queue are in different AWS regions, the
aws_sns_topic_subscription
must use an AWS provider that is in the same region as the SNS topic. If theaws_sns_topic_subscription
uses a provider with a different region than the SNS topic, Terraform will fail to create the subscription. NOTE: Setup of cross-account subscriptions from SNS topics to SQS queues requires Terraform to have access to BOTH accounts. NOTE: If an SNS topic and SQS queue are in different AWS accounts but the same region, theaws_sns_topic_subscription
must use the AWS provider for the account with the SQS queue. Ifaws_sns_topic_subscription
uses a Provider with a different account than the SQS queue, Terraform creates the subscription but does not keep state and tries to re-create the subscription at everyapply
. NOTE: If an SNS topic and SQS queue are in different AWS accounts and different AWS regions, the subscription needs to be initiated from the account with the SQS queue but in the region of the SNS topic. NOTE: You cannot unsubscribe to a subscription that is pending confirmation. If you useemail-json
, orhttp
/https
(without auto-confirmation enabled), until the subscription is confirmed (e.g., outside of Terraform), AWS does not allow Terraform to delete / unsubscribe the subscription. If youdestroy
an unconfirmed subscription, Terraform will remove the subscription from its state but the subscription will still exist in AWS. However, if you delete an SNS topic, SNS deletes all the subscriptions associated with the topic. Also, you can import a subscription after confirmation and then have the capability to delete it.
AWS::SNS::Topic Subscription (CloudFormation)
The Topic Subscription in SNS can be configured in CloudFormation with the resource name AWS::SNS::Topic Subscription
. The following sections describe how to use the resource and its parameters.
Example Usage from GitHub
An example could not be found in GitHub.
Parameters
Endpoint
The endpoint that receives notifications from the Amazon SNS topic. The endpoint value depends on the protocol that you specify. For more information, see the Endpoint
parameter of the [Subscribe](https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html)
action in the Amazon SNS API Reference.
Required: Yes
Type: String
Update requires: Replacement
Protocol
The subscription's protocol. For more information, see the Protocol
parameter of the [Subscribe](https://docs.aws.amazon.com/sns/latest/api/API_Subscribe.html)
action in the Amazon SNS API Reference.
Required: Yes
Type: String
Update requires: Replacement
Explanation in CloudFormation Registry
Subscription
is an embedded property that describes the subscription endpoints of an Amazon SNS topic.Note For full control over subscription behavior (for example, delivery policy, filtering, raw message delivery, and cross-region subscriptions), use the AWS::SNS::Subscription resource.
Frequently asked questions
What is AWS Amazon SNS Topic Subscription?
AWS Amazon SNS Topic Subscription is a resource for Amazon SNS of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS Amazon SNS Topic Subscription?
For Terraform, the rhythmictech/terraform-aws-config-multiregion, lindenlab/example-terraform-localstack and karrybit/ayatori source code examples are useful. See the Terraform Example section for further details.