So I have a main RDS Module, which references another module called "cloudwatch_alarms".
Now, under that ONE RDS Module ... I have around 6 cloudwatch alarms which all reference that ONE "cloudwatch_alarms" module.
The problem I am facing is: now each alarm has its own configuration. For example, alarmA has a threshold of 15000 and alarmB has a threshold of 25000. I want my code to be that it picks up the "threshold" value of each alarm itself - as each value for each alarm is different - I cannot hardcode it in the variable.tf.
I want terraform to put the value itself.
I use this same module in 4 different environments and each alarm in every environment has different "threshold" values .. so I cannot keep ONE value for it. So I want my code to be changed in a way that the variable - terraform picks up the value of each alarm (from AWS) automatically.
Is there a way to do that?
My modules are as below:
part of main.tf module RDS:
...
...
module "alarmA" {
source = "[email protected]:cloudwatch_alarms"
alarm_description = "xxx"
alarm_name = "${var.name}-alarmA"
comparison_operator = "LessThanOrEqualToThreshold"
evaluation_periods = 4
namespace = "AWS/RDS"
notification_topic = [var.notification_topic]
period = 6
threshold = 15000
}
module "alarmB" {
source = "[email protected]:cloudwatch_alarms"
alarm_description = "xxxx"
alarm_name = "${var.name}-alarmB"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = 5
namespace = "AWS/RDS"
notification_topic = [var.notification_topic]
period = 7
threshold = 25000
}
part of main.tf of module cloudwatch_alarms:
resource "aws_cloudwatch_metric_alarm" "alarm" {
count = var.alarm_count
alarm_description = var.alarm_description
alarm_name = var.alarm_count > 1 ? format("%v-%03d", local.alarm_name, count.index + 1) : local.alarm_name
alarm_actions = []
comparison_operator = var.comparison_operator
datapoints_to_alarm = var.datapoints_to_alarm
dimensions = var.dimensions[count.index]
evaluation_periods = var.evaluation_periods
metric_name = var.metric_name
namespace = var.namespace
period = var.period
statistic = var.statistic
threshold = var.threshold
unit = var.unit
variable.tf of cloudwatch_alwarms:
variable "threshold" {
description = "The value against which the specified statistic is compared."
type = string
}