10

Currently, I'm working on a requirement to make Terraform Tags for AWS resources more modular. In this instance, there will be one tag 'Function' that will be unique to each resource and the rest of the tags to be attached will apply to all resources. What I'm trying to do is combine the unique 'Function' value with the other tags for each resource.

Here's what I've got so far:

tags = {
Resource = "Example",
"${var.tags}

This tags value is defined as a map in the variables.tf file like so:

variable "tags" {
type        = map
description = "Tags for infrastructure resources."
}

and populated in the tfvars file with:

tags = {
    "Product"     = "Name",
    "Application" = "App",
    "Owner"       = "Email"
}

When I run TF Plan, however, I'm getting an error:

Expected an attribute value, introduced by an equals sign ("=").

How can variables be combined like this in Terraform? Thanks in advance for your help.

4 Answers 4

14

I tried to use map, it does work with new versions. The lines below works for me:

tags = "${merge(var.resource_tags, {a="bb"})}"
Sign up to request clarification or add additional context in comments.

1 Comment

9

Figured this one out after further testing. Here you go:

tags = "${merge(var.tags, 
                map("Product", "Product Name", 
                    "App", "${var.environment}")
               )
         }"

So, to reiterate: this code will merge a map variable of tags that (in my case) are applicable to many resources with the tag (Product and App) that are unique to each infrastructure resource. Hope this helps someone in the future. Happy Terraforming.

1 Comment

with map function deprecated we would need to use tomap
6

My solution, without map or tomap function (but only type):

variable "tags" {
    default = {}
    type = map(string)
}

And

  tags = merge(
    {Name = "${var.yyyyyy}"},
    var.tags
  )

Comments

1

Creating values in my tfvars file did not work for me... Here is my approach....

I created a separate variable in my variables.tf file to call during the tagging process..

my default variable for tags are imported/pass from a parent module. So therefore it doesnt need to specify any default data. the extra tagging in the child module is done in the sub_tags variable..

imported/passed from parent/root module

variable "tags" {
    type = "map"
}

tags in the child module

variable "sub_tags"{
    type = "map"
    default = {
      Extra_Tags_key = "extra tagging value"
    }
}

in the resource that needs the extra tagging.. i call it like this

 tags   = "${merge(var.tags, var.sub_tags)}"

this worked great for me

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.