Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] "count" cannot be determined until apply #102

Open
gregory-lecomte opened this issue May 4, 2023 · 8 comments
Open

[BUG] "count" cannot be determined until apply #102

gregory-lecomte opened this issue May 4, 2023 · 8 comments
Assignees
Labels

Comments

@gregory-lecomte
Copy link

Describe the bug
I just want to configure a VPC peering between 3 AWS account, but I have an error message "The "count" value depends on resource attributes that cannot be determined until apply, so Terraform cannot predict how many instances will be created."

The issue looks related to this : #83 but as you can see in my configuration, I have set depends_on.

Terraform version:
1.4.6

Module version:
5.1.0

Error message:

╷
│ Error: Invalid count argument
│ 
│   on .terraform/modules/vpc-peering/main.tf line 55, in resource "aws_route" "this_routes":
│   55:   count                     = local.create_routes_this ? length(local.this_routes) : 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-peering/main.tf line 79, in resource "aws_route" "peer_routes":
│   79:   count                     = local.create_routes_peer ? length(local.peer_routes) : 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.

Terraform code that produces the error:

module "vpc-peering" {
  source  = "grem11n/vpc-peering/aws"
  version = "5.1.0"

  depends_on = [
    module.vpc_source,
    module.vpc_destination
  ]

  providers = {
    aws.this = aws.source
    aws.peer = aws.destination
  }

  this_vpc_id = module.vpc_source.vpc_id
  peer_vpc_id = module.vpc_destination.vpc_id

  auto_accept_peering = true

  tags     = local.common_tags
}

Additional context
I use the "terraform-aws-modules/vpc/aws" module to create a VPC for each AWS account and I want to configure peering between theses VPC in the same terraform project.

@PhilipSchmid
Copy link

I ran into a similar issue today. What helped was actually removing the complete depends_on block in my Terraform code:

  depends_on = [
    module.vpc_source,
    module.vpc_destination
  ]

@gregory-lecomte
Copy link
Author

I still have the same issue in both cases

@gregory-lecomte
Copy link
Author

@grem11n, have you got a solution ?

@uridium
Copy link
Contributor

uridium commented Jun 9, 2023

Same problem here.
I tried a workaround from here https://github.com/grem11n/terraform-aws-vpc-peering/tree/master/examples/associated-cidrs but it doesn't help

@uridium
Copy link
Contributor

uridium commented Jun 9, 2023

The full code example is down below.
It's not possible to create vpc-us, vpc-eu and vpc-peering in one go.

provider "aws" {
  region = "us-east-1"
  alias  = "us"
}

provider "aws" {
  region = "eu-west-1"
  alias  = "eu"
}

module "vpc-us" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"

  providers = { aws = aws.us }

  name = "north-virginia"
  cidr = "10.1.0.0/16"

  azs             = ["us-east-1a", "us-east-1b", "us-east-1c"]
  public_subnets  = ["10.1.0.0/21", "10.1.16.0/21", "10.1.32.0/21"]
  private_subnets = ["10.1.128.0/21", "10.1.144.0/21", "10.1.160.0/21"]

  enable_nat_gateway = false
}

module "vpc-eu" {
  source  = "terraform-aws-modules/vpc/aws"
  version = "5.0.0"

  providers = { aws = aws.eu }

  name = "ireland"
  cidr = "10.2.0.0/16"

  azs             = ["eu-west-1a", "eu-west-1b", "eu-west-1c"]
  public_subnets  = ["10.2.0.0/21", "10.2.16.0/21", "10.2.32.0/21"]
  private_subnets = ["10.2.128.0/21", "10.2.144.0/21", "10.2.160.0/21"]

  enable_nat_gateway = false
}

module "vpc-peering" {
  source  = "grem11n/vpc-peering/aws"
  version = "6.0.0"

  providers = {
    aws.this = aws.us
    aws.peer = aws.eu
  }

  this_vpc_id = module.vpc-us.vpc_id
  peer_vpc_id = module.vpc-eu.vpc_id

  auto_accept_peering = true
}

@grem11n
Copy link
Owner

grem11n commented Jun 15, 2023

Please, refer to the example of the module with depends_on which covers this case.

To avoid this error, one has to also provide route table ids.

@uridium
Copy link
Contributor

uridium commented Jul 21, 2023

For future searches, here is the updated vpc-peering module:

module "vpc-peering" {
  source  = "grem11n/vpc-peering/aws"
  version = "6.0.0"

  providers = { 
    aws.this = aws.us
    aws.peer = aws.eu
  }

  this_vpc_id  = module.vpc-us.vpc_id
  peer_vpc_id  = module.vpc-eu.vpc_id
  this_rts_ids = concat(module.vpc-us.public_route_table_ids, module.vpc-us.private_route_table_ids)
  peer_rts_ids = concat(module.vpc-eu.public_route_table_ids, module.vpc-eu.private_route_table_ids)

  auto_accept_peering = true
}

Now terraform apply works in one go.

@gregory-lecomte
Copy link
Author

Thank you for your feedback !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants