Terraform 迭代地图中的列表

问题描述 投票:0回答:2

我正在使用 terraform,并且我的配置如下:

locals  {
  clusters {
    "cluster1" = {
      instance_type = "t2.large"
      vms = ["vm1", "vm2", "vm3"]
      cidr_block = "10.0.0.0/24"
    }
    "cluster2" = {
      instance_type = "t2.large"
      vms = ["vm1", "vm2", "vm3"]
      cidr_block = "10.0.0.0/24"
    }
  }
}

我想要实现的是每个集群部署3个虚拟机。

resource "aws_instance" "cluster_vms" {
  for_each = local.clusters
  ami = local.amis.ubuntu2204
  instance_type = each.value.instance_type
  subnet_id = aws_subnet.public_subnets["${each.key}"].id
  vpc_security_group_ids = [aws_security_group.generic_sg.id]
  key_name = "my_pair"
}

这只会创建 2 个虚拟机,因为有 2 个集群。我需要能够部署 6 个虚拟机,但我没有找到创建名为的虚拟机的方法:

["cluster1_vm1", "cluster2_vm1", "cluster1_vm2", .... ]
这可能吗?或者更好的问题是,如何解决这个问题?

terraform nested-loops
2个回答
0
投票

您可以展平局部变量以提供集群和虚拟机组合。请参阅文档此处

locals {
  cluster_vms_flat = flatten([
    for cluster_name, cluster in local.clusters : [
      for vm in cluster.vms : {
        cluster_name   = cluster_name
        instance_type  = cluster.instance_type
        vm_name        = vm
        cidr_block     = cluster.cidr_block
      }
    ]
  ])
}

这将为您提供以下地图列表,您可以使用它来使用

'${cluster_name}_${vm_name}'
:

构建资源名称
[
  {
    "cluster_name" = "cluster1"
    "instance_type" = "t2.large"
    "vm_name" = "vm1"
    "cidr_block" = "10.0.0.0/24"
  },
  {
    "cluster_name" = "cluster1"
    "instance_type" = "t2.large"
    "vm_name" = "vm2"
    "cidr_block" = "10.0.0.0/24"
  },
  {
    "cluster_name" = "cluster1"
    "instance_type" = "t2.large"
    "vm_name" = "vm3"
    "cidr_block" = "10.0.0.0/24"
  },
  {
    "cluster_name" = "cluster2"
    "instance_type" = "t2.large"
    "vm_name" = "vm1"
    "cidr_block" = "10.0.0.0/24"
  },
  {
    "cluster_name" = "cluster2"
    "instance_type" = "t2.large"
    "vm_name" = "vm2"
    "cidr_block" = "10.0.0.0/24"
  },
  {
    "cluster_name" = "cluster2"
    "instance_type" = "t2.large"
    "vm_name" = "vm3"
    "cidr_block" = "10.0.0.0/24"
  },
...
]


0
投票
locals {
  clusters = {
    "cluster1" = {
      instance_type = "t2.large"
      vms           = ["vm1", "vm2", "vm3"]
      cidr_block    = "10.0.0.0/24"
    }
    "cluster2" = {
      instance_type = "t2.large"
      vms           = ["vm1", "vm2", "vm3"]
      cidr_block    = "10.0.0.0/24"
    }
  }
  cluster_vm_names = flatten([
    for cluster, cluster_data in local.clusters : [
      for vm in cluster_data.vms :
      {

        name          = "${cluster}_${vm}"
        instance_type = cluster_data.instance_type
        cidr_block    = cluster_data.cidr_block
      }
    ]
  ])
}



resource "aws_instance" "cluster_vms" {
  for_each = {
    for entry in local.cluster_vm_names : entry.name => entry
  }
  ami                    = local.amis.ubuntu2204
  instance_type          = each.value.instance_type
  subnet_id              = aws_subnet.public_subnets["${each.key}"].id
  vpc_security_group_ids = [aws_security_group.generic_sg.id]
  key_name               = "my_pair"
}

}
© www.soinside.com 2019 - 2024. All rights reserved.