Define Common Tags as a map(string) Variable

Create a Terraform map(string) variable for resource tags applied consistently across all infrastructure resources.

Common Patterns

Detailed Explanation

Common Tags Map Variable

Consistent resource tagging is critical for cost allocation, compliance, and operations. A map(string) variable for common tags ensures every resource gets the same baseline tags.

Variable Definition

variable "tags" {
  type        = map(string)
  description = "Common tags applied to all resources"
  default = {
    Environment = "dev"
    ManagedBy   = "terraform"
  }
}

Extended Tags Pattern

variable "tags" {
  type        = map(string)
  description = "Common tags applied to all resources"
  default = {
    Environment = "dev"
    ManagedBy   = "terraform"
    Project     = "my-project"
    Team        = "platform"
    CostCenter  = "engineering"
  }
}

Merging with Resource-Specific Tags

resource "aws_instance" "web" {
  ami           = var.ami_id
  instance_type = var.instance_type

  tags = merge(var.tags, {
    Name = "web-server"
    Role = "frontend"
  })
}

The merge() function combines the common tags with resource-specific tags, where resource-specific values override common ones if keys collide.

Why map(string)?

  • Flexible: Any number of key-value pairs without changing the type definition
  • Mergeable: Works naturally with Terraform's merge() function
  • AWS compatible: Maps directly to AWS resource tags
  • Simple: No nested structures needed for flat tag maps

Organization Tagging Standards

Many organizations require specific tags for compliance:

Tag Purpose Example
Environment Cost separation dev, staging, prod
ManagedBy Track IaC vs manual terraform
Project Cost allocation my-project
Team Ownership platform-engineering
CostCenter Finance tracking CC-1234
DataClassification Compliance internal, confidential

Use Case

Enterprise environments where every AWS resource must carry standardized tags for cost allocation, compliance auditing, and operational visibility.

Try It — Terraform Variable Generator

Open full tool