![](/img/trans.png)
[英]Terraform - Use for_each with a list of strings inside tuple
[英]for_each for a list of strings
我正在 terraform 雲中創建一個變量,並使用該變量作為輸入來創建一個random_pet
資源。
resource "tfe_variable" "names" {
key = "name_prefixes"
value = jsonencode(yamldecode(file("names_list.yaml")))
workspace_id = "ws-id"
hcl = true
category = "terraform"
}
resource "random_pet" "pet" {
for_each = toset(tfe_variable.names.key)
prefix = each.key
}
cat names_list.yaml
---
- "tfe"
- "tfc"
- "ansible"
- "puppet"
我收到一個錯誤:
Error: Invalid function argument
on main.tf line 12, in resource "random_pet" "pet":
for_each = toset(tfe_variable.names.key)
Invalid value for "v" parameter: cannot convert string to set of any single type.
你能建議嗎?
您只有一個tfe_variable.names
實例。 因此,沒有什么可以迭代的。 所以你應該有:
resource "random_pet" "pet" {
prefix = tfe_variable.names.key
}
更新
resource "tfe_variable" "names" {
key = "name_prefixes"
value = jsonencode(yamldecode(file("names_list.yaml")))
workspace_id = "ws-id"
hcl = true
category = "terraform"
}
resource "random_pet" "pet" {
for_each = toset(yamldecode(file("names_list.yaml")))
prefix = each.value
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.