Interpolation-only expression warning for key variable in for loop

Hello

I am attempting an upgrade of TF from 0.12 to 0.13. I am receiving

Interpolation-only expressions are deprecated

warnings but do not know how else to use the syntax in a for loop.

Example:

locals {
    recordSet_loop = flatten([
    for key, value in var.private_dns_record_sets : {
      "${key}" = {
        rrdatas = value.rrdatas
      }
    }
  ])

I tried removing the “${ and }” from key but that merely sets the object key to the word “key” instead of expanding the key in the for loop. I may be missing something very simple but my Google fu is failing me.

Thanks in advance!

Hi @shanewilliamsontouch!

It seems like this warning has got a bit over-zealous in 0.13 because disambiguating a dynamic key in an object constructor is a reasonable thing to do with the interpolation syntax.

Another thing that ought to work is wrapping the expression in parentheses, like (key) = ..., but I must admit it’s been long enough since I was looking at the code for this that I’m not 100% sure I’m remembering this properly.

The general idea here is that anything that isn’t just a bare identifier should be understood as an expression, so there are various other permutations you can use to make that be true, like tostring(key), join("", [key]), etc… but I’m suggesting these only as workarounds in case the bare parentheses don’t work; if that doesn’t work then I’d like to treat that as a bug to be fixed.

1 Like

Thank you @apparentlymart!

Wrapping the expressions in () seems to have worked. The expressions are expanding as they should and the warnings have stopped.

I appreciate your help!