1
votes

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
}
1
Sorry, but it's not clear what you want to do. What is wrong with your current code? Any errors?Marcin
@Marcin my code is not wrong. The problem is that I cannot keep one same value for the variable "threshold" as my each alarm has different value. So I want my code to be changed in a way that the variable or terraform picks up the value of each alarm (whats in AWS) automatically.mgb
Sadly you can't do this without custom external resource, as terraform does not provide data sources for cloudwatch alarms.Marcin
@Marcin ohh ty! cant I use a way for "lookup" or something? I am not sure thoughmgb

1 Answers

2
votes

Assuming that my understanding of your issue is correct, you can't reference existing AWS alarms in terraform as it does not provide data sources for alarms. The only sources provided by TF are aws_cloudwatch_log_group and aws_cloudwatch_event_source. To overcome that you would have to develop your own External Data Source using, e.g. AWS CLI in bash, as exemplified in the TF docs linked.

However, if these alarms are outputs of your child module, you can reference them in the parent module. For that you would have to modify your child module to output the threshold values for each of the alarms. Then, in the parent module you could simply get these output values and use them for other purposes, e.g. by passing them to other child module.