core: Specialized errors for incorrect indexes in resource reference #19727
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In prior versions of Terraform we permitted inconsistent use of indexes in resource references, but in as of 0.12 the index usage must correlate properly with whether
count
is set on the resource.Since users are likely to have existing configurations with incorrect usage, here we introduce some specialized error messages for situations where we can detect such issues statically. This seems to cover all of the common patterns we've seen in practice.
Some usage patterns will fall back on a less-helpful dynamic error here, but no configurations coming from 0.11 can end up that way because 0.11 did not permit forms such as
aws_instance.no_count[count.index].bar
that this validation would not be able to "see".Our configuration upgrade tool also contains a fix for this already, but it takes a more conservative approach of adding the index
[0]
rather than[count.index]
because it can't be sure (without human help) if correlation of indices is what was intended.This addresses #19216 in two ways:
terraform apply
in that example, whereas in alpha1 we'd catch it only after the resources were initially created and so we could see the incorrect "each mode" in state.