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

Commit bba4822

Browse files
feat: support config via tags (#1)
1 parent e9abb00 commit bba4822

File tree

3 files changed

+105
-30
lines changed

3 files changed

+105
-30
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,22 @@ terraform init
2323
terraform apply
2424
```
2525

26+
## Config via tags
27+
28+
You can also configure this module by tagging the SQS queue (requires v1.0.0 or higher). Tags take precedence over variables (tags override variables).
29+
30+
| tag key | default value | allowed values |
31+
| ------------------------------------------------------------------ | -------------------------------------------------------------------- | ----------------------------------------------|
32+
| `marbot` | on | on,off |
33+
| `marbot:approximate-age-of-oldest-message` | variable `approximate_age_of_oldest_message` | static,off |
34+
| `marbot:approximate-age-of-oldest-message:threshold` | variable `approximate_age_of_oldest_message_threshold` | >= 0 |
35+
| `marbot:approximate-age-of-oldest-message:period` | variable `approximate_age_of_oldest_message_period` | <= 86400 and multiple of 60 |
36+
| `marbot:approximate-age-of-oldest-message:evaluation-periods` | variable `approximate_age_of_oldest_message_evaluation_periods` | >= 1 and $period*$evaluation-periods <= 86400 |
37+
| `marbot:approximate-number-of-messages-visible` | variable `approximate_number_of_messages_visible` | static,off |
38+
| `marbot:approximate-number-of-messages-visible:threshold` | variable `approximate_number_of_messages_visible_threshold` | >= 0 |
39+
| `marbot:approximate-number-of-messages-visible:period` | variable `approximate_number_of_messages_visible_period` | <= 86400 and multiple of 60 |
40+
| `marbot:approximate-number-of-messages-visible:evaluation-periods` | variable `approximate_number_of_messages_visible_evaluation_periods` | >= 1 and $period*$evaluation-periods <= 86400 |
41+
2642
## Update procedure
2743

2844
1. Update the `version`

main.tf

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,27 @@ data "aws_caller_identity" "current" {}
1212

1313
data "aws_region" "current" {}
1414

15+
data "aws_sqs_queue" "queue" {
16+
name = var.queue_name
17+
}
18+
1519
locals {
1620
topic_arn = var.create_topic == false ? var.topic_arn : join("", aws_sns_topic.marbot.*.arn)
21+
enabled = var.enabled && lookup(data.aws_sqs_queue.queue.tags, "marbot", "on") != "off"
22+
23+
approximate_age_of_oldest_message = lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message", var.approximate_age_of_oldest_message)
24+
approximate_age_of_oldest_message_threshold = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message:threshold", var.approximate_age_of_oldest_message_threshold)), var.approximate_age_of_oldest_message_threshold)
25+
approximate_age_of_oldest_message_period_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message:period", var.approximate_age_of_oldest_message_period)), var.approximate_age_of_oldest_message_period)
26+
approximate_age_of_oldest_message_period = min(max(floor(local.approximate_age_of_oldest_message_period_raw / 60) * 60, 60), 86400)
27+
approximate_age_of_oldest_message_evaluation_periods_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-age-of-oldest-message:evaluation-periods", var.approximate_age_of_oldest_message_evaluation_periods)), var.approximate_age_of_oldest_message_evaluation_periods)
28+
approximate_age_of_oldest_message_evaluation_periods = min(max(local.approximate_age_of_oldest_message_evaluation_periods_raw, 1), floor(86400 / local.approximate_age_of_oldest_message_period))
29+
30+
approximate_number_of_messages_visible = lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible", var.approximate_number_of_messages_visible)
31+
approximate_number_of_messages_visible_threshold = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible:threshold", var.approximate_number_of_messages_visible_threshold)), var.approximate_number_of_messages_visible_threshold)
32+
approximate_number_of_messages_visible_period_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible:period", var.approximate_number_of_messages_visible_period)), var.approximate_number_of_messages_visible_period)
33+
approximate_number_of_messages_visible_period = min(max(floor(local.approximate_number_of_messages_visible_period_raw / 60) * 60, 60), 86400)
34+
approximate_number_of_messages_visible_evaluation_periods_raw = try(tonumber(lookup(data.aws_sqs_queue.queue.tags, "marbot:approximate-number-of-messages-visible:evaluation-periods", var.approximate_number_of_messages_visible_evaluation_periods)), var.approximate_number_of_messages_visible_evaluation_periods)
35+
approximate_number_of_messages_visible_evaluation_periods = min(max(local.approximate_number_of_messages_visible_evaluation_periods_raw, 1), floor(86400 / local.approximate_number_of_messages_visible_period))
1736
}
1837

1938
##########################################################################
@@ -23,14 +42,14 @@ locals {
2342
##########################################################################
2443

2544
resource "aws_sns_topic" "marbot" {
26-
count = (var.create_topic && var.enabled) ? 1 : 0
45+
count = (var.create_topic && local.enabled) ? 1 : 0
2746

2847
name_prefix = "marbot"
2948
tags = var.tags
3049
}
3150

3251
resource "aws_sns_topic_policy" "marbot" {
33-
count = (var.create_topic && var.enabled) ? 1 : 0
52+
count = (var.create_topic && local.enabled) ? 1 : 0
3453

3554
arn = join("", aws_sns_topic.marbot.*.arn)
3655
policy = data.aws_iam_policy_document.topic_policy.json
@@ -72,7 +91,7 @@ data "aws_iam_policy_document" "topic_policy" {
7291

7392
resource "aws_sns_topic_subscription" "marbot" {
7493
depends_on = [aws_sns_topic_policy.marbot]
75-
count = (var.create_topic && var.enabled) ? 1 : 0
94+
count = (var.create_topic && local.enabled) ? 1 : 0
7695

7796
topic_arn = join("", aws_sns_topic.marbot.*.arn)
7897
protocol = "https"
@@ -98,7 +117,7 @@ JSON
98117

99118
resource "aws_cloudwatch_event_rule" "monitoring_jump_start_connection" {
100119
depends_on = [aws_sns_topic_subscription.marbot]
101-
count = (var.module_version_monitoring_enabled && var.enabled) ? 1 : 0
120+
count = (var.module_version_monitoring_enabled && local.enabled) ? 1 : 0
102121

103122
name = "marbot-sqs-queue-connection-${random_id.id8.hex}"
104123
description = "Monitoring Jump Start connection (created by marbot)"
@@ -107,7 +126,7 @@ resource "aws_cloudwatch_event_rule" "monitoring_jump_start_connection" {
107126
}
108127

109128
resource "aws_cloudwatch_event_target" "monitoring_jump_start_connection" {
110-
count = (var.module_version_monitoring_enabled && var.enabled) ? 1 : 0
129+
count = (var.module_version_monitoring_enabled && local.enabled) ? 1 : 0
111130

112131
rule = join("", aws_cloudwatch_event_rule.monitoring_jump_start_connection.*.name)
113132
target_id = "marbot"
@@ -116,7 +135,7 @@ resource "aws_cloudwatch_event_target" "monitoring_jump_start_connection" {
116135
{
117136
"Type": "monitoring-jump-start-tf-connection",
118137
"Module": "sqs-queue",
119-
"Version": "0.7.0",
138+
"Version": "1.0.0",
120139
"Partition": "${data.aws_partition.current.partition}",
121140
"AccountId": "${data.aws_caller_identity.current.account_id}",
122141
"Region": "${data.aws_region.current.name}"
@@ -138,17 +157,17 @@ resource "random_id" "id8" {
138157

139158
resource "aws_cloudwatch_metric_alarm" "approximate_age_of_oldest_message" {
140159
depends_on = [aws_sns_topic_subscription.marbot]
141-
count = (var.approximate_age_of_oldest_message_threshold >= 0 && var.enabled) ? 1 : 0
160+
count = (local.approximate_age_of_oldest_message == "static" && local.enabled) ? 1 : 0
142161

143162
alarm_name = "marbot-sqs-queue-message-age-${random_id.id8.hex}"
144163
alarm_description = "Queue contains old messages. Is message processing failing or is the message procesing capacity too low? (created by marbot)"
145164
namespace = "AWS/SQS"
146165
metric_name = "ApproximateAgeOfOldestMessage"
147166
statistic = "Maximum"
148-
period = 60
149-
evaluation_periods = 1
167+
period = local.approximate_age_of_oldest_message_period
168+
evaluation_periods = local.approximate_age_of_oldest_message_evaluation_periods
150169
comparison_operator = "GreaterThanThreshold"
151-
threshold = var.approximate_age_of_oldest_message_threshold
170+
threshold = local.approximate_age_of_oldest_message_threshold
152171
alarm_actions = [local.topic_arn]
153172
ok_actions = [local.topic_arn]
154173
dimensions = {
@@ -162,17 +181,17 @@ resource "aws_cloudwatch_metric_alarm" "approximate_age_of_oldest_message" {
162181

163182
resource "aws_cloudwatch_metric_alarm" "approximate_number_of_messages_visible" {
164183
depends_on = [aws_sns_topic_subscription.marbot]
165-
count = (var.approximate_number_of_messages_visible_threshold >= 0 && var.enabled) ? 1 : 0
184+
count = (local.approximate_number_of_messages_visible == "static" && local.enabled) ? 1 : 0
166185

167186
alarm_name = "marbot-sqs-queue-length-${random_id.id8.hex}"
168187
alarm_description = "Queue contains too many messages. Is message processing failing or is the message procesing capacity too low? (created by marbot)"
169188
namespace = "AWS/SQS"
170189
metric_name = "ApproximateNumberOfMessagesVisible"
171190
statistic = "Maximum"
172-
period = 60
173-
evaluation_periods = 1
191+
period = local.approximate_number_of_messages_visible_period
192+
evaluation_periods = local.approximate_number_of_messages_visible_evaluation_periods
174193
comparison_operator = "GreaterThanThreshold"
175-
threshold = var.approximate_number_of_messages_visible_threshold
194+
threshold = local.approximate_number_of_messages_visible_threshold
176195
alarm_actions = [local.topic_arn]
177196
ok_actions = [local.topic_arn]
178197
dimensions = {

variables.tf

Lines changed: 56 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
1+
# 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.
2+
variable "create_topic" {
3+
type = bool
4+
description = "Create SNS topic? If set to false you must set topic_arn as well!"
5+
default = true
6+
}
7+
8+
variable "topic_arn" {
9+
type = string
10+
description = "Optional SNS topic ARN if create_topic := false (usually the output of the modules marbot-monitoring-basic or marbot-standalone-topic)."
11+
default = ""
12+
}
13+
14+
variable "stage" {
15+
type = string
16+
description = "marbot stage (never change this!)."
17+
default = "v1"
18+
}
19+
120
variable "endpoint_id" {
221
type = string
322
description = "Your marbot endpoint ID (to get this value: select a channel where marbot belongs to and send a message like this: \"@marbot show me my endpoint id\")."
@@ -26,33 +45,54 @@ variable "queue_name" {
2645
description = "The SQS queue name that you want to monitor."
2746
}
2847

48+
49+
50+
variable "approximate_age_of_oldest_message" {
51+
type = string
52+
description = "Old messages (static|off)."
53+
default = "static"
54+
}
55+
2956
variable "approximate_age_of_oldest_message_threshold" {
3057
type = number
31-
description = "The maximum age (in seconds) of a message in the queue (set to -1 to disable)."
58+
description = "The maximum age (in seconds) of a message in the queue (>= 0)."
3259
default = 600
3360
}
3461

35-
variable "approximate_number_of_messages_visible_threshold" {
62+
variable "approximate_age_of_oldest_message_period" {
3663
type = number
37-
description = "The maximum number of messages in the queue waiting for processing (set to -1 to disable)"
38-
default = 10
64+
description = "The period in seconds over which the specified statistic is applied (<= 86400 and multiple of 60)."
65+
default = 60
3966
}
4067

41-
# 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.
42-
variable "create_topic" {
43-
type = bool
44-
description = "Create SNS topic? If set to false you must set topic_arn as well!"
45-
default = true
68+
variable "approximate_age_of_oldest_message_evaluation_periods" {
69+
type = number
70+
description = "The number of periods over which data is compared to the specified threshold (>= 1 and $period*$evaluation_periods <= 86400)."
71+
default = 1
4672
}
4773

48-
variable "topic_arn" {
74+
75+
76+
variable "approximate_number_of_messages_visible" {
4977
type = string
50-
description = "Optional SNS topic ARN if create_topic := false (usually the output of the modules marbot-monitoring-basic or marbot-standalone-topic)."
51-
default = ""
78+
description = "Waiting messages (static|off)."
79+
default = "static"
5280
}
5381

54-
variable "stage" {
55-
type = string
56-
description = "marbot stage (never change this!)."
57-
default = "v1"
82+
variable "approximate_number_of_messages_visible_threshold" {
83+
type = number
84+
description = "The maximum number of messages in the queue waiting for processing (>= 0)."
85+
default = 10
86+
}
87+
88+
variable "approximate_number_of_messages_visible_period" {
89+
type = number
90+
description = "The period in seconds over which the specified statistic is applied (<= 86400 and multiple of 60)."
91+
default = 60
92+
}
93+
94+
variable "approximate_number_of_messages_visible_evaluation_periods" {
95+
type = number
96+
description = "The number of periods over which data is compared to the specified threshold (>= 1 and $period*$evaluation_periods <= 86400)."
97+
default = 1
5898
}

0 commit comments

Comments
 (0)