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

Author Derek Morgan

Last updated 2 May, 2023

2 mins read

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

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.






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
5
6
7
8
9
module "finance-bucket" {
    count   = 1
    source  = "./s3"
    service = "S3"
    team    = "Finance"
    extra_tags = {
        contact = "Derek"
    }
}

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
variable "extra_tags" {
    type = map
    default = {}
}

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
> merge({service = "s3", team = "devops"}, {contact = "derek"})
{
  "contact" = "derek"
  "service" = "s3"
  "team" = "devops"
}

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
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
}

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.

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

Manage, track, and report your AWS spending in seconds — not hours

CloudForecast’s focused daily AWS cost monitoring reports to help busy engineering teams understand their AWS costs, rapidly respond to any overspends, and promote opportunities to save costs.

Monitor & Manage AWS Cost in Seconds — Not Hours

CloudForecast makes the tedious work of AWS cost monitoring less tedious.

AWS cost management is easy with CloudForecast

We would love to learn more about the problems you are facing around AWS cost. Connect with us directly and we’ll schedule a time to chat!

AWS daily cost reports
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"
}