Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to enable IF condition on terrafrom modules

I need to create TF CloudWatch Metrices only of the env is QAT and PROD. Currently using TF modules to create those in all env.

module "aws_cloudwatch_log_metric_filter" {
  source = "https://github.com/modules.git//aws-cloudwatch-log-metric-filter"
  log_group_name = "/aws/lambda/${var.lambda_name}"
  pattern = "{$.message = \"---------------- Message ----------------\"}"
}

locals {
  base_tags = {
    environment      = var.environment
  }
}

Main resource where I am calling module.

resource "aws_cloudwatch_log_metric_filter" "log_metric" {
  count          = var.count
  name           = "Metric"
  pattern        = var.pattern
  log_group_name = var.log_group_name

  metric_transformation {
    name      = "name"
    namespace = "namespace"
    value     = "1"
    default_value = "0"
  }
}

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You could do this in the module:

resource "aws_cloudwatch_log_metric_filter" "log_metric" {
  count          = var.environment == "QAT" || var.environment == "PROD" ? 1 : 0
  name           = "Metric"
  pattern        = var.pattern
  log_group_name = "${var.environment}-${var.log_group_name}"

  metric_transformation {
    name      = "name"
    namespace = "namespace"
    value     = "1"
    default_value = "0"
  }
}

The log_group_name = "${var.environment}-${var.log_group_name}" will ensure you don’t get issues with the same log group name.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading