Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ozbillwang/7cf60daad769351c7f152e9b0e08c784 to your computer and use it in GitHub Desktop.
Save ozbillwang/7cf60daad769351c7f152e9b0e08c784 to your computer and use it in GitHub Desktop.
a new way to manage tags with terraform

https://stackoverflow.com/questions/48460316/terraform-take-parameter-name-from-variable

According to this comment, dynamic variable names are not possible at this time in HCL.

You can use zipmap to emulate this, though it's a bit of a clunky workaround;

locals {
  ec2_tag_keys = ["Name", "Group", "${var.vault_tag_name}"]
  ec2_tag_vals = ["${var.name}", "${var.group_tag}", "${var.vault_tag_value}"]
}

resource "aws_instance", "instance" {
  ...
  tags = "${zipmap(local.ec2_tag_keys, local.ec2_tag_vals)}"
}

Result;

+ aws_instance.instance
      tags.%:                       "3"
      tags.Group:                   "MyGroup"
      tags.Name:                    "MyName"
      tags.MyTagName:               "MyTagValue"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment