Derek Morgan
Derek Morgan Industrial IoT engineer and course creator for More Than Certified. His Terraform course on Udemy has over 10,000+ students to date.

Part 4: Using Terraform to Add Arbitrary Tags to our Existing AWS Tags

Part 4: Using Terraform to Add Arbitrary Tags to our Existing AWS Tags

In this video, we’re going to utilize the terraform merge function and a new extra_tags variable to enable us to be able to add arbitrary tags to our required existing tags. If you haven’t completed the previous post, you can always clone the code from the Github link here.

Add Extra Tags to the Module

What we’re going to do is add another variable to our modules that allows for extra_tags. This will be a map of tags that we can merge to form the full tag block. Let’s go ahead and add that to the finance bucket.

1
2
3
4
5
6
7
8
9
module "finance-bucket" {
    count   = 1
    source  = "./s3"
    service = "S3"
    team    = "Finance"
    extra_tags = {
        contact = "Derek"
    }
}

Initialize the extra_tags Variable in s3-bucket/variables.tf

Alright, perfect. Now that we’ve done that, let’s initilize the variable within s3-bucket/variables.tf

1
2
3
4
variable "extra_tags" {
    type = map
    default = {}
}

Experiment with the Merge Function in the Console

Now that we have our tags block and our extra tags block, we need to merge the two to create our full tags block. To do that, we can use the merge() function. This will merge the two maps and create one block of variables! Run terraform console and let’s try it:

1
2
3
4
5
6
> merge({service = "s3", team = "devops"}, {contact = "derek"})
{
  "contact" = "derek"
  "service" = "s3"
  "team" = "devops"
}

Merge our Maps Using a Locals Block

Neat! That seamlessly combined our two maps. Now, we just need a way to do it automatically. Unfortunately, Terraform does not like referencing variables within other variables, so we’re going to utilize a locals map.

Within s3-bucket/main.tf, let’s create a locals block that allows us to merge our tags, then we’ll just reference that single value:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
locals {
    tags = merge({
        service = var.service
        team = var.team
    }, var.extra_tags)
}

resource "random_id" "s3_id" {
    byte_length = 2
}

resource "aws_s3_bucket" "team-bucket" {
    bucket = "${lower(var.team)}-bucket-${random_id.s3_id.dec}"

    tags = local.tags
}

As you can see, we’ve created the locals block. This allows us to create a map of our existing tags, service and team, and merge that with the map of our var.extra_tags. This will produce one single block of tags that will be added to our resources. Let’s run a terraform apply -auto-approve and verify that it works:

1
2
3
4
5
6
7
8
9
10
11
+ tags                                 = {
    + "contact" = "derek"
    + "service" = "s3"
    + "team"    = "finance"
}
+ tags_all                             = {
    + "contact" = "derek"
    + "env"     = "dev"
    + "service" = "s3"
    + "team"    = "finance"
}

Nice! It looks like the tags were merged successfully!

Conclusion

We have now added functionality to our deployment to enable us to add an arbitrary block of tags to our deployment.