英文:
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 "x" times over the number of URI present on the list, and if a URI value is not present on the map (null) the "for_each" 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 的类型从字符串更改为字符串列表。
英文:
- It is completely possible to use more dynamic blocks inside another dynamic block
- The parameter http.path is a list too of blocks so you should be able to do something like this:
<!-- language: lang-hcl -->
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 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"]
}
}
}
}
}
}
}
}
<!-- end snippet -->
Note: don't forget change the uri type from string to list(string)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论