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

Document value of 'count' cannot be computed Terraform errors #131

Closed
aknysh opened this issue Jun 11, 2018 · 4 comments
Closed

Document value of 'count' cannot be computed Terraform errors #131

aknysh opened this issue Jun 11, 2018 · 4 comments
Assignees

Comments

@aknysh
Copy link
Member

aknysh commented Jun 11, 2018

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:
hashicorp/terraform#13980
hashicorp/terraform#10857
hashicorp/terraform#12570
hashicorp/terraform#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 aknysh self-assigned this Jun 11, 2018
@aknysh aknysh changed the title Document Document value of 'count' cannot be computed Terraform errors Jun 11, 2018
@osterman osterman mentioned this issue Jul 12, 2018
@osterman osterman reopened this Aug 22, 2018
@osterman
Copy link
Member

osterman commented Aug 22, 2018

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 😎

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
Copy link
Member

@osterman
Copy link
Member

hashicorp/terraform#12570 (comment)

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
Copy link
Member

New issue related to this one:
hashicorp/terraform#17421

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants