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

Error: Invalid count argument #58

Closed
djkerya opened this issue Feb 11, 2022 · 3 comments
Closed

Error: Invalid count argument #58

djkerya opened this issue Feb 11, 2022 · 3 comments
Labels
bug 🐛 An issue with the system

Comments

@djkerya
Copy link

djkerya commented Feb 11, 2022

│ Error: Invalid count argument
│ 
│   on .terraform/modules/vpc-peering-multi-account/accepter.tf line 68, in data "aws_route_table" "accepter":68:   count     = local.accepter_enabled ? local.accepter_subnet_ids_count : 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-multi-account/requester.tf line 122, in data "aws_route_table" "requester":122:   count     = local.enabled ? local.requester_subnet_ids_count : 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.
@djkerya djkerya added the bug 🐛 An issue with the system label Feb 11, 2022
@djkerya
Copy link
Author

djkerya commented Feb 11, 2022

terraform {
  required_providers {
    aws = {
      version = "3.74.1"
    }
  }
}

provider "aws" {
  region = "eu-west-2"
}

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

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


resource "aws_vpc" "vpc_uswest" {
  provider             = aws.us-west-1
  cidr_block           = "10.10.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true
}


resource "aws_vpc" "vpc_euwest" {
  provider             = aws.eu-west-2
  cidr_block           = "10.11.0.0/16"
  enable_dns_support   = true
  enable_dns_hostnames = true
}

resource "aws_iam_role" "test_role" {
  name = "cross_region_vpc_peering"


  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [
      {
        Action = "sts:AssumeRole"
        Effect = "Allow"
        Sid    = ""
        Principal = {
          "AWS" : "arn:aws:iam::426005093346:user/kiril"
        }
    }]
  })
}

module "vpc-peering-multi-account" {
  source = "cloudposse/vpc-peering-multi-account/aws"
  //  version = "0.5.0"

  accepter_region               = var.accepter_region
  accepter_vpc_id               = aws_vpc.vpc_uswest.id
  requester_aws_assume_role_arn = aws_iam_role.test_role.arn
  requester_region              = var.requester_region
  requester_vpc_id              = aws_vpc.vpc_euwest.id

}

resource "aws_subnet" "subnet_1_us" {
  provider = aws.us-west-1
  // availability_zone = element(data.aws_availability_zones.azs.names, 0)
  vpc_id     = aws_vpc.vpc_uswest.id
  cidr_block = "10.10.1.0/24"
}

resource "aws_subnet" "subnet_1_eu" {
  //  provider   = aws.eu-west-2
  vpc_id            = aws_vpc.vpc_euwest.id
  cidr_block        = "10.11.1.0/24"
  availability_zone = "eu-west-2a"
}

resource "aws_route_table" "route_table_us" {
  provider = aws.us-west-1
  vpc_id   = aws_vpc.vpc_uswest.id
  //  route {
  //    cidr_block = "0.0.0.0/0"
  //    gateway_id = aws_internet_gateway.igw-us.id
  //  }
  //    route {
  //    cidr_block                = "10.11.1.0/24"
  //    vpc_peering_connection_id = aws_vpc_peering_connection.eu-us-peering.id
  //  }

  lifecycle {
    ignore_changes = all
  }
  tags = {
    Name = "US-Region-RT"
  }
}

resource "aws_main_route_table_association" "set-us-default-rt-assoc" {
  provider       = aws.us-west-1
  vpc_id         = aws_vpc.vpc_uswest.id
  route_table_id = aws_route_table.route_table_us.id
}

resource "aws_route_table" "route_table_eu" {
  provider = aws.eu-west-2
  vpc_id   = aws_vpc.vpc_euwest.id
  /*  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.igw-eu.id
  }
    route {
    cidr_block                = "10.0.1.0/24"
    vpc_peering_connection_id = aws_vpc_peering_connection.eu-us-peering.id
  }
*/
  lifecycle {
    ignore_changes = all
  }
  tags = {
    Name = "EU-Region-RT"
  }
}

resource "aws_main_route_table_association" "set-eu-default-rt-assoc" {
  provider       = aws.eu-west-2
  vpc_id         = aws_vpc.vpc_euwest.id
  route_table_id = aws_route_table.route_table_eu.id
}

@djkerya
Copy link
Author

djkerya commented Feb 11, 2022

terraform2.zip

@nitrocode
Copy link
Member

I fixed some of the formatting above to make it readable.

This is most likely due to the subnets and vpc not already existing prior to creating the vpc peering.

In our current example, we only create the peering and assume the vpcs are already created.

https://github.com/cloudposse/terraform-aws-vpc-peering-multi-account/blob/master/examples/complete/main.tf

You may want to consider our other module which does support creating both vpcs and the peering connection.

https://github.com/cloudposse/terraform-aws-vpc-peering/blob/master/examples/complete/main.tf

Please comment and we can reopen the issue

@nitrocode nitrocode pinned this issue Jun 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An issue with the system
Projects
None yet
Development

No branches or pull requests

2 participants