Home > Mobile >  Conditional attribue for Dimensions of Cloudwatch Alarm with Terraform
Conditional attribue for Dimensions of Cloudwatch Alarm with Terraform

Time:12-21

HI I am creating a common module for cloudwatch alarm that can be used by other services. The dimensions has to be dynamic in this case and it has to accept with no dimensions as well. How do I make it conditional attribute so that it gets executed only when the value is not null. My current code is below.

resource "aws_cloudwatch_metric_alarm" "cloudwatch_metric_alarm" {
  alarm_name                = var.alarm_name
  comparison_operator       = var.comparison_operator
  evaluation_periods        = var.evaluation_periods
  alarm_description         = var.alarm_description
  insufficient_data_actions = var.insufficient_data_actions
  alarm_actions             = var.alarm_actions
  metric_name               = var.metric_name
  namespace                 = var.namespace
  period                    = var.period
  statistic                 = var.statistic
  threshold                 = var.threshold
  datapoints_to_alarm       = var.datapoints_to_alarm
  dimensions = {
    "${var.dimensions_name}" = "${var.dimensions_value}"
  }
  tags = var.tags
}

CodePudding user response:

Assuming that everything else is correct, you can use null:

  dimensions = (var.dimensions_value != null ? {
    "${var.dimensions_name}" = "${var.dimensions_value}"
  } : null)
  • Related