Mastering Terraform Tags Like a True IT Queen
By Tatiana Mikhaleva Senior Developer Advocate · Docker Captain · CNCF Ambassador · IBM Champion
Originally published on my earlier blog.
Hey girls!
If you’re as obsessed with Infrastructure as Code (IaC) as I am, then you know that Terraform is THE tool to have in your IT arsenal. And if you’re working with cloud resources, you’ve probably heard about tags - those little key-value labels that help you organize, track expenses, and automate processes.
But let’s be honest: tags can sometimes feel like a necessary evil, especially when you’re managing multiple cloud environments.
Don’t worry, darling! Here’s the rundown on Terraform tags: why you need them, how to use them, and the best practices to follow. So grab your favorite coffee ☕ and let’s go!
Vladimir Mikhalev covered Terraform tags earlier: https://heyvaldemar.com/mastering-terraform-tags-for-infrastructure-excellence/
What Are Terraform Tags?
Think of Terraform tags as little stickers you can attach to cloud resources to keep things organized and manageable. They are key-value pairs that allow you to classify resources by purpose, environment, or owner.
📌 Example:
tags = { Environment = "Production" Owner = "IT Girl Dev Team" Project = "CoolNewApp"}💡 Why do you need this? Because proper tagging makes resource management easier, helps automate processes, and controls access. Plus, it keeps your infrastructure structured and aesthetic - and we don’t want a messy cloud, do we? ❌
Tags vs. tags_all: What’s the Difference?
Terraform has two types of tags: tags and tags_all. They sound similar but serve different purposes:
- tags - The tags you define on the resource in your Terraform code.
- tags_all - A computed, read-only map in the AWS provider: your resource
tagsplus everything inherited from the provider’sdefault_tagsblock. You never set it yourself.
📌 Example (reading all tags of an AWS resource):
aws_vpc.example.tags_allNot every provider has it: azurerm resources, for example, have tags only.
Why Should You Use Tags?
Tags aren’t just for aesthetics (although we love that too 💅). Here are some key reasons why tagging is essential:
Organization
Imagine having thousands of resources in your AWS account without tags. Total chaos, right? Tags help group resources by environment, project, or team.
📌 Example:
tags = { env = "staging" owner = "DevOps Team" purpose = "testing"}Cost Management
Cloud bills can be terrifying 😱, but Terraform tags help you track where your money is going.
📌 Example:
tags = { cost_center = "Marketing Analytics"}Automation
You know how much I love automation! Tags allow you to manage deployments, shut down unnecessary resources, and set up backups.
📌 Example:
tags = { auto_shutdown = "true" deploy = "true"}Access Control
You can configure tags to restrict access to resources using AWS IAM policies.
📌 Example:
tags = { Environment = "Development"}How to Add Tags to Terraform Resources
It’s super simple! Just use the tags attribute inside the resource block.
📌 Example:
resource "aws_instance" "my_instance" { tags = { Environment = "Production" }}Want to Add Multiple Tags? Easy!
tags = { Environment = "Development" Owner = "Leia Organa" Department = "Rebel Alliance"}What Are Terraform Default Tags?
Default tags are set once in the AWS provider and applied to every resource it creates, so you don’t repeat them in every block.
📌 Example:
provider "aws" { default_tags { tags = { Environment = "Development" Owner = "IT Girl Dev Team" } }}How to Override Default Tags?
Set the tag on the resource. When the same key is in both places, the resource’s own value wins, and tags_all shows the result.
📌 Example:
resource "aws_instance" "my_instance" { tags = { Name = "MyAwesomeServer" Environment = "Production" }}For providers without default_tags, keep a shared map in a variable and combine it with merge(var.common_tags, { Name = "MyAwesomeServer" }).
How to Ignore Tag Changes in Terraform?
If external systems modify your tags (Azure Databricks, I see you 👀), tell Terraform to ignore them!
📌 Example:
resource "aws_instance" "my_instance" { lifecycle { ignore_changes = [tags] }}Or ignore them at the provider level:
provider "aws" { ignore_tags { keys = ["CostCenter"] }}Best Practices for Terraform Tags
💡 Tips to keep your cloud tagging strategy clean and effective:
✔ Be consistent 👑 - Use the same key names across resources.
✔ Document your tags 📚 - Define what each tag means.
✔ Automate tagging ⚙️ - Use Terraform modules.
✔ Use Default Tags ✅ - Follow the DRY (Don’t Repeat Yourself) principle.
Conclusion
Terraform tags may seem small, but they make a huge difference in cloud resource management. Start using them correctly, and your future IT-self will thank you!
Share your favorite Terraform hacks in the comments or on Twitter! 💬💻
Until next time, keep slaying the IT world! 🚀💖
Tatiana Mikhaleva
Docker CaptainCNCF AmbassadorIBM Champion
DevOps.Pink — Signal over noise in cloud-native & AI.
I'm a Docker Captain, CNCF Ambassador, IBM Champion, AWS Community Builder and a GitKraken, Cypress, Notion and Platform Engineering ambassador. Vendors don't review my posts; sponsored posts say so at the top. Referral links are labeled as such and earn me a commission.