Skip to content

Commit a0ea3ba

Browse files
committed
Fix cloudwatch alarm fail (#56)
Signed-off-by: Varun Palekar <[email protected]>
1 parent aff272d commit a0ea3ba

File tree

1 file changed

+7
-6
lines changed

1 file changed

+7
-6
lines changed

main.tf

+7-6
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ resource "aws_security_group_rule" "ingress_cidr_blocks" {
5454

5555
locals {
5656
elasticache_subnet_group_name = var.elasticache_subnet_group_name != "" ? var.elasticache_subnet_group_name : join("", aws_elasticache_subnet_group.default.*.name)
57+
elasticache_member_clusters = tolist(aws_elasticache_replication_group.default.member_clusters)
5758
}
5859

5960
resource "aws_elasticache_subnet_group" "default" {
@@ -117,8 +118,8 @@ resource "aws_elasticache_replication_group" "default" {
117118
# CloudWatch Resources
118119
#
119120
resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
120-
count = var.enabled ? 1 : 0
121-
alarm_name = "${module.label.id}-cpu-utilization"
121+
count = var.enabled ? length(local.elasticache_member_clusters) : 0
122+
alarm_name = "${element(local.elasticache_member_clusters, count.index)}-cpu-utilization"
122123
alarm_description = "Redis cluster CPU utilization"
123124
comparison_operator = "GreaterThanThreshold"
124125
evaluation_periods = "1"
@@ -130,7 +131,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
130131
threshold = var.alarm_cpu_threshold_percent
131132

132133
dimensions = {
133-
CacheClusterId = module.label.id
134+
CacheClusterId = element(local.elasticache_member_clusters, count.index)
134135
}
135136

136137
alarm_actions = var.alarm_actions
@@ -139,8 +140,8 @@ resource "aws_cloudwatch_metric_alarm" "cache_cpu" {
139140
}
140141

141142
resource "aws_cloudwatch_metric_alarm" "cache_memory" {
142-
count = var.enabled ? 1 : 0
143-
alarm_name = "${module.label.id}-freeable-memory"
143+
count = var.enabled ? length(local.elasticache_member_clusters) : 0
144+
alarm_name = "${element(local.elasticache_member_clusters, count.index)}-freeable-memory"
144145
alarm_description = "Redis cluster freeable memory"
145146
comparison_operator = "LessThanThreshold"
146147
evaluation_periods = "1"
@@ -152,7 +153,7 @@ resource "aws_cloudwatch_metric_alarm" "cache_memory" {
152153
threshold = var.alarm_memory_threshold_bytes
153154

154155
dimensions = {
155-
CacheClusterId = module.label.id
156+
CacheClusterId = element(local.elasticache_member_clusters, count.index)
156157
}
157158

158159
alarm_actions = var.alarm_actions

0 commit comments

Comments
 (0)