From 1e8a10faa5dfcb03552c4d69465d9c127807620c Mon Sep 17 00:00:00 2001 From: Charles Bushong Date: Wed, 29 Apr 2020 10:20:48 -0400 Subject: [PATCH] #46 Remove data resource for sns topic to avoid race condition Data Resources are evaluated before any code is executed, so if a single terraform apply intends to create an SNS topic _and_ create this module, the apply will fail because the data resource cannot resolve. Avoiding the use of data resources by deriving the SNS manually corrects the issue in this case. --- main.tf | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/main.tf b/main.tf index 258ce92a..7b7b200b 100644 --- a/main.tf +++ b/main.tf @@ -1,8 +1,5 @@ -data "aws_sns_topic" "this" { - count = false == var.create_sns_topic && var.create ? 1 : 0 - - name = var.sns_topic_name -} +data "aws_caller_identity" "current" {} +data "aws_region" "current" {} resource "aws_sns_topic" "this" { count = var.create_sns_topic && var.create ? 1 : 0 @@ -15,9 +12,8 @@ resource "aws_sns_topic" "this" { locals { sns_topic_arn = element( concat( - aws_sns_topic.this.*.arn, - data.aws_sns_topic.this.*.arn, - [""], + aws_sns_topic.this.*.id, + ["arn:aws:sns:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sns_topic_name}"], ), 0, )