基于验证的地图拆分 - Terraform

Splitting of a map, based on validation - Terraform

我正在寻找一种拆分配置的方法 list/map。这样做的原因是我们正在寻找一种与其他 AWS 账户共享资源的方法,方法是使用 aws_ram_principal.

resource "aws_ram_principal_association" "vpc" {
  count = length(module.custom_local.accounts)

  principal          = lookup(module.custom_local.accounts[count.index], "shared") == true ? lookup(module.custom_local.accounts[count.index], "id") : null
  resource_share_arn = aws_ram_resource_share.vpc.arn
}

module.custom_local.accounts 变量如下所示。

  accounts = [
    {
      "name"   = "account_a",
      "id"     = "111111111111",
      "shared" = false
    },
    {
      "name"   = "account_b"
      "id"     = "222222222222"
      "shared" = true
    },
    {
      "name"   = "account_c"
      "id"     = "333333333333"
      "shared" = true
    }
]

这里的问题是,只要所有共享值都是 true,它就可以工作,在 false 的情况下,主体无效并抱怨:The argument "principal" is required, but no definition was found.

现在我想知道是否可以创建一个只包含 true.

帐户的帐户 ID 的局部变量

类似于

locals {
  share_accounts = ....
}

我不确定这是否可行,但我尝试使用下面的方法并没有让我得到任何结果。

  share_accounts = { for s in module.custom_local.accounts : s => ... }
- or - 
  share_accounts = [ for index in range(0, length(module.custom_local.accounts) ... ]

非常感谢任何帮助 - ty。

For 语句带条件。你可以这样做:

share_accounts = [ for s in module.custom_local.accounts : s if s.shared ]