Skip to content
This repository was archived by the owner on Sep 12, 2023. It is now read-only.

Commit 7ca6f08

Browse files
committed
feat: support external SNS topic
1 parent 3a4c2de commit 7ca6f08

File tree

2 files changed

+26
-9
lines changed

2 files changed

+26
-9
lines changed

main.tf

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,25 @@ data "aws_caller_identity" "current" {}
1212

1313
data "aws_region" "current" {}
1414

15+
locals {
16+
topic_arn = var.create_topic == false ? var.topic_arn : join("", aws_sns_topic.marbot.*.arn)
17+
}
18+
1519
##########################################################################
1620
# #
1721
# TOPIC #
1822
# #
1923
##########################################################################
2024

2125
resource "aws_sns_topic" "marbot" {
22-
count = var.enabled ? 1 : 0
26+
count = (var.create_topic && var.enabled) ? 1 : 0
2327

2428
name_prefix = "marbot"
2529
tags = var.tags
2630
}
2731

2832
resource "aws_sns_topic_policy" "marbot" {
29-
count = var.enabled ? 1 : 0
33+
count = (var.create_topic && var.enabled) ? 1 : 0
3034

3135
arn = join("", aws_sns_topic.marbot.*.arn)
3236
policy = data.aws_iam_policy_document.topic_policy.json
@@ -68,7 +72,7 @@ data "aws_iam_policy_document" "topic_policy" {
6872

6973
resource "aws_sns_topic_subscription" "marbot" {
7074
depends_on = [aws_sns_topic_policy.marbot]
71-
count = var.enabled ? 1 : 0
75+
count = (var.create_topic && var.enabled) ? 1 : 0
7276

7377
topic_arn = join("", aws_sns_topic.marbot.*.arn)
7478
protocol = "https"
@@ -107,12 +111,12 @@ resource "aws_cloudwatch_event_target" "monitoring_jump_start_connection" {
107111

108112
rule = join("", aws_cloudwatch_event_rule.monitoring_jump_start_connection.*.name)
109113
target_id = "marbot"
110-
arn = join("", aws_sns_topic.marbot.*.arn)
114+
arn = local.topic_arn
111115
input = <<JSON
112116
{
113117
"Type": "monitoring-jump-start-tf-connection",
114118
"Module": "sqs-queue",
115-
"Version": "0.5.3",
119+
"Version": "0.6.0",
116120
"Partition": "${data.aws_partition.current.partition}",
117121
"AccountId": "${data.aws_caller_identity.current.account_id}",
118122
"Region": "${data.aws_region.current.name}"
@@ -145,8 +149,8 @@ resource "aws_cloudwatch_metric_alarm" "approximate_age_of_oldest_message" {
145149
evaluation_periods = 1
146150
comparison_operator = "GreaterThanThreshold"
147151
threshold = var.approximate_age_of_oldest_message_threshold
148-
alarm_actions = [join("", aws_sns_topic.marbot.*.arn)]
149-
ok_actions = [join("", aws_sns_topic.marbot.*.arn)]
152+
alarm_actions = [local.topic_arn]
153+
ok_actions = [local.topic_arn]
150154
dimensions = {
151155
QueueName = var.queue_name
152156
}
@@ -169,8 +173,8 @@ resource "aws_cloudwatch_metric_alarm" "approximate_number_of_messages_visible"
169173
evaluation_periods = 1
170174
comparison_operator = "GreaterThanThreshold"
171175
threshold = var.approximate_number_of_messages_visible_threshold
172-
alarm_actions = [join("", aws_sns_topic.marbot.*.arn)]
173-
ok_actions = [join("", aws_sns_topic.marbot.*.arn)]
176+
alarm_actions = [local.topic_arn]
177+
ok_actions = [local.topic_arn]
174178
dimensions = {
175179
QueueName = var.queue_name
176180
}

variables.tf

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ variable "approximate_number_of_messages_visible_threshold" {
3232
default = 10
3333
}
3434

35+
# We can not only check the var.topic_arn !="" because of the Terraform error: The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created.
36+
variable "create_topic" {
37+
type = bool
38+
description = "Create SNS topic? If set to false you must set topic_arn as well!"
39+
default = true
40+
}
41+
42+
variable "topic_arn" {
43+
type = string
44+
description = "Optional SNS topic ARN if create_topic := false (usually the output of the modules marbot-monitoring-basic or marbot-standalone-topic)."
45+
default = ""
46+
}
47+
3548
variable "stage" {
3649
type = string
3750
description = "marbot stage (never change this!)."

0 commit comments

Comments
 (0)