github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/website/source/docs/providers/aws/r/sns_topic_subscription.html.markdown (about) 1 --- 2 layout: "aws" 3 page_title: "AWS: sns_topic_subscription" 4 sidebar_current: "docs-aws-resource-sns-topic-subscription" 5 description: |- 6 Provides a resource for subscribing to SNS topics. 7 --- 8 9 # aws\_sns\_topic\_subscription 10 11 Provides a resource for subscribing to SNS topics. Requires that an SNS topic exist for the subscription to attach to. 12 This resource allows you to automatically place messages sent to SNS topics in SQS queues, send them as HTTP(S) POST requests 13 to a given endpoint, send SMS messages, or notify devices / applications. The most likely use case for Terraform users will 14 probably be SQS queues. 15 16 ~> **NOTE:** If SNS topic and SQS queue are in different AWS regions it is important to place the "aws_sns_topic_subscription" into the terraform configuration of the region with the SQS queue. If "aws_sns_topic_subscription" is placed in the terraform configuration of the region with the SNS topic terraform will fail to create the subscription. 17 18 ~> **NOTE:** Setup of cross-account subscriptions from SNS topics to SQS queues requires Terraform to have access to BOTH accounts. 19 20 ~> **NOTE:** If SNS topic and SQS queue are in different AWS accounts but the same region it is important to place the "aws_sns_topic_subscription" into the terraform configuration of the account with the SQS queue. If "aws_sns_topic_subscription" is placed in the terraform configuration of the account with the SNS topic terraform creates the subscriptions but does not keep state and tries to re-create the subscription at every apply. 21 22 ~> **NOTE:** If SNS topic and SQS queue are in different AWS accounts and different AWS regions it is important to recognize that the subscription needs to be initiated from the account with the SQS queue but in the region of the SNS topic. 23 24 ## Example Usage 25 26 You can directly supply a topic and ARN by hand in the `topic_arn` property along with the queue ARN: 27 28 ```hcl 29 resource "aws_sns_topic_subscription" "user_updates_sqs_target" { 30 topic_arn = "arn:aws:sns:us-west-2:432981146916:user-updates-topic" 31 protocol = "sqs" 32 endpoint = "arn:aws:sqs:us-west-2:432981146916:terraform-queue-too" 33 } 34 ``` 35 36 Alternatively you can use the ARN properties of a managed SNS topic and SQS queue: 37 38 ```hcl 39 resource "aws_sns_topic" "user_updates" { 40 name = "user-updates-topic" 41 } 42 43 resource "aws_sqs_queue" "user_updates_queue" { 44 name = "user-updates-queue" 45 } 46 47 resource "aws_sns_topic_subscription" "user_updates_sqs_target" { 48 topic_arn = "${aws_sns_topic.user_updates.arn}" 49 protocol = "sqs" 50 endpoint = "${aws_sqs_queue.user_updates_queue.arn}" 51 } 52 ``` 53 54 You can subscribe SNS topics to SQS queues in different Amazon accounts and regions: 55 56 ```hcl 57 /* 58 # 59 # Variables 60 # 61 */ 62 variable "sns" { 63 default = { 64 account-id = "111111111111" 65 role-name = "service/service-hashicorp-terraform" 66 name = "example-sns-topic" 67 display_name = "example" 68 region = "us-west-1" 69 } 70 } 71 72 variable "sqs" { 73 default = { 74 account-id = "222222222222" 75 role-name = "service/service-hashicorp-terraform" 76 name = "example-sqs-queue" 77 region = "us-east-1" 78 } 79 } 80 81 data "aws_iam_policy_document" "sns-topic-policy" { 82 policy_id = "__default_policy_ID" 83 84 statement { 85 actions = [ 86 "SNS:Subscribe", 87 "SNS:SetTopicAttributes", 88 "SNS:RemovePermission", 89 "SNS:Receive", 90 "SNS:Publish", 91 "SNS:ListSubscriptionsByTopic", 92 "SNS:GetTopicAttributes", 93 "SNS:DeleteTopic", 94 "SNS:AddPermission", 95 ] 96 97 condition { 98 test = "StringEquals" 99 variable = "AWS:SourceOwner" 100 101 values = [ 102 "${var.sns["account-id"]}", 103 ] 104 } 105 106 effect = "Allow" 107 108 principals { 109 type = "AWS" 110 identifiers = ["*"] 111 } 112 113 resources = [ 114 "arn:aws:sns:${var.sns["region"]}:${var.sns["account-id"]}:${var.sns["name"]}", 115 ] 116 117 sid = "__default_statement_ID" 118 } 119 120 statement { 121 actions = [ 122 "SNS:Subscribe", 123 "SNS:Receive", 124 ] 125 126 condition { 127 test = "StringLike" 128 variable = "SNS:Endpoint" 129 130 values = [ 131 "arn:aws:sqs:${var.sqs["region"]}:${var.sqs["account-id"]}:${var.sqs["name"]}", 132 ] 133 } 134 135 effect = "Allow" 136 137 principals { 138 type = "AWS" 139 identifiers = ["*"] 140 } 141 142 resources = [ 143 "arn:aws:sns:${var.sns["region"]}:${var.sns["account-id"]}:${var.sns["name"]}", 144 ] 145 146 sid = "__console_sub_0" 147 } 148 } 149 150 data "aws_iam_policy_document" "sqs-queue-policy" { 151 policy_id = "arn:aws:sqs:${var.sqs["region"]}:${var.sqs["account-id"]}:${var.sqs["name"]}/SQSDefaultPolicy" 152 153 statement { 154 sid = "example-sns-topic" 155 effect = "Allow" 156 157 principals { 158 type = "AWS" 159 identifiers = ["*"] 160 } 161 162 actions = [ 163 "SQS:SendMessage", 164 ] 165 166 resources = [ 167 "arn:aws:sqs:${var.sqs["region"]}:${var.sqs["account-id"]}:${var.sqs["name"]}", 168 ] 169 170 condition { 171 test = "ArnEquals" 172 variable = "aws:SourceArn" 173 174 values = [ 175 "arn:aws:sns:${var.sns["region"]}:${var.sns["account-id"]}:${var.sns["name"]}", 176 ] 177 } 178 } 179 } 180 181 # provider to manage SNS topics 182 provider "aws" { 183 alias = "sns" 184 region = "${var.sns["region"]}" 185 186 assume_role { 187 role_arn = "arn:aws:iam::${var.sns["account-id"]}:role/${var.sns["role-name"]}" 188 session_name = "sns-${var.sns["region"]}" 189 } 190 } 191 192 # provider to manage SQS queues 193 provider "aws" { 194 alias = "sqs" 195 region = "${var.sqs["region"]}" 196 197 assume_role { 198 role_arn = "arn:aws:iam::${var.sqs["account-id"]}:role/${var.sqs["role-name"]}" 199 session_name = "sqs-${var.sqs["region"]}" 200 } 201 } 202 203 # provider to subscribe SQS to SNS (using the SQS account but the SNS region) 204 provider "aws" { 205 alias = "sns2sqs" 206 region = "${var.sns["region"]}" 207 208 assume_role { 209 role_arn = "arn:aws:iam::${var.sqs["account-id"]}:role/${var.sqs["role-name"]}" 210 session_name = "sns2sqs-${var.sns["region"]}" 211 } 212 } 213 214 resource "aws_sns_topic" "sns-topic" { 215 provider = "aws.sns" 216 name = "${var.sns["name"]}" 217 display_name = "${var.sns["display_name"]}" 218 policy = "${data.aws_iam_policy_document.sns-topic-policy.json}" 219 } 220 221 resource "aws_sqs_queue" "sqs-queue" { 222 provider = "aws.sqs" 223 name = "${var.sqs["name"]}" 224 policy = "${data.aws_iam_policy_document.sqs-queue-policy.json}" 225 } 226 227 resource "aws_sns_topic_subscription" "sns-topic" { 228 provider = "aws.sns2sqs" 229 topic_arn = "${aws_sns_topic.sns-topic.arn}" 230 protocol = "sqs" 231 endpoint = "${aws_sqs_queue.sqs-queue.arn}" 232 } 233 ``` 234 235 ## Argument Reference 236 237 The following arguments are supported: 238 239 * `topic_arn` - (Required) The ARN of the SNS topic to subscribe to 240 * `protocol` - (Required) The protocol to use. The possible values for this are: `sqs`, `lambda`, `application`. (`http` or `https` are partially supported, see below) (`email`, `sms`, are options but unsupported, see below). 241 * `endpoint` - (Required) The endpoint to send data to, the contents will vary with the protocol. (see below for more information) 242 * `endpoint_auto_confirms` - (Optional) Boolean indicating whether the end point is capable of [auto confirming subscription](http://docs.aws.amazon.com/sns/latest/dg/SendMessageToHttp.html#SendMessageToHttp.prepare) e.g., PagerDuty (default is false) 243 * `confirmation_timeout_in_minutes` - (Optional) Integer indicating number of minutes to wait in retying mode for fetching subscription arn before marking it as failure. Only applicable for http and https protocols (default is 1 minute). 244 * `raw_message_delivery` - (Optional) Boolean indicating whether or not to enable raw message delivery (the original message is directly passed, not wrapped in JSON with the original message in the message property). 245 246 ### Protocols supported 247 248 Supported SNS protocols include: 249 250 * `lambda` -- delivery of JSON-encoded message to a lambda function 251 * `sqs` -- delivery of JSON-encoded message to an Amazon SQS queue 252 * `application` -- delivery of JSON-encoded message to an EndpointArn for a mobile app and device 253 254 Partially supported SNS protocols include: 255 256 * `http` -- delivery of JSON-encoded messages via HTTP. Supported only for the end points that auto confirms the subscription. 257 * `https` -- delivery of JSON-encoded messages via HTTPS. Supported only for the end points that auto confirms the subscription. 258 259 Unsupported protocols include the following: 260 261 * `email` -- delivery of message via SMTP 262 * `email-json` -- delivery of JSON-encoded message via SMTP 263 * `sms` -- delivery text message 264 265 These are unsupported because the endpoint needs to be authorized and does not 266 generate an ARN until the target email address has been validated. This breaks 267 the Terraform model and as a result are not currently supported. 268 269 ### Specifying endpoints 270 271 Endpoints have different format requirements according to the protocol that is chosen. 272 273 * SQS endpoints come in the form of the SQS queue's ARN (not the URL of the queue) e.g: `arn:aws:sqs:us-west-2:432981146916:terraform-queue-too` 274 * Application endpoints are also the endpoint ARN for the mobile app and device. 275 276 ## Attributes Reference 277 278 The following attributes are exported: 279 280 * `id` - The ARN of the subscription 281 * `topic_arn` - The ARN of the topic the subscription belongs to 282 * `protocol` - The protocol being used 283 * `endpoint` - The full endpoint to send data to (SQS ARN, HTTP(S) URL, Application ARN, SMS number, etc.) 284 * `arn` - The ARN of the subscription stored as a more user-friendly property 285 286 ## Import 287 288 SNS Topic Subscriptions can be imported using the `subscription arn`, e.g. 289 290 ``` 291 $ terraform import aws_sns_topic_subscription.user_updates_sqs_target arn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f 292 ```