我正在尝试使用 for_each 迭代一些扁平数据。我得到的输出看起来是正确的,但在迭代它时我遗漏了一些东西。见下图:
地形:
## LOCALS ##
locals {
alternate_contacts = {
contacts = {
title = "yes"
contact_type = ["BILLING", "OPERATIONS", "SECURITY"]
aws_account = ["12345", "67891", "11213"]
email_address = ["[email protected]", "[email protected]", "[email protected]"]
name = ["bills", "ops", "sec"]
phone_number = "123456789"
}
}
all_aws_accounts = flatten([
for key, contacts in local.alternate_contacts : [
for acc_id in contacts.aws_account : {
contacts = contacts
acc_id = acc_id
}
]
])
accounts = { for v in local.all_aws_accounts : "${v.acc_id}" => v }
}
## RESOURCE ##
resource "aws_account_alternate_contact" "alternate_contacts" {
for_each = local.accounts
alternate_contact_type = each.value.contacts.contact_type
name = each.value.contacts.name
title = each.value.contacts.title
email_address = each.value.contacts.email_address
phone_number = each.value.contacts.phone_number
account_id = each.key
}
输出:
Changes to Outputs:
+ all_aws_accounts = {
+ "11213" = {
+ acc_id = "11213"
+ contacts = {
+ aws_account = [
+ "12345",
+ "67891",
+ "11213",
]
+ contact_type = [
+ "BILLING",
+ "OPERATIONS",
+ "SECURITY",
]
+ email_address = [
+ "[email protected]",
+ "[email protected]",
+ "[email protected]",
]
+ name = [
+ "bills",
+ "ops",
+ "sec",
]
+ phone_number = "123456789"
+ title = "yes"
}
}
+ "12345" = {
+ acc_id = "12345"
+ contacts = {
+ aws_account = [
+ "12345",
+ "67891",
+ "11213",
]
+ contact_type = [
+ "BILLING",
+ "OPERATIONS",
+ "SECURITY",
]
+ email_address = [
+ "[email protected]",
+ "[email protected]",
+ "[email protected]",
]
+ name = [
+ "bills",
+ "ops",
+ "sec",
]
+ phone_number = "123456789"
+ title = "yes"
}
}
+ "67891" = {
+ acc_id = "67891"
+ contacts = {
+ aws_account = [
+ "12345",
+ "67891",
+ "11213",
]
+ contact_type = [
+ "BILLING",
+ "OPERATIONS",
+ "SECURITY",
]
+ email_address = [
+ "[email protected]",
+ "[email protected]",
+ "[email protected]",
]
+ name = [
+ "bills",
+ "ops",
+ "sec",
]
+ phone_number = "123456789"
+ title = "yes"
}
}
}
错误:
│ Error: Incorrect attribute value type
│
│ on main.tf line 94, in resource "aws_account_alternate_contact" "alternate_contacts":
│ 94: alternate_contact_type = each.value.contacts.contact_type
│ ├────────────────
│ │ each.value.contacts.contact_type is tuple with 3 elements
│
│ Inappropriate value for attribute "alternate_contact_type": string required.
我已经看过这个关于迭代的解释: 如何在 Terraform 0.12 中通过列表(对象)for_each 但似乎就是做不到。
请帮助我理解我在这里做错了什么
对我来说,输出看起来不正确,所有这些都是不合适的属性值,
如果我们查看文档:
https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/account_alternate_contact
不知道为什么你在
alternate_contacts
中有这样的结构......locals {
alternate_contacts = {
contacts = {
title = "yes"
contact_type = ["BILLING", "OPERATIONS", "SECURITY"]
aws_account = ["12345", "67891", "11213"]
email_address = ["[email protected]", "[email protected]", "[email protected]"]
name = ["bills", "ops", "sec"]
phone_number = "123456789"
}
}
accounts = {
for k, v in local.alternate_contacts.contacts.aws_account : v => {
aws_account = v
title = local.alternate_contacts.contacts.title
phone_number = local.alternate_contacts.contacts.phone_number
contact_type = local.alternate_contacts.contacts.contact_type[k]
email_address = local.alternate_contacts.contacts.email_address[k]
name = local.alternate_contacts.contacts.name[k]
}
}
}
output "accounts" {
value = local.accounts
}
如果我们对此制定地形计划,我们会得到:
Changes to Outputs:
+ accounts = {
+ "11213" = {
+ aws_account = "11213"
+ contact_type = "SECURITY"
+ email_address = "[email protected]"
+ name = "sec"
+ phone_number = "123456789"
+ title = "yes"
}
+ "12345" = {
+ aws_account = "12345"
+ contact_type = "BILLING"
+ email_address = "[email protected]"
+ name = "bills"
+ phone_number = "123456789"
+ title = "yes"
}
+ "67891" = {
+ aws_account = "67891"
+ contact_type = "OPERATIONS"
+ email_address = "[email protected]"
+ name = "ops"
+ phone_number = "123456789"
+ title = "yes"