Terraform “Value of count cannot be computed” workaround
Terraform currently has some noisy limitations. I figured out how I could at least workaround one of them…

When building a Cloud Infrastructure with Terraform you want to put everything in Terraform-Modules.
Terraform Modules are simply a blueprint to create resources with Terraform.
When it comes down to using those modules I hit one of these mentioned limitations by Terraform: “value of ‘count’ cannot be computed.”
Lets say we have the givven scenario:
The Scenario:
Module A creates ECS or EC2 instances and outputs a list of IPs for one or multiple instances.
That means we have a dependency between those Terraform-Modules.
Module B creates DNS-Entries based on the output of Terraform Module A of those instance ips.
The Terraform Module dummy_module
Initial Module B
variables.tf
variable "number_dns_entries" {}variable "dns_entries" {
type = "list"
}
main.tf
- will show the “value of ‘count’ cannot be computed” issue
resource "null_resource" "any_value" {count = "${length(var.dns_entries)}"provisioner "local-exec" {
# Bootstrap script called with private_ip of each node in the clutser
command = "echo ${length(var.dns_entries)}"
}
}
output.tf
- will print an the real value of the length of
dns_entries
output "dummy_output" {
value = "${length(var.dns_entries)}"
}
The Terraform Module call:
module "dummy_module" {
source = "modules/dummy"
dns_entries = [
{ direction = "ingress" ethertype = "IPv4" protocol = "tcp" port_range_min = "443" port_range_max = "443" remote_ip_cidr = "${module.ecs_name.ecs_instances_priv_ip_output}/32" },
]
}
Dependency:
ECS / EC2 outputs its IPs — > Module B creates a DNS-Record using this IPs.
What we want to create is the following, a DNS-Record which uses all IPs from the Output (list) of Module A to create something like this:
dummyname A 10.16.30.31
A 10.16.30.32
A 10.16.30.33
The Problem:
Terraform seams to evaluate functions like “count” before its terraform plan
or terraform apply
runs.
null_resource.any_value: value of ‘count’ cannot be computed.
After searching around for a day or two I found multiple solutions which are error prone and requires manual action..
Anyways till Terraform 0.12 there will be no choice but to do it by this way..
One of them which I now use is the following:
The number_of_dns_entries
must match the length of the list of maps.
In this case I’ve two entries and therefore a length
if 2.
The new Module call:
module "dummy_module" {
source = "modules/dummy" # ------------------------------
# THIS MUST BE SET MANUALLY WHEN CHANGING NUMBER OF SEC GROUPS
number_dns_entries = "2"
# ------------------------------ dns_entries = [ { record_name = "dummyname" record_ip = "${module.ecs_name.ecs_instances_priv_ip_output}" },
{ record_name = "dummyname" record_ip = "${module.another_ecs_name.ecs_instances_priv_ip_output}" }, ]
}
The new variables.tf
variable "number_dns_entries" {}variable "dns_entries" {
type = "list"
}
The new main.tf
resource "null_resource" "any_value" {count = "${var.number_dns_entries}"provisioner "local-exec" {
# Bootstrap script called with private_ip of each node in the clutser
command = "echo ${length(var.dns_entries)}"
}
}
The new output.tf
output "dummy_output" {
value = "${length(var.dns_entries)}"
}output "dummy2_output" {
value = "${var.number_dns_entries}"
}
I hope this helps!
Cheers