Terraform 使用局部变量组合变量

Terraform combine variables using locals

我想合并 2 个变量。我尝试使用语言环境,但即使使用 flatten

也无法获得最终结果

还有其他推荐吗? 我尝试使用这种结构,因为 vpc 模块接收到带有子网列表的对象,想法是在网络中我们可以在列表中添加子网的名称

variable "subnets" {
  default = {
    "subnet-01" = {
      name          = "subnet-01"
      subnet_ip     = "10.10.10.0/24"
      subnet_region = "us-central1"
      description   = "This subnet has a description"
    }
    "subnet-02" = {
      name          = "subnet-02"
      subnet_ip     = "10.10.20.0/24"
      subnet_region = "us-central1"
      description   = "This subnet has a description"
    }
    "subnet-03" = {
      name          = "subnet-03"
      subnet_ip     = "10.10.20.0/24"
      subnet_region = "us-central1"
      description   = "This subnet has a description"
    }
  }
}
variable "networks" {
  default = {
    "test-network" = {
      subnets         = ["subnet-01", "subnet-02"]
        auto_create_subnetworks         = "false"
        mtu                             = "0"
    }
    "test-network-2" = {
      subnets         = ["subnet-03"]
      auto_create_net = false
        auto_create_subnetworks         = "false"
        mtu                             = "0"
    }
  }
}

是否可以选择以下结果?

networks = {
  test-network = {
    auto_create_subnetworks = false
    mtu                     = 0
    subnets = [
        subnet-01           = {
            name            = "subnet-01"
            subnet_ip       = "10.10.10.0/24"
            subnet_region   = "us-central1"
            description     = "This subnet has a description"
        }
        subnet-02           = {
            name            = "subnet-02"
            subnet_ip       = "10.10.20.0/24"
            subnet_region   = "us-central1"
            description     = "This subnet has a description"
        }
    ]
  }
  test-network-2 = {
    auto_create_subnetworks = false
    mtu                     = 0
    subnets = [
        subnet-03           = {
            name            = "subnet-03"
            subnet_ip       = "10.10.30.0/24"
            subnet_region   = "us-central1"
            description     = "This subnet has a description"
        }
    ]
  }
}

您可以使用嵌入式循环实现该结果:

locals {
  networks = {
    for name, network in var.networks : name => {
      "subnets" : [for subnet_name in network.subnets : var.subnets[subnet_name]],
      "auto_create_subnetworks" : network.auto_create_subnetworks,
      "mtu" : network.mtu
    }
  }
}

此外,请注意,您的 subnet-02 具有 subnet-01name,这可能不是您想要的。

我有以下代码。

我的模块

variable "senses" {
  type = string
}


locals {
  sounds = {
    "cat" = "meow"
    "dog" = ["bark", "woof"]
  }
}

output "noise" {
  value = local[var.senses]["cat"]
}


调用我的模块

module "mymodule" {
  source = "../../../modules/mymodule"
  senses = "sound"
}

returns 错误:

Error: Invalid reference

  on ../../../modules/mymodule/outputs.tf line 62, in output "noise":
  62:   value = local[var.senses]["cat"]

The "local" object cannot be accessed directly. Instead, access one of its
attributes.

我的代码似乎无法处理

value = local[var.senses]["cat"]

关于如何让它工作有什么建议吗?