繁体   English   中英

如何遍历 terraform 中的 map 变量

[英]How can I iterate through a map variable in terraform

我正在尝试遍历变量类型 map,但我不确定如何

这是我到目前为止所拥有的

在我的 main.tf 中:

resource "aws_route_53_record" "proxy_dns" {
  count = "${length(var.account_name)}"
  zone_id = "${infrastructure.zone_id}"
  name = "proxy-${element(split(",", var.account_name), count.index)}-dns
  type = CNAME
  ttl = 60
  records = ["{records.dns_name}"]
}

在我的 variables.tf 中

variable "account_name" {
  type = "map"
  default = {
    "account1" = "accountA"
    "account2" = "accountB"
  }
}

我希望能够使用不同的帐户名创建多个资源

如果您使用 Terraform 0.12.6 或更高版本,则可以使用for_each而不是count为地图中的每个元素生成一个实例:

resource "aws_route53_record" "proxy_dns" {
  for_each = var.account_name

  zone_id = infrastructure.zone_id
  name    = "proxy-${each.value}-dns"
  # ... etc ...
}

for_each over count的主要优点是 Terraform 将通过地图中的键识别实例,因此您将获得aws_route53_record.proxy_dns["account1"]类的实例而不是aws_route53_record.proxy_dns[0] ,因此您可以添加并在未来从地图中删除元素,Terraform 知道哪个特定实例属于每个元素。

当使用for_each时,资源类型参数中的each.keyeach.value替换count.index 它们分别评估当前地图元素的键和值。

您可以使用 map、键 function、索引 function 和计数的组合。 这个 terraform 创建了 3 个具有各种规则的 acl。

  • acl 的名称由键决定。
  • acl 的数量由键的数量决定。
  • 每条规则的索引(优先级)由索引function决定
  • 每个规则的名称来自 map 中的 CONTAINS_WORD 或 CONTAINS 属性

=>

variable "acls" {
  type = map(any)
  default = {

    "acl1" = {
      "CONTAINS_WORD" = ["api","aaa", "bbb", "ccc"]
      "CONTAINS" = ["xxx","yyy"]
    }

    "acl2" = {
      "CONTAINS_WORD" = [ "url1,"url2","url3"]
      "CONTAINS" = ["url4"]
    }

    "acl3" = {
      "CONTAINS_WORD" = ["xxx"]
      "CONTAINS" = []
    } 
  }
}

resource "aws_wafv2_web_acl" "acl" {
  name  = keys(var.acls)[count.index]
  scope = "REGIONAL"
  count = length(keys(var.acls))

  default_action {
    block {}
  }

  dynamic "rule" {

    for_each = toset(var.acls[keys(var.acls)[count.index]].CONTAINS_WORD)

    content {
      name     =  rule.key
      priority = index(var.acls[keys(var.acls)[count.index]].CONTAINS_WORD, rule.key)

      action {
        allow {}
      }

      statement {
        
        #https://docs.aws.amazon.com/waf/latest/APIReference/API_ByteMatchStatement.html
        byte_match_statement  {

          positional_constraint = "CONTAINS_WORD"
          search_string         = lower(rule.key)

          field_to_match {
            uri_path {}
          }

          text_transformation {
            priority = 0
            type     = "LOWERCASE"
          }
        }
      } 

      visibility_config {
        cloudwatch_metrics_enabled = true
        metric_name                = "waf-${keys(var.acls)[count.index]}-${rule.key}"
        sampled_requests_enabled   = true
      }
    }
  }

  dynamic "rule" {

    for_each = toset(var.acls[keys(var.acls)[count.index]].CONTAINS)

    content {
      name     = replace(rule.key, ".", "_")
      priority = index(var.acls[keys(var.acls)[count.index]].CONTAINS, rule.key) + length(var.acls[keys(var.acls)[count.index]].CONTAINS_WORD)

      action {
        allow {}
      }

      statement {
        
        #https://docs.aws.amazon.com/waf/latest/APIReference/API_ByteMatchStatement.html
        byte_match_statement  {

          positional_constraint = "CONTAINS"
          search_string         = lower(rule.key)

          field_to_match {
            uri_path {}
          }

          text_transformation {
            priority = 0
            type     = "LOWERCASE"
          }
        }
      } 

      visibility_config {
        cloudwatch_metrics_enabled = true
        metric_name                = "waf-${keys(var.acls)[count.index]}-${replace(rule.key, ".", "_")}"
        sampled_requests_enabled   = true
      }
    }
  }


  visibility_config {
    cloudwatch_metrics_enabled = true
    metric_name                = "waf-${keys(var.acls)[count.index]}"
    sampled_requests_enabled   = true
  }
}

使变量成为列表而不是地图。 映射用于将名称引用到值。 列表更适合通过计数方法进行迭代。

variable "account_name" {
  type = "list"
  default = {"accountA","accountB"}
}
resource "aws_route_53_record" "proxy_dns" {
    count = "${length(var.account_name)}"
    zone_id = "${infrastructure.zone_id}"
    name = "proxy-${element(var.account_name, count.index)}-dns
    type = CNAME
    ttl = 60
    records = ["{records.dns_name}"]
}

我正在尝试遍历变量类型映射,但我不确定如何

这是我到目前为止所拥有的

在我的main.tf中:

   resource "aws_route_53_record" "proxy_dns" {
      count = "${length(var.account_name)}"
      zone_id = "${infrastructure.zone_id}"
      name = "proxy-${element(split(",", var.account_name), count.index)}-dns
      type = CNAME
      ttl = 60
      records = ["{records.dns_name}"]
}

在我的variables.tf中

variable "account_name" {
   type = "map"
  default = {
      "account1" = "accountA"
      "account2" = "accountB"
}
}

我希望能够使用不同的帐户名创建多个资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM