如何在Terraform模块上启用IF条件

huangapple go评论63阅读模式
英文:

How to enable IF condition on terrafrom modules

问题

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

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"
  }
}
英文:

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"
  }
}

答案1

得分: 1

以下是翻译好的部分:

A simple but good solution:

    locals {
      cw_env = ["QUA", "PROD"]
    }

    resource "aws_cloudwatch_log_metric_filter" "log_metric" {
      count          = contains(local.cw_env, var.env) ? 1 : 0
      name           = "Metric";
      pattern        = var.pattern
      log_group_name = var.log_group_name

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

Note you can also use it at the above level:

    module "aws_cloudwatch_log_metric_filter" {
      source = "https://github.com/modules.git//aws-cloudwatch-log-metric-filter"

      count          = contains(local.cw_env, local.base_tags.environment) ? 1 : 0
      log_group_name = "/aws/lambda/${var.lambda_name}";
      pattern = "{$.message = \"---------------- Message ----------------\"}";
    }

    locals {
      base_tags = {
        environment      = var.environment
      }
      cw_env = ["QUA", "PROD"]
    }

希望这对您有所帮助。

英文:

A simple but good solution :

locals {
  cw_env = ["QUA", "PROD"]
}

resource "aws_cloudwatch_log_metric_filter" "log_metric" {
  count          = contains(local.cw_env, var.env) ? 1 : 0
  name           = "Metric"
  pattern        = var.pattern
  log_group_name = var.log_group_name

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

Note you can also use it at the above level :

module "aws_cloudwatch_log_metric_filter" {
  source = "https://github.com/modules.git//aws-cloudwatch-log-metric-filter"

  count          = contains(local.cw_env, local.base_tags.environment) ? 1 : 0
  log_group_name = "/aws/lambda/${var.lambda_name}"
  pattern = "{$.message = \"---------------- Message ----------------\"}"
}

locals {
  base_tags = {
    environment      = var.environment
  }
  cw_env = ["QUA", "PROD"]
}

答案2

得分: 1

你可以在模块中执行以下操作:

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"
  }
}

log_group_name = "${var.environment}-${var.log_group_name}" 确保您不会遇到相同的日志组名称问题。

英文:

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.

huangapple
  • 本文由 发表于 2023年2月14日 00:16:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75438502.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定