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

sns.tf#L11
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
localstack.tf#L44
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
sns_subscription.tf#L1
resource "aws_sns_topic_subscription" "ServiceA_Event" {
  topic_arn = "ServiceA_Event"
  protocol  = "sqs"
  endpoint  = ServiceD
}

Review your Terraform file for AWS best practices

Shisho Cloud, our free checker to make sure your Terraform configuration follows best practices, is available (beta).

Parameters

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 the aws_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, the aws_sns_topic_subscription must use the AWS provider for the account with the SQS queue. If aws_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 every apply. 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 use email, email-json, or http/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 you destroy 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.