【问题标题】:How to run count.index for limited indexes in terraform如何为 terraform 中的有限索引运行 count.index
【发布时间】:2020-08-14 18:50:02
【问题描述】:

有什么方法可以为某些索引值而不是列表中的所有值运行 count.index。 我有一个列表变量

variable "subnets_cidr" {
    type = list
    default = ["172.17.1.0/24", "172.17.2.0/24", "172.17.3.0/24","172.17.10.0/24","172.17.11.0/24","172.17.12.0/24"]
}

还有一个子网

    # Subnets : Public
resource "aws_subnet" "public" {
  count = length(var.subnets_cidr)
  vpc_id = aws_vpc.terra_vpc.id
  cidr_block = element(var.subnets_cidr,count.index)
  availability_zone = element(var.azs,count.index)
  map_public_ip_on_launch = true
  tags = {
    Name = element(var.pub_sub_names, count.index)
  }
}

使用上面的代码正在创建 6 个子网,但我不希望这样,我想限制只创建 3 个子网。 我正在考虑像 count.index[1:3] 这样对 count.index 变量进行切片,但它不起作用。

还有下面的路由表关联代码,它关联了所有子网,但在这里我也想限制子网的数量。例如说前 3 个子网。

# Route table association with public subnets
resource "aws_route_table_association" "a" {
  count = length(var.subnets_cidr)
  subnet_id      = element(aws_subnet.public.*.id,count.index)
  route_table_id = aws_route_table.public_rt.id
}

【问题讨论】:

    标签: terraform-provider-aws terraform0.12+


    【解决方案1】:

    您可以将本地变量用于不同的子网,而不是使用计数 对一个子网范围使用 subnetone,对其他子网范围使用 subnettwo

    locals {
     teams = {
      "subnetone" = {
        subnet1 = "172.17.1.0/24"
        subnet2 = "172.17.2.0/24"
        subnet3 = "172.17.3.0/24"
      },
      "subnettwo" = {
         subnet4 = "172.17.10.0/24"
         subnet5 = "172.17.11.0/24"
         subnet6 = "172.17.12.0/24"
      } 
    }
    }
    
    resource "aws_subnet" "public" {
      for_each = local.teams.subnetone
      cidr_block = each.value
      vpc_id = "vpc-09c37360e8b599296"
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-22
      • 2021-08-06
      • 2018-09-09
      • 2018-10-15
      • 2019-01-23
      • 2020-05-23
      • 2022-11-03
      • 2020-01-16
      • 1970-01-01
      相关资源
      最近更新 更多