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…

Image for post
Image for post
Picture by linagora.com

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:

Image for post
Image for post
Photo by Daria Nepriakhina on Unsplash

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}"
}
Image for post
Image for post
Photo by Matt Botsford on Unsplash

I hope this helps!

Cheers

Working as a IT-Operations engineer at NeXenio, a spin-off by Hasso-Plattner-Institute for products around a digitial workspace.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store