遍历字符串的条件 for_each 映射

Iterate through a conditional for_each map of strings

试图将一些东西放在一起以通过 tfe 插件的限制。

我有 200 多个工作区,我使用 Terraform Cloud 中的一个变量进行管理,我需要更新这些工作区。在这种情况下,我需要更新的所有工作区都以“dev-workspace”开头。

我有一个包含以下内容的数据块:

data "tfe_workspace_ids" "all" {
  names        = ["*"]
  organization = "myorganization"
}

由于模块的限制,我无法对这些工作区进行通配符搜索。此数据块 return 是一个字符串映射,其中包括 所有 我的工作区:

aa = {
    "dev-workspace-1"                   = "ws-anonymized"
    "dev-workspace-2"                   = "ws-ws-anonymized"
    "dev-workspace-3"                   = "ws-ws-anonymized"
    "test-workspace-1"                  = "ws-ws-anonymized"
    "prod-workspace-1"                  = "ws-ws-anonymized"
}

我的问题是我需要获取这个字符串映射并将其过滤到 return 键中具有“dev-workspace”的字符串。我试过类似下面的东西:

resource "tfe_variable" "dev-workspace" {
  for_each = contains(data.tfe_workspace_ids.all.ids, "dev-workspace")

  key = "access_key"
  value = "XXXX"
  category = "terraform"
  workspace_id = each.value
  sensitive = true
  description = "AWS IAM secret access key."
}

但看起来您不能以这种方式将 containsfor_each:

一起使用
Error: Error in function call

  on main.tf line 16, in resource "tfe_variable" "dev-workspace":
  16:   for_each = contains(data.tfe_workspace_ids.all.ids, "dev-workspace")
    |----------------
    | data.tfe_workspace_ids.all.ids is map of string with 284 elements

Call to function "contains" failed: argument must be list, tuple, or set.

我不太确定在这里做什么,但已经尝试了几种方法,但无法弄清楚。感谢您的帮助。

如果您想过滤,您的资源可能是(您必须将 var.aa 更改为生成输入映射的 data.tfe_workspace_ids 的值):


variable "aa" {

  default = {
      "dev-workspace-1"                   = "ws-anonymized"
      "dev-workspace-2"                   = "ws-ws-anonymized"
      "dev-workspace-3"                   = "ws-ws-anonymized"
      "test-workspace-1"                  = "ws-ws-anonymized"
      "prod-workspace-1"                  = "ws-ws-anonymized"
  }
}

resource "tfe_variable" "dev-workspace" {

  for_each = {for k, v in var.aa:
              k => v if length(regexall("dev-workspace", k)) > 0}

  key = "access_key"
  value = "XXXX"
  category = "terraform"
  workspace_id = each.value
  sensitive = true
  description = "AWS IAM secret access key."
}