docs icon indicating copy to clipboard operation
docs copied to clipboard

Document `value of 'count' cannot be computed` Terraform errors

Open aknysh opened this issue 6 years ago • 4 comments

what

  • Document the error

module.ec2_instance.module.label.null_resource.tags_as_list_of_maps: null_resource.tags_as_list_of_maps: value of 'count' cannot be computed

  • Document Terraform issues with counts in maps

why

Terraform (in the current incarnation) is very sensitive to these two things:

  1. Dynamic counts across modules - when you have a dynamic count (calculated by some expression with input params) in one module and then use the module from other modules
  2. It does not especially like those dynamic counts in maps and lists

Some know issues about that: https://github.com/hashicorp/terraform/issues/13980 https://github.com/hashicorp/terraform/issues/10857 https://github.com/hashicorp/terraform/issues/12570 https://github.com/hashicorp/terraform/issues/17048

I know this issue has been discussed time and again (Ex: #12570) and that if a module has a map variable and has interpolation inside this map variable, count inside a module results in value of ‘count’ cannot be computed. What puzzles me is that this error occurs when terraforming a new environment but not any existing environment!

In our case:

Here the count depends on the map and the input var.tags https://github.com/cloudposse/terraform-null-label/blob/master/main.tf#L23

And here var.tags depends on the map, the other inputs and on the data provider https://github.com/cloudposse/terraform-aws-ec2-instance/blob/master/main.tf#L68

This circular dependency breaks TF.

It’s very difficult to say for sure what’s going on, because it could work in some cases and in some environments, but not in the others. (see the complains above).

I know this is not a good explanation, but they have been discussing the issue for years and can’t explain it eigher. Because nobody understands it.

The fix:

  1. Remove dynamic counts (provide explicit counts if possible)
  2. Or remove maps from counts
  3. Or try to remove the data source (could work in some cases)
  4. apply in stages with -target (not a pretty solution)

aknysh avatar Jun 11 '18 16:06 aknysh

Document possible workaround suggested by @maartenvanderhoef

https://sweetops.slack.com/archives/CB6GHNLG0/p1533834243000311

For terraformers, who like dirty hacks, and who are encountering issues with count.index inside a conditionally created resource with count to length(var.of_a_list).. here's something that worked for me :sunglasses:

create a data "template_file" with count of the length of the list with no conditions.

  count = "${length(var.custom_listen_hosts)}"
...```

And refer to the template_file from the resource with the condition..

```resource "aws_lb_listener_rule" 
"host_based_routing_custom_listen_host" {
....
  count = "${local.create && length(var.custom_listen_hosts) > 0 ? length(var.custom_listen_hosts) : 0 }"

    values = ["${data.template_file.custom_listen_host.*.rendered[count.index]}"]
}```

osterman avatar Aug 22 '18 22:08 osterman

https://docs.cloudposse.com/faq/terraform-value-of-count-cannot-be-computed/

osterman avatar Aug 22 '18 22:08 osterman

https://github.com/hashicorp/terraform/issues/12570#issuecomment-366324566

I was able to workaround my issue by passing around the variable used to define the number of resources I was counting. When using count, all values used in the computation must ultimately come from data sources or from variables that do not ultimately derive from a resource.

osterman avatar Aug 22 '18 22:08 osterman

New issue related to this one: https://github.com/hashicorp/terraform/issues/17421

osterman avatar Dec 27 '18 00:12 osterman