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

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

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

问题

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

  1. 我有这个资源和这个带有变量的动态块:
  2. ```hcl
  3. variable "services" {
  4. description = "服务映射"
  5. type = map(object({
  6. port = number
  7. uri = optional(string)
  8. }))
  9. }
  1. services = {
  2. "auth-service" = {
  3. port = 3050
  4. uri = "example1"
  5. }
  6. "clone-service" = {
  7. port = 3040
  8. }
  9. }
  1. resource "kubernetes_ingress_v1" "eks_global_ingress" {
  2. [...]
  3. dynamic "rule" {
  4. # 我制作了这个表达式来绕过没有URI的服务。
  5. for_each = { for k, v in var.services : k => v if v["uri"] != null }
  6. content {
  7. http {
  8. path {
  9. path = "/${rule.value["uri"]}/*"
  10. backend {
  11. service {
  12. name = "${rule.key}-i-svc"
  13. port {
  14. number = rule.value["port"]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }

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

  1. services = {
  2. "auth-service" = {
  3. port = 3050
  4. uri = ["example1", "example2"]
  5. }
  6. "clone-service" = {
  7. port = 3040
  8. }
  9. }

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

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

  1. Is there anything else you would like to know or clarify about this code?
  2. <details>
  3. <summary>英文:</summary>
  4. 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"]
}
}
}
}
}
}
}

  1. 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

  1. 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?
  2. 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.
  3. </details>
  4. # 答案1
  5. **得分**: 0
  6. 1. 完全可以在另一个动态块内部使用更多的动态块。
  7. 2. 参数 http.path 也是[块的列表][1],所以你应该能够做类似这样的事情:
  8. ```hcl
  9. dynamic "rule" {
  10. # 通过此表达式绕过没有 URI 的服务。
  11. for_each = { for k, v in var.services : k => v if length(v["uri"]) > 0 }
  12. content {
  13. http {
  14. dynamic "path" {
  15. for_each = toset(rule.value["uri"])
  16. content {
  17. path = "/${path.key}/*"
  18. backend {
  19. service {
  20. name = "${rule.key}-i-svc"
  21. port {
  22. number = rule.value["port"]
  23. }
  24. }
  25. }
  26. }
  27. }
  28. }
  29. }
  30. }

注意:不要忘记将 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 -->

  1. dynamic &quot;rule&quot; {
  2. # I make this expression to bypass the services that doesn&#39;t have URI.
  3. for_each = { for k, v in var.services : k =&gt; v if length(v[&quot;uri&quot;]) &gt; 0 }
  4. content {
  5. http {
  6. dynamic &quot;path&quot; {
  7. for_each = toset(rule.value[&quot;uri&quot;])
  8. content {
  9. path = &quot;/${path.key}/*&quot;
  10. backend {
  11. service {
  12. name = &quot;${rule.key}-i-svc&quot;
  13. port {
  14. number = rule.value[&quot;port&quot;]
  15. }
  16. }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. }

<!-- 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:

确定