如何修复 terraform 错误 var.zaccounts 是一个包含 1 个元素的字符串列表。

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

How to fix terraform error var.zaccounts is list of string with 1 element

问题

Sure, here's the translated text:

我想要一个字符串列表,而我的一个帐户(由tfstate支持)恰好在列表中有一个1。我感到困惑的是为什么您可以循环遍历大小为1的列表。代码如下:

resource "datadog_dashboard" "hq" {
  for_each = var.zaccounts
  ...

然后变量是:

variable "zaccounts" {
    type        = list(string)
    default     = []
    description = "帐户列表"
}

var.auto.vars 是:

zaccounts = [ "zmainbranch" ]

在这个示例中,当我们运行时,只有一个tfstate,一个帐户,开发人员创建了一个基本上创建上述tf文件的框架,因此我们没有使用工作区或其他任何东西。这是一个相当简单的示例。

我不确定在这里应该尝试什么,因为错误是违反直观的,准确地说出了在任何编程语言中都应该能够执行的操作 - 即,您应该能够循环遍历大小为1的列表。

英文:

I want a list of strings and one of my accounts (backed by tfstate) happens to have 1 in the list. I am confused why you can loop over list of size 1. The code is simply

resource "datadog_dashboard" "hq" {
  for_each = var.zaccounts
  ...

and then the var is

variable "zaccounts" {
    type = list(string)
    default = []
    description = "List of accounts"
}

var.auto.vars is

zaccounts = [ "zmainbranch" ]

In the example, when we run there is only ONE tfstate, ONE account and a developer created a framework which basically creates the above tf files so we are not using workspaces or anything else. It is a pretty simple example.

I am not sure what to try here as the error is counter intuitive saying exactly what you should be able to do in any programming language - ie. you should be able to loop over a list of size 1.

答案1

得分: 2

The for_each meta argument can work only with maps and sets:

> If a resource or module block includes a for_each argument whose value is a map or a set of strings, Terraform creates one instance for each member of that map or set.

So you could either define the variable as a set of strings or perform a conversion to a set:

resource "datadog_dashboard" "hq" {
  for_each = toset(var.zaccounts)
  ...
}

As mentioned, alternatively you can redefine the variable:

variable "zaccounts" {
    type = set(string)
    default = []
    description = "List of accounts"
}

In that case, you would not need the toset built-in function, the code you have can remain as-is.

英文:

The for_each meta argument can work only with maps and sets:

> If a resource or module block includes a for_each argument whose value is a map or a set of strings, Terraform creates one instance for each member of that map or set.

So you could either define the variable as a set of strings or perform a conversion to a set:

resource "datadog_dashboard" "hq" {
  for_each = toset(var.zaccounts)
  ...
}

As mentioned, alternatively you can redefine the variable:

variable "zaccounts" {
    type = set(string)
    default = []
    description = "List of accounts"
}

In that case, you would not need the toset built-in function, the code you have can remain as-is.

huangapple
  • 本文由 发表于 2023年5月17日 15:54:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76269740.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定