无法在Terraform中计算“计数”的值

时间:2018-12-19 21:09:28

标签: terraform terraform-provider-aws

在这里,我尝试为每个可用区创建一个subnet,然后将route table与每个可用区相关联。

locals {
  aws_region = "${var.aws_regions[var.profile]}"
  base_name = "${var.product}-${local.aws_region}"
  aws_avzones = {
    pro = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
    dev = ["eu-west-2a", "eu-west-2b", "eu-west-2c"]
  }
}

# ---

# Create VPC
resource "aws_vpc" "default" {
  cidr_block = "${var.vpc_cidr_block}"

  tags = {
    Name = "${local.base_name}-vpc"
  }
}

# ---

# Create public subnets - each in a different AZ
resource "aws_subnet" "public" {
  count                   = "${length(local.aws_avzones[var.profile])}"
  vpc_id                  = "${aws_vpc.default.id}"
  cidr_block              = "${cidrsubnet(var.vpc_cidr_block, 8, count.index)}"
  availability_zone       = "${element(local.aws_avzones[var.profile], count.index)}"
  map_public_ip_on_launch = 1

  tags = {
    "Name" = "Public subnet - ${element(local.aws_avzones[var.profile], count.index)}"
  }
}

# ---

# Create Internet gateway for inbound-outbound connections
resource "aws_internet_gateway" "default" {
  vpc_id = "${aws_vpc.default.id}"

  tags = {
    "Name" = "${local.base_name}-igw"
  }
}

# ---

# Create Internet gateway routes table
resource "aws_route_table" "pub" {
  vpc_id = "${aws_vpc.default.id}"

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.default.id}"
  }

  tags = {
    Name = "${local.base_name}-rtb-igw"
  }
}

# ---

# Associate public subnets with the public route table
resource "aws_route_table_association" "pub" {
  count          = "${length(aws_subnet.public.*.id)}"
  subnet_id      = "${element(aws_subnet.public.*.id, count.index)}"
  route_table_id = "${aws_route_table.pub.id}"
}

不幸的是,terraform plan导致错误:

  
      
  • aws_route_table_association.pub:aws_route_table_association.pub:无法计算“计数”的值
  •   

为什么无法计算? Terraform在红外线时没有抱怨。一切正常并且正在运行,只有在销毁后尝试重新创建基础结构时,我才发现此错误。

当前,我的解决方法是先注释掉所有aws_route_table_association块,然后注释terraform apply,取消注释,然后完成作业。显然,这与理想情况相去甚远。

顺便说一句,我也尝试过像这样的显式依赖声明:

resource "aws_route_table_association" "pub" {
  count          = "${length(aws_subnet.public.*.id)}"
  subnet_id      = "${element(aws_subnet.public.*.id, count.index)}"
  route_table_id = "${aws_route_table.pub.id}"

  depends_on = ["aws_subnet.public"]
}

但这没有帮助。

$ terraform --version
Terraform v0.11.11
+ provider.aws v1.52.0

0 个答案:

没有答案