terraform-aws-vpc-peering icon indicating copy to clipboard operation
terraform-aws-vpc-peering copied to clipboard

The count fails in resource "aws_route" "requestor"

Open mordax7 opened this issue 4 years ago • 5 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.

mordax7 avatar Jun 30 '20 12:06 mordax7

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

gowthamakanthan avatar Mar 04 '21 06:03 gowthamakanthan

have the same problem

hanselke avatar Mar 31 '21 05:03 hanselke

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.

ekristen avatar Apr 29 '21 13:04 ekristen

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

terraform plan -target="module.vpc"

@mordax7

pj0620 avatar Sep 07 '21 16:09 pj0620

+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.

mohnishbasha avatar Jun 22 '23 16:06 mohnishbasha