github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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 ``` 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 ``` 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 You can subscribe SNS topics to SQS queues in different Amazon accounts and regions: 54 55 ``` 56 /* 57 # 58 # Variables 59 # 60 */ 61 variable "sns" { 62 default = { 63 account-id = "111111111111" 64 role-name = "service/service-hashicorp-terraform" 65 name = "example-sns-topic" 66 display_name = "example" 67 region = "us-west-1" 68 } 69 } 70 71 variable "sqs" { 72 default = { 73 account-id = "222222222222" 74 role-name = "service/service-hashicorp-terraform" 75 name = "example-sqs-queue" 76 region = "us-east-1" 77 } 78 } 79 80 data "aws_iam_policy_document" "sns-topic-policy" { 81 policy_id = "__default_policy_ID" 82 83 statement { 84 actions = [ 85 "SNS:Subscribe", 86 "SNS:SetTopicAttributes", 87 "SNS:RemovePermission", 88 "SNS:Receive", 89 "SNS:Publish", 90 "SNS:ListSubscriptionsByTopic", 91 "SNS:GetTopicAttributes", 92 "SNS:DeleteTopic", 93 "SNS:AddPermission", 94 ] 95 96 condition { 97 test = "StringEquals" 98 variable = "AWS:SourceOwner" 99 100 values = [ 101 "${var.sns["account-id"]}", 102 ] 103 } 104 105 effect = "Allow" 106 107 principals { 108 type = "AWS" 109 identifiers = ["*"] 110 } 111 112 resources = [ 113 "arn:aws:sns:${var.sns["region"]}:${var.sns["account-id"]}:${var.sns["name"]}", 114 ] 115 116 sid = "__default_statement_ID" 117 } 118 119 statement { 120 actions = [ 121 "SNS:Subscribe", 122 "SNS:Receive", 123 ] 124 125 condition { 126 test = "StringLike" 127 variable = "SNS:Endpoint" 128 129 values = [ 130 "arn:aws:sqs:${var.sqs["region"]}:${var.sqs["account-id"]}:${var.sqs["name"]}", 131 ] 132 } 133 134 effect = "Allow" 135 136 principals { 137 type = "AWS" 138 identifiers = ["*"] 139 } 140 141 resources = [ 142 "arn:aws:sns:${var.sns["region"]}:${var.sns["account-id"]}:${var.sns["name"]}", 143 ] 144 145 sid = "__console_sub_0" 146 } 147 } 148 149 data "aws_iam_policy_document" "sqs-queue-policy" { 150 policy_id = "arn:aws:sqs:${var.sqs["region"]}:${var.sqs["account-id"]}:${var.sqs["name"]}/SQSDefaultPolicy" 151 152 statement { 153 sid = "example-sns-topic" 154 effect = "Allow" 155 156 principals { 157 type = "AWS" 158 identifiers = ["*"] 159 } 160 161 actions = [ 162 "SQS:SendMessage", 163 ] 164 165 resources = [ 166 "arn:aws:sqs:${var.sqs["region"]}:${var.sqs["account-id"]}:${var.sqs["name"]}", 167 ] 168 169 condition { 170 test = "ArnEquals" 171 variable = "aws:SourceArn" 172 173 values = [ 174 "arn:aws:sns:${var.sns["region"]}:${var.sns["account-id"]}:${var.sns["name"]}", 175 ] 176 } 177 } 178 } 179 180 # provider to manage SNS topics 181 provider "aws" { 182 alias = "sns" 183 region = "${var.sns["region"]}" 184 185 assume_role { 186 role_arn = "arn:aws:iam::${var.sns["account-id"]}:role/${var.sns["role-name"]}" 187 session_name = "sns-${var.sns["region"]}" 188 } 189 } 190 191 # provider to manage SQS queues 192 provider "aws" { 193 alias = "sqs" 194 region = "${var.sqs["region"]}" 195 196 assume_role { 197 role_arn = "arn:aws:iam::${var.sqs["account-id"]}:role/${var.sqs["role-name"]}" 198 session_name = "sqs-${var.sqs["region"]}" 199 } 200 } 201 202 # provider to subscribe SQS to SNS (using the SQS account but the SNS region) 203 provider "aws" { 204 alias = "sns2sqs" 205 region = "${var.sns["region"]}" 206 207 assume_role { 208 role_arn = "arn:aws:iam::${var.sqs["account-id"]}:role/${var.sqs["role-name"]}" 209 session_name = "sns2sqs-${var.sns["region"]}" 210 } 211 } 212 213 resource "aws_sns_topic" "sns-topic" { 214 provider = "aws.sns" 215 name = "${var.sns["name"]}" 216 display_name = "${var.sns["display_name"]}" 217 policy = "${data.aws_iam_policy_document.sns-topic-policy.json}" 218 } 219 220 resource "aws_sqs_queue" "sqs-queue" { 221 provider = "aws.sqs" 222 name = "${var.sqs["name"]}" 223 policy = "${data.aws_iam_policy_document.sqs-queue-policy.json}" 224 } 225 226 resource "aws_sns_topic_subscription" "sns-topic" { 227 provider = "aws.sns2sqs" 228 topic_arn = "${aws_sns_topic.sns-topic.arn}" 229 protocol = "sqs" 230 endpoint = "${aws_sqs_queue.sqs-queue.arn}" 231 } 232 ``` 233 234 ## Argument Reference 235 236 The following arguments are supported: 237 238 * `topic_arn` - (Required) The ARN of the SNS topic to subscribe to 239 * `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). 240 * `endpoint` - (Required) The endpoint to send data to, the contents will vary with the protocol. (see below for more information) 241 * `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) 242 * `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). 243 * `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). 244 245 ### Protocols supported 246 247 Supported SNS protocols include: 248 249 * `lambda` -- delivery of JSON-encoded message to a lambda function 250 * `sqs` -- delivery of JSON-encoded message to an Amazon SQS queue 251 * `application` -- delivery of JSON-encoded message to an EndpointArn for a mobile app and device 252 253 Partially supported SNS protocols include: 254 255 * `http` -- delivery of JSON-encoded messages via HTTP. Supported only for the end points that auto confirms the subscription. 256 * `https` -- delivery of JSON-encoded messages via HTTPS. Supported only for the end points that auto confirms the subscription. 257 258 Unsupported protocols include the following: 259 260 * `email` -- delivery of message via SMTP 261 * `email-json` -- delivery of JSON-encoded message via SMTP 262 * `sms` -- delivery text message 263 264 These are unsupported because the endpoint needs to be authorized and does not 265 generate an ARN until the target email address has been validated. This breaks 266 the Terraform model and as a result are not currently supported. 267 268 ### Specifying endpoints 269 270 Endpoints have different format requirements according to the protocol that is chosen. 271 272 * 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` 273 * Application endpoints are also the endpoint ARN for the mobile app and device. 274 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 287 ## Import 288 289 SNS Topic Subscriptions can be imported using the `subscription arn`, e.g. 290 291 ``` 292 $ terraform import aws_sns_topic_subscription.user_updates_sqs_target arn:aws:sns:us-west-2:0123456789012:my-topic:8a21d249-4329-4871-acc6-7be709c6ea7f 293 ```