cloudposse / terraform-aws-vpc-peering

Terraform module to create a peering connection between two VPCs in the same AWS account.

Home Page:https://cloudposse.com/accelerate

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

The count fails in resource "aws_route" "requestor"

mordax7 opened this issue · comments

The Terraform AWS VPC Peering module fails if the requestor VPC does not exist yet.
We are trying to create the requestor's VPC and the peering at the same time, which ends up in this error:

Error: Invalid count argument

  on .terraform/modules/vpc.vpc_peering/main.tf line 62, in resource "aws_route" "requestor":
  62:   count                     = var.enabled ? length(distinct(sort(data.aws_route_tables.requestor.0.ids))) * length(data.aws_vpc.acceptor.0.cidr_block_associations) : 0

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.


Error: Invalid count argument

  on .terraform/modules/vpc.vpc_peering/main.tf line 71, in resource "aws_route" "acceptor":
  71:   count                     = var.enabled ? length(distinct(sort(data.aws_route_tables.acceptor.0.ids))) * length(data.aws_vpc.requestor.0.cidr_block_associations) : 0

The "count" value depends on resource attributes that cannot be determined
until apply, so Terraform cannot predict how many instances will be created.
To work around this, use the -target argument to first apply only the
resources that the count depends on.
commented

Facing the same issue. Is this fixed? Am using the following versions.

Terraform v0.12.29
+ provider.aws v3.1.0
+ provider.template v2.1.2

have the same problem

I am seeing the same but I am not sure there's anyway around it with the way the module is written, it's a limitation of terraform. One possible way to address this would be to have a version where you could pass all the route tables from the subnet modules in vs having it discover, terraform would be able to know then what route tables were going to exist at plan time.

I fixed this issue by, as noted in the error message, using the -target argument.

terraform plan -target="module.vpc"

@mordax7

+1, the module.vpc_peering expects both requestor and acceptor vpcs to be provisioned before calling itself.
Is it kinda annoying but you will need to provision them first using the -target= construct and then run terraform again.