英文:
Looking to pass a map in terraform
问题
Here is the translated portion of your code:
我有一个本地的YAML文件。
```acls:
- owner: "terraform-service-account"
principal: "sa-1234"
resources:
- resource_name: "be_serivicing"
resource_type: "TOPIC"
operation: "WRITE"
- resource_name: "cjiscool"
resource_type: "TOPIC"
operation: "READ"
- owner: "terraform-service-account-write"
principal: "sa-948534"
resources:
- resource_name: "cjiscoolgroup"
resource_type: "GROUP"
operation: "READ"
我希望将resource_name、resource_type、principal和operation的值传递给一个模块。
从我的代码中,我有一个输出
output "principals_resources" {
value = {for acl in local.acls : acl.owner => acl.resources}
}
这给我输出
Changes to Outputs:
+ principals_resources = {
+ terraform-service-account = [
+ {
+ operation = "WRITE"
+ resource_name = "be_serivicing"
+ resource_type = "TOPIC"
},
+ {
+ operation = "READ"
+ resource_name = "cjiscool"
+ resource_type = "TOPIC"
},
]
+ terraform-service-account-write = [
+ {
+ operation = "READ"
+ resource_name = "cjiscoolgroup"
+ resource_type = "GROUP"
},
]
}
当我在模块中添加for_each时,最初我得到了包含多个值的元组,这应该是从输出中预期的。
module "acls" {
source = "../acls"
for_each = {for acl in local.acls : acl.owner => acl.resources}
resource_type = each.value.resource_type
resource_name = each.value.resource_name
principal = each.value.principal
operation = each.value.operation
}
我如何将每个resource_name、resource_type、operation和principal传递给模块?
我尝试将第一个映射值输出到另一个输出,然后将其输入到我的模块中。如果这个问题让你感到困惑,我对此感到抱歉,我是terraform的新手,需要帮助。
编辑: 这是模块。它正在创建一个Confluent主题。
resource "confluent_kafka_acl" "topic_write" {
count = var.resource_type == "TOPIC" && var.operation == "WRITE" ? 1 : 0
resource_type = "TOPIC"
resource_name = var.resource_name
pattern_type = "LITERAL"
principal = "User:${var.principal}"
host = "*"
operation = "WRITE"
permission = "ALLOW"
}
这是我收到的错误。
│ on main.tf line 12, in module "acls":
│ 12: resource_type = each.value.resource_type
│ ├────────────────
│ │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
如果您有进一步的问题或需要更多的帮助,请随时提出。
英文:
i have a local yaml file.
- owner: "terraform-service-account"
principal: "sa-1234"
resources:
- resource_name: "be_serivicing"
resource_type: "TOPIC"
operation: "WRITE"
- resource_name: "cjiscool"
resource_type: "TOPIC"
operation: "READ"
- owner: "terraform-service-account-write"
principal: "sa-948534"
resources:
- resource_name: "cjiscoolgroup"
resource_type: "GROUP"
operation: "READ"
Im looking to pass the values resource_name, resource_type, principal and operation to a module.
From my code I have an output
output "principals_resources" {
value = {for acl in local.acls : acl.owner => acl.resources}
}
which gives me output
Changes to Outputs:
+ principals_resources = {
+ terraform-service-account = [
+ {
+ operation = "WRITE"
+ resource_name = "be_serivicing"
+ resource_type = "TOPIC"
},
+ {
+ operation = "READ"
+ resource_name = "cjiscool"
+ resource_type = "TOPIC"
},
]
+ terraform-service-account-write = [
+ {
+ operation = "READ"
+ resource_name = "cjiscoolgroup"
+ resource_type = "GROUP"
},
]
}
When I add the for_each in the module initially Im getting two or more values inside the tuple which should be intended from the output.
module "acls" {
source = "../acls"
for_each = {for acl in local.acls : acl.owner => acl.resources}
resource_type = each.value.resource_type
resource_name = each.value.resource_name
principal = each.value.principal
operation = each.value.operation
}
How can I pass each resource_name, resource_type, operation and principal to a module?
I have tried to output the first map values to another output that would be inputted into my module. Sorry if this question is confusing, Im new to terraform and need help
EDIT: Here is the module. its creating a confluent topic
resource "confluent_kafka_acl" "topic_write" {
count = var.resource_type == "TOPIC" && var.operation == "WRITE" ? 1 : 0
resource_type = "TOPIC"
resource_name = var.resource_name
pattern_type = "LITERAL"
principal = "User:${var.principal}"
host = "*"
operation = "WRITE"
permission = "ALLOW"
}
& here is the error Im getting.
│ on main.tf line 12, in module "acls":
│ 12: resource_type = each.value.resource_type
│ ├────────────────
│ │ each.value is tuple with 2 elements
│
│ This value does not have any attributes.
答案1
得分: 1
You have to flatten your acls
:
locals {
acls_flat = merge([
for owner, values in local.acls: {
for resource in values.resources:
"${owner}-${resource.resource_name}" => {
"${owner}" = owner
principal = values.principal
"resource" = resource
}
}
]...)
}
then
module "acls" {
source = "../acls"
for_each = local.acls_flat
resource_type = each.value.resource.resource_type
resource_name = each.value.resource.resource_name
principal = each.value.principal
operation = each.value.operation
}
英文:
You have to flatten you acls
:
locals {
acls_flat = merge([
for owner, values in local.acls: {
for resource in values.resources:
"${owner}-${resource.resource_name}" => {
"${owner}" = owner
principal = values.principal
"resource" = resource
}
}
]...)
}
then
module "acls" {
source = "../acls"
for_each = local.acls_flat
resource_type = each.value.resource.resource_type
resource_name = each.value.resource.resource_name
principal = each.value.principal
operation = each.value.operation
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论