aws_autoscaling_group "interpolated tags" example contains non-standard usage that won't upgrade well to 0.12 #7655
Labels
breaking-change
Introduces a breaking change in current functionality; usually deferred to the next major release.
documentation
Introduces or discusses updates to documentation.
service/ec2
Issues and PRs that pertain to the ec2 service.
Milestone
Community Note
Description
The documentation for
aws_autoscaling_group
currently includes an "Interpolated tags" example, as follows:I understand from the history that this
tags
argument (defined as a list of maps attribute, rather than as a nested block) was added alongsidetag
to allow for this sort of dynamic construction. The example above is relying on the accidental flexibility that resulted from the lossy nature of configuration decoding in Terraform 0.11, where defining the same attribute more than once ended up being treated the same as defining a nested block more than once. Such usage is no longer allowed in Terraform 0.12, where we allow each argument to be defined only once.A better way to write this for Terraform 0.11 would be to show only a single dynamic attribute definition with all of the elements together in the same expression, which I think is also subjectively clearer because it doesn't leave the unfamiliar reader wondering about the meaning of a repeated definition of the same argument:
Because the Terraform v0.12 upgrade tool has a requirement of preserving existing declaration order and comments it will not combine these two arguments together and will instead just migrate both of them and produce an error after upgrade. I think that the error that results is the best we can do to handle this unusual case for existing users, but I'm hoping we can also update the documentation to reflect a usage pattern that will migrate correctly, such as the above.
We might also consider in this particular case having separate examples for Terraform 0.11 and 0.12, because I think the idiomatic way to write this for 0.12 would be to stick to using the
tag
nested block type and ignore thetags
attribute altogether:Affected Resources
aws_autoscaling_group
Additional Context
The following is the result from upgrading the example exactly as given in the documentation using
terraform 0.12upgrade
:As noted above, it produces two definitions of
tags =
because of the requirement to maintain declaration order and any attached comments, even though a subsequentterraform validate
would detect that second definition as an error.This shows another interesting quirk of this particular example where the upgrade tool is unable to definitively prove whether or not the expression given in
tags
is an example of the legacy usage pattern of wrapping a list expression in a redundant set of list brackets, and so it generates aTF-UPGRADE-TODO
to prompt the user to decide. In this case it looks like it would be necessary to remove that extra level of brackets to make this work. The upgrade tool is able to make a decision on this in most normal cases, but this one is a little too complicated so it takes the safe path of prompting the user to review it.Interestingly, writing the original example using the
tag
block type rather than thetags
attribute -- while retaining everything else unchanged -- produces a more complete result because the upgrade tool can recognize it as a usage of the pre-0.12 hack of treating a block type as if it were an attribute:This result is not perfect by any means either... it includes more than strictly necessary in the
dynamic
block because it is being conservative about maintaining what it considers to be a dynamic expression. But this would, at least, be parsed as expected by a subsequentterraform validate
since the multiple definitions are of distincttag
blocks rather than thetags
attribute.Since the existing example will undoubtedly already have been used as the basis for real configurations the difference above is pretty academic and not something we can really do anything about now, but I thought it was interesting additional context to see exactly how the upgrade tool is understanding this right now.
The text was updated successfully, but these errors were encountered: