Terraform: Variable Interpolation In Tag Keys

Created on 13 Jul 2015  ยท  5Comments  ยท  Source: hashicorp/terraform

Is it possible to get variable interpolation in tag keys?

Something like this:

 tags { 
    "${var.global_tag_key}" = "${var.global_tag_value}" 
  } 

When I try it now, it just seems to set the tag key to the literal string ${var.global_tag_key}, instead of of interpolating it.

Is this intended behavior? If so, would you accept a PR that changes this behavior to allow interpolation inside tag keys?

core enhancement

Most helpful comment

Would love to see this working!
Or something similar to the way auto scaling group handles tag would be great.

All 5 comments

Hi @saulshanabrook -

I can see this being useful. it might be nontrivial to implement, but you're welcome to take a stab at a PR if you like!

@phinze OK makes sense, I will take a look

Would love to see this working!
Or something similar to the way auto scaling group handles tag would be great.

Hi @saulshanabrook! Thanks for opening this and sorry for the long silence.

This seems to be the same as what was suggested in #2042, which I just closed with a comment explaining the current suggested way to achieve this.

To add some more color here, since you were asking about whether we'd accept a PR for interpolation inside keys in particular: in general we prefer the overall configuration structure to be "static", since this enables us to catch a variety of interesting errors during a validation pass that can happen before any interpolations are resolved. However, attributes that are defined as being maps are a special case because their keys truly are dynamic, and can (as my comment in the other issue shows) be set dynamically via the interpolation language.

So it's likely that in future we will make some improvements to HCL/HIL syntaxes to make such a construct more natural, but we may approach it in a different way than simply allowing interpolations generally in keys so that structured configuration elements (as opposed to maps) can still be reliably validated.

I'm going to close this out because the use-case is now served, albeit in a sub-optimal way, using the map function. When we make improvements to the language in future they will likely be more holistic changes, which will have their own issues/PRs as appropriate. Thanks again!

I'm going to lock this issue because it has been closed for _30 days_ โณ. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

c4milo picture c4milo  ยท  3Comments

rjinski picture rjinski  ยท  3Comments

larstobi picture larstobi  ยท  3Comments

franklinwise picture franklinwise  ยท  3Comments

rnowosielski picture rnowosielski  ยท  3Comments