0
votes

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.

3

3 Answers

2
votes

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
votes

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

0
votes

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

tags = "${merge(var.resource_tags, {a="bb"})}"