代码之家  ›  专栏  ›  技术社区  ›  NarÅ«nasK

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

  •  0
  • NarÅ«nasK  · 技术社区  · 6 年前

    我想创造一个 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\路由\表_协会.pub:aws\路由\表_协会.pub:无法计算“count”的值

    为什么不能计算?Terraform并没有抱怨,当infra。当一切正常运行时,我在试图重新创建infra时,仅在销毁之后才发现此错误。

    目前我的解决方法是注释掉所有 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 回复  |  直到 6 年前