@@ -13,8 +13,53 @@ locals {
1313 " stitching_dlq" = " ${ terraform . workspace } -deadletter-stitching-queue"
1414 " mns_dlq" = " ${ terraform . workspace } -deadletter-mns-notification-queue"
1515 }
16- days_until_alarm = 6
16+ days_until_alarm = [6 ,10 ]
17+ #
18+ # monitored_queue_day_pairs = {
19+ # for pair in flatten([
20+ # for queue_key in keys(local.monitored_queues) : [
21+ # for day in local.days_until_alarm : {
22+ # key = "${queue_key}_${day}"
23+ # value = {
24+ # queue_key = queue_key
25+ # queue_name = local.monitored_queues[queue_key]
26+ # days = day
27+ # }
28+ # }
29+ # ]
30+ # ]) : pair.key => pair.value
31+ # }
32+ # }
33+ # using map
34+ # monitored_queue_day_set = toset(flatten([
35+ # for queue_key in keys(local.monitored_queues) : [
36+ # for day in local.days_until_alarm :
37+ # "${queue_key}:::${local.monitored_queues[queue_key]}:::${day}"
38+ # ]
39+ # ]))
40+ # monitored_queue_day_map = {
41+ # for s in local.monitored_queue_day_set :
42+ # s => {
43+ # queue_key = split(":::", s)[0]
44+ # queue_name = split(":::", s)[1]
45+ # days = tonumber(split(":::", s)[2])
46+ # }
47+ # }
48+
49+ # using a list instead of map
50+
51+ monitored_queue_day_list = flatten ([
52+ for queue_key in keys (local. monitored_queues ) : [
53+ for day in local . days_until_alarm : [
54+ queue_key ,
55+ local . monitored_queues [queue_key ],
56+ day
57+ ]
58+ ]
59+ ])
1760}
61+
62+
1863locals {
1964 is_test_sandbox = contains ([], terraform. workspace ) # empty list disables sandbox detection, for testing only
2065}
@@ -50,28 +95,39 @@ module "global_sqs_age_alarm_topic" {
5095 })
5196}
5297
98+
5399resource "aws_cloudwatch_metric_alarm" "sqs_oldest_message" {
54- for_each = local. is_test_sandbox ? {} : local. monitored_queues # TODO:change is_test_sandbox to is_sandbox
100+ # for_each = local.is_test_sandbox ? {} : local.monitored_queue_day_pairs # TODO:change is_test_sandbox to is_sandbox
101+ # for_each = local.is_test_sandbox ? toset([]) : local.monitored_queue_day_set # TODO:change is_test_sandbox to is_sandbox
102+ # for_each = local.is_test_sandbox ? {} : local.monitored_queue_day_map # TODO:change is_test_sandbox to is_sandbox
103+
104+ count = local. is_test_sandbox ? 0 : length (local. monitored_queue_day_list )# TODO:change is_test_sandbox to is_sandbox
105+
106+ # alarm_name = "${terraform.workspace}_${each.value.queue_key}_oldest_message_alarm_${each.value.days}d"
107+ alarm_name = " ${ terraform . workspace } _${ local . monitored_queue_day_list [count . index ][0 ]} _oldest_message_alarm_${ local . monitored_queue_day_list [count . index ][2 ]} d"
55108
56- alarm_name = " ${ terraform . workspace } _${ each . key } _oldest_message_alarm"
57109 comparison_operator = " GreaterThanThreshold"
58110 evaluation_periods = 1
59111 metric_name = " ApproximateAgeOfOldestMessage"
60112 namespace = " AWS/SQS"
61113 period = 60 # TODO: change to 86400 (24h))
62114 statistic = " Maximum"
63- threshold = 60 # TODO: change to local.days_until_alarm*24*60*60
115+ # threshold = each.value.days # TODO: change to each.value.days*24*60*60
116+ threshold = local. monitored_queue_day_list [count . index ][2 ] # TODO: change to each.value.days*24*60*60
64117 treat_missing_data = " notBreaching"
65118
66119 dimensions = {
67- QueueName = each.value
120+ # QueueName = each.value.queue_name
121+ QueueName = local.monitored_queue_day_list[count.index][1 ]
68122 }
69123
70- alarm_description = " Alarm when a message in queue '${ each . value } ' is older than '${ local . days_until_alarm } ' days."
124+ # alarm_description = "Alarm when a message in queue '${each.value.queue_name}' is older than '${each.value.days}' days."
125+ alarm_description = " Alarm when a message in queue '${ local . monitored_queue_day_list [count . index ][1 ]} ' is older than '${ local . monitored_queue_day_list [count . index ][2 ]} ' days."
71126 alarm_actions = [module . global_sqs_age_alarm_topic [0 ]. arn ]
72127
73128 tags = {
74- Name = " ${ terraform . workspace } _${ each . key } _oldest_message_alarm"
129+ # Name = "${terraform.workspace}_${each.value.queue_key}_oldest_message_alarm_${each.value.days}d"
130+ Name = " ${ terraform . workspace } _${ local . monitored_queue_day_list [count . index ][0 ]} _oldest_message_alarm_${ local . monitored_queue_day_list [count . index ][2 ]} d"
75131 Owner = var.owner
76132 Environment = var.environment
77133 Workspace = terraform.workspace
0 commit comments