Provider Default Tags Support
Terratags integrates with AWS provider's default_tags
feature. When you define default tags in your AWS provider configuration, Terratags will recognize these tags and consider them when validating resources.
How Default Tags Work
- Tags defined in the AWS provider's
default_tags
block are automatically applied to all taggable resources created by that provider - Terratags tracks tag inheritance from provider default_tags to individual resources
- Resources only need to specify tags not covered by default_tags
- Default tags can be overridden at the resource level if needed
Example with Default Tags
provider "aws" {
region = "us-west-2"
default_tags {
tags = {
Environment = "dev"
Owner = "team-a"
Project = "demo"
}
}
}
resource "aws_instance" "example" {
ami = "ami-12345678"
instance_type = "t2.micro"
# Only need to specify Name tag, as other required tags come from default_tags
tags = {
Name = "example-instance"
}
}
In this example, the AWS instance will have all four required tags: Name
from the resource-level tags, and Environment
, Owner
, and Project
from the provider's default_tags.
Benefits of Using Default Tags
- Consistency: Ensures consistent tagging across all resources
- Reduced Duplication: Eliminates the need to repeat the same tags on every resource
- Centralized Management: Makes it easier to update tags across all resources
- Reduced Errors: Minimizes the chance of missing required tags
Default Tags Limitations
- Provider Specific: Only works with providers that support default_tags (like AWS)
- Override Behavior: Resource-level tags override default tags with the same key
- Module Awareness: When using modules, be aware of how default tags propagate
Best Practices
- Use for Common Tags: Use default_tags for tags that should be consistent across all resources
- Resource-Specific Tags: Use resource-level tags for tags that are specific to individual resources
- Documentation: Document which tags are provided by default_tags to avoid confusion
- Validation: Still use Terratags to validate that all required tags are present