count.index and count.indexes limits on scalar and tuple arguments
0 reputation · 14 Feb 2020, 14:03 UTC
0 reputation · 14 Feb 2020, 14:03 UTC
Clarifying the documented limits for count.index and count.indexes is needed for a module design that relies on count to iterate over inputs of varying types.
The relevant capability is the use of count.index for list and set iterations and count.indexes for map iterations in Terraform 1.9+. The behavior is documented for collection-typed arguments, with uncertainty about how scalar or tuple arguments are handled and whether the error is InvalidArgument or a silent empty result.
What argument types are officially supported for count.index and count.indexes? Does count.indexes provide different key handling for maps compared with list indexing? What is the documented limit when a count expression references a scalar or tuple argument?
Terraform’s count.index is available only when the count argument resolves to a number. The number is most commonly produced by applying length to a list, set, or tuple. When the source is a scalar (string, number, bool) or a raw tuple that Terraform does not treat as a list/set, the configuration fails validation with an InvalidArgument error; Terraform does not silently produce an empty result.
count (and thus count.index)length(var.my_list), length(var.my_set), length(var.my_tuple).size on a map or object is also a number, but using count with a map size is discouraged; prefer for_each for map iteration.A tuple value is treated as a list‑like collection for the purpose of length. Therefore count = length(var.my_tuple) works exactly like it does for a list, and count.index yields the same zero‑based sequence (0, 1, 2, …) regardless of the tuple’s element types.
If you attempt to set count = var.scalar where var.scalar is a string, bool, or a number that is not explicitly converted (e.g. you forget to wrap it in length), Terraform’s type checker reports:
Error: Invalid value for "count" argument: required a number.This is an
InvalidArgument error during validation; no resources are created and count.index is never accessed.
count.indexesTerraform does not provide a count.indexes attribute. To iterate over a map or object you must use for_each, which exposes each.key and each.value. Using count with the map’s length loses the original keys and is not the idiomatic way to handle map data.
list_var, set_var, tuple_var, scalar_var, map_var.count = length(var.list_var) (or set/ tuple). Inside the resource output count.index and run terraform init && terraform plan – you should see a sequence of indices matching the collection size.count = var.scalar_var (without length). Run terraform plan and observe the InvalidArgument error.count.index values are referenced.Use comments to ask for clarification. Post a solution as an answer.
4,150 reputation · 14 Feb 2020, 23:47 UTC
To build on the previous answer, it is critical to distinguish between the count meta-argument and for_each when dealing with non-scalar types. While count can be driven by the length() of a map, count.index only provides an integer. This forces a reliance on keys(var.map)[count.index] to retrieve the actual map key, which is computationally inefficient for large sets.
When verifying behavior in Terraform 1.9+, note that while tuples and lists both support count.index, they differ in type constraints. A tuple with mixed types (e.g., [1, "a", true]) will still work with count = length(var.tuple), but any attribute assigned using var.tuple[count.index] must be able to handle those varying types or use a can()/try() block to avoid type mismatch errors during the apply phase.