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

Errror: Value of count cannot be computed #15172

Closed
awhamann opened this issue Jun 7, 2017 · 2 comments
Closed

Errror: Value of count cannot be computed #15172

awhamann opened this issue Jun 7, 2017 · 2 comments

Comments

@awhamann
Copy link

awhamann commented Jun 7, 2017

Issue

Unable to dynamically allocate one or more route table, routes or route associations per subnets. Attempts to do so returns the error:
aws_route_table.route_table: aws_route_table.route_table: value of 'count' cannot be computed

Terraform Version

Terraform v0.9.7

Affected Resource(s)

  • aws_route_table
  • aws_route
  • aws_route_table_association

This may be a core issue as I have seen the behavior with other resources from providers other than AWS.

Terraform Configuration Files

main.tf

# Configure the AWS Provider
provider "aws" {
  access_key = <redacted>
  secret_key = <redacted>
  region     = "us-east-2"
}

variable addressSpace {
   type = "string"
   default = "10.10.11.0/24"
}

variable tags {
   type = "map"
   default = {
      Name = "awhOtherEnv"
      environment = "Anthony's other test environment."
   }
}

variable subnet-cnt {
   type = "string"
   default = "2"
}

resource "aws_vpc" "vpc" {
  cidr_block = "${var.addressSpace}"
  instance_tenancy = "default"
  tags = "${merge(var.tags, map("Name", format("%s-vpc", var.tags["Name"])))}"
}

resource "aws_internet_gateway" "internet-gateway" {
   vpc_id = "${aws_vpc.vpc.id}"
   tags = "${merge(var.tags, map("Name", format("%s-gw", var.tags["Name"])))}"
}

resource "aws_subnet" "subnet" {
   count = "${var.subnet-cnt}"
   vpc_id = "${aws_vpc.vpc.id}"
   cidr_block = "${cidrsubnet(aws_vpc.vpc.cidr_block, ceil(log(var.subnet-cnt,2)), count.index)}"
   tags = "${merge(var.tags, map("Name", format("%s-snet%02d", var.tags["Name"], count.index)))}"
   depends_on = [ "aws_vpc.vpc" ]
}

resource "aws_route_table" "route_table" {
   count = "${length(aws_subnet.subnet.*.id)}" # "${var.subnet-cnt}" #
   vpc_id = "${aws_vpc.vpc.id}"
   tags = "${merge(var.tags, map("Name", format("%s-rtbl%02d", var.tags["Name"], count.index)))}"
   depends_on = [ "aws_subnet.subnet", "aws_vpc.vpc" ]
}

resource "aws_route" "route" {
   count = "${length(aws_route_table.route_table.*.id)}"
   route_table_id = "${element(aws_route_table.route_table.*.id, count.index)}"
   destination_cidr_block = "0.0.0.0/0"
   gateway_id = "${aws_internet_gateway.internet-gateway.id}"
   depends_on = [ "aws_subnet.subnet", "aws_vpc.vpc" ]
}

resource "aws_route_table_association" "route_assn" {
   count = "${length(aws_route_table.route_table.*.id)}"
   route_table_id = "${element(aws_route_table.route_table.*.id, count.index)}"
   subnet_id = "${element(aws_subnet.subnet.*.id, count.index)}"
   depends_on = [ "aws_subnet.subnet", "aws_vpc.vpc" ]
}

Debug Output

terraform.log

Expected Behavior

What should have happened?

A route table should be have been created with one route associated with each subnet.

Actual Behavior

What actually happened?

Error running plan: 1 error(s) occurred:

  • aws_route_table.route_table: aws_route_table.route_table: value of 'count' cannot be computed

Steps to Reproduce

  1. terraform plan

References

#12570

@apparentlymart
Copy link
Contributor

Hi @awhamann! Sorry this isn't behaving as expected.

Currently Terraform isn't able to automatically deal with this situation. Although in principle it should be possible to know the length of the subnets list even though the resources haven't been created yet, currently Terraform treats the entire list (including its length) as unknown until the resources have been created.

To work around this, it's necessary to use -target to create the subnet resources first:

$ terraform plan -target=aws_subnet.subnet -out=tfplan
...
$ terraform apply tfplan
...

In future we hope to make this work as expected through improvements to the configuration language. This issue is already captured in #14677, so I'm going to close this issue just to keep the discussion consolidated over there. Thanks for filing this issue.

@ghost
Copy link

ghost commented Apr 11, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Apr 11, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants