Replies: 1 comment
-
This proposal is very hard to follow because of invalid syntax in the examples. An ideal example is minimal and runnable. That means no indirection through That said, no plans to implement anything like this, but converting to a discussion for any future comments. TFLint can only enforce things that can be determined statically. The issue here arises when Separately, this ruleset is focused on automating guidance from the Terraform documentation, and not trying to detect potential footguns. |
Beta Was this translation helpful? Give feedback.
-
Introduction
Resources can be created with
for_each
using aset()
ormap()
A resource created with
for_each
with aset()
uses thevalue
A resource created with
for_each
with amap()
uses thekey
If we have resources, and attempt to use the resulting attributes witihin a
for_each
, it will result in an error if the resource has not yet been createdThis issue goes unnotticed if a module is within development, and hasn't been tested on an empty workspace
Example:
The above would work, if
resource.thingAssociation
was added afterresource.thing1
andresource.thing2
were craetedHowever, it can't be deployed on a new workspace, because the
for_each
uses thevalue
of theset()
to determine the new reference/id that will be stored in the Terraform StateSo we get the error:
But, if a
map()
is used, eg:Example:
It will now use the
key
for the determine the new reference/id that will be stored in the Terraform State - which is staticProposal
Warn against the use of resource attributes within
for_each
, unless they are used as amap("thing" = *.id)
rather thanset[*.id]
Beta Was this translation helpful? Give feedback.
All reactions