Dynamic block with "for each" in a variable with a list – Terraform

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

Dynamic block with "for each" in a variable with a list - Terraform

问题

I can provide a translation of the code part you've shared:

我有这个资源和这个带有变量的动态块:

```hcl
variable "services" {
  description = "服务映射"
  type = map(object({
    port = number
    uri  = optional(string)
  }))
}
services = {

  "auth-service" = {
    port = 3050
    uri  = "example1"
   }

"clone-service" = {
    port = 3040
   }

}
resource "kubernetes_ingress_v1" "eks_global_ingress" {

[...]

dynamic "rule" {
     # 我制作了这个表达式来绕过没有URI的服务。
      for_each = { for k, v in var.services : k => v if v["uri"] != null }
      content {
        http {
          path {
            path = "/${rule.value["uri"]}/*"
            backend {
              service {
                name = "${rule.key}-i-svc"
                port {
                  number = rule.value["port"]
                }
              }
            }
          }
        }
      }
    }

到目前为止一切都正常,但现在我想为同一个服务添加另一个URI。所以我将URI制作成了一个列表,如下所示:

services = {
  "auth-service" = {
    port = 3050
    uri  = ["example1", "example2"]
  }
  "clone-service" = {
    port = 3040
  }
}

但是,使用这种方式,我无法在动态块中包含另一个for_each。您如何同时过滤空的URI并循环处理具有2个或更多URI的代码?

我希望规则根据列表中存在的URI数量重复"x"次,并且如果地图中不存在URI值(null),则"for_each"会绕过此规则。


Is there anything else you would like to know or clarify about this code?

<details>
<summary>英文:</summary>

I have this resource and this dynamic block with this variables:

variable "services" {
description = "Map of the Services"
type = map(object({
port = number
uri = optional(string)
}))
}



services = {

"auth-service" = {
port = 3050
uri = "example1"
}

"clone-service" = {
port = 3040
}

}



resource "kubernetes_ingress_v1" "eks_global_ingress" {

[...]

dynamic "rule" {
# I make this expression to bypass the services that doesn't have URI.
for_each = { for k, v in var.services : k => v if v["uri"] != null }
content {
http {
path {
path = "/${rule.value["uri"]}/*"
backend {
service {
name = "${rule.key}-i-svc"
port {
number = rule.value["port"]
}
}
}
}
}
}
}


At this point everything is ok, but now i want to add another URI to the same service. So i make a list in the URI like below:


services = {
"auth-service" = {
port = 3050
uri = ["example1","example2]
"clone-service" = {
port = 3040


But with this i cant include another for_each in the Dynamic Block. How can i filter null URI and at the same time, loop the code with 2 or more URI as a list?

I expect that the rule is duplicated &quot;x&quot; times over the number of URI present on the list, and if a URI value is not present on the map (null) the &quot;for_each&quot; bypass this rule. 

</details>


# 答案1
**得分**: 0


1. 完全可以在另一个动态块内部使用更多的动态块。
2. 参数 http.path 也是[块的列表][1],所以你应该能够做类似这样的事情:

```hcl
dynamic "rule" {
  # 通过此表达式绕过没有 URI 的服务。
  for_each = { for k, v in var.services : k => v if length(v["uri"]) > 0 }
  content {
    http {
      dynamic "path" {
        for_each = toset(rule.value["uri"])
        content {
          path = "/${path.key}/*"
          backend {
            service {
              name = "${rule.key}-i-svc"
              port {
                number = rule.value["port"]
              }
            }
          }
        }
      }
    }
  }
}

注意:不要忘记将 uri 的类型从字符串更改为字符串列表。

英文:
  1. It is completely possible to use more dynamic blocks inside another dynamic block
  2. The parameter http.path is a list too of blocks so you should be able to do something like this:

<!-- language: lang-hcl -->

      dynamic &quot;rule&quot; {
     # I make this expression to bypass the services that doesn&#39;t have URI.
      for_each = { for k, v in var.services : k =&gt; v if length(v[&quot;uri&quot;]) &gt; 0 }
      content {
        http {
          dynamic &quot;path&quot; {
            for_each = toset(rule.value[&quot;uri&quot;])
            content {
              path = &quot;/${path.key}/*&quot;
              backend {
                service {
                  name = &quot;${rule.key}-i-svc&quot;
                  port {
                    number = rule.value[&quot;port&quot;]
                  }
                }
              }
            }
          }
        }
      }
    }

<!-- end snippet -->

Note: don't forget change the uri type from string to list(string)

huangapple
  • 本文由 发表于 2023年5月11日 18:32:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76226670.html
匿名

发表评论

匿名网友

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

确定