Skip to content

Commit 2c4f15b

Browse files
authored
fix: Remove data resource for sns topic to avoid race condition (#81)
1 parent 3374836 commit 2c4f15b

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ To run the tests:
104104

105105
| Name |
106106
|------|
107+
| [aws_caller_identity](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) |
107108
| [aws_cloudwatch_log_group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) |
108109
| [aws_iam_policy_document](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) |
109-
| [aws_sns_topic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/sns_topic) |
110+
| [aws_region](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/region) |
110111
| [aws_sns_topic](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic) |
111112
| [aws_sns_topic_subscription](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/sns_topic_subscription) |
112113

main.tf

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
data "aws_sns_topic" "this" {
2-
count = false == var.create_sns_topic && var.create ? 1 : 0
3-
4-
name = var.sns_topic_name
5-
}
1+
data "aws_caller_identity" "current" {}
2+
data "aws_region" "current" {}
63

74
resource "aws_sns_topic" "this" {
85
count = var.create_sns_topic && var.create ? 1 : 0
@@ -15,7 +12,14 @@ resource "aws_sns_topic" "this" {
1512
}
1613

1714
locals {
18-
sns_topic_arn = element(concat(aws_sns_topic.this.*.arn, data.aws_sns_topic.this.*.arn, [""]), 0)
15+
sns_topic_arn = element(
16+
concat(
17+
aws_sns_topic.this.*.arn,
18+
["arn:aws:sns:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sns_topic_name}"],
19+
[""]
20+
),
21+
0,
22+
)
1923

2024
lambda_policy_document = {
2125
sid = "AllowWriteToCloudwatchLogs"

0 commit comments

Comments
 (0)