These examples demonstrate practical, real-world usage patterns for the tagging-policy module. Each example is self-contained and ready to run—simply copy the configuration, customize the values for your environment, and apply.

Getting Started

To run any example, follow these steps:

  1. Authenticate with the registry: terraform login registry.patterneddesigns.ca
  2. Initialize the working directory: terraform init
  3. Review the execution plan: terraform plan
  4. Apply the configuration: terraform apply

Usage Examples

Basic Usage

Minimal configuration for a tagging policy

module "tags" {
  source  = "registry.patterneddesigns.ca/standardnat/tagging-policy/aws"
  version = "1.1.0"

  required_tags = ["Environment", "Owner"]

  default_tags = {
    ManagedBy = "terraform"
  }
}

# Apply to all resources
provider "aws" {
  default_tags {
    tags = module.tags.merged_tags
  }
}

Required Tags

Define comprehensive required tags for compliance

module "tags" {
  source  = "registry.patterneddesigns.ca/standardnat/tagging-policy/aws"
  version = "1.1.0"

  required_tags = [
    "Environment",
    "Owner",
    "CostCenter",
    "Project",
    "Application",
    "DataClassification",
    "Compliance"
  ]

  default_tags = {
    ManagedBy    = "terraform"
    Organization = "acme-corp"
  }
}

# Validate required tags are present
output "required_tag_list" {
  value       = module.tags.required_tag_keys
  description = "Tags that must be provided for all resources"
}

Cost Allocation

Tags optimized for AWS Cost Explorer and billing

module "cost_tags" {
  source  = "registry.patterneddesigns.ca/standardnat/tagging-policy/aws"
  version = "1.1.0"

  required_tags = [
    "CostCenter",
    "Project",
    "Team",
    "Environment"
  ]

  default_tags = {
    ManagedBy  = "terraform"
    BillingOrg = "engineering"
  }

  tag_values = {
    Environment = ["dev", "staging", "prod"]
    CostCenter = [
      "eng-001",
      "platform-002",
      "data-003",
      "ops-004"
    ]
  }

  enforce_lowercase = true
}

# Apply cost allocation tags
provider "aws" {
  default_tags {
    tags = module.cost_tags.merged_tags
  }
}

# Export for AWS Cost Allocation Tags setup
output "cost_allocation_tags" {
  value = [
    "CostCenter",
    "Project",
    "Team"
  ]
  description = "Tags to activate in AWS Cost Allocation Tags"
}

Environment Tags

Per-environment tagging configuration

variable "environment" {
  type        = string
  description = "Deployment environment"
  validation {
    condition     = contains(["dev", "staging", "prod"], var.environment)
    error_message = "Environment must be dev, staging, or prod."
  }
}

locals {
  environment_config = {
    dev = {
      cost_center = "dev-001"
      owner       = "dev-team"
    }
    staging = {
      cost_center = "staging-002"
      owner       = "qa-team"
    }
    prod = {
      cost_center = "prod-003"
      owner       = "platform-team"
    }
  }
}

module "tags" {
  source  = "registry.patterneddesigns.ca/standardnat/tagging-policy/aws"
  version = "1.1.0"

  required_tags = ["Environment", "Owner", "CostCenter"]

  default_tags = {
    ManagedBy   = "terraform"
    Environment = var.environment
    Owner       = local.environment_config[var.environment].owner
    CostCenter  = local.environment_config[var.environment].cost_center
  }

  enforce_lowercase = true
}

provider "aws" {
  default_tags {
    tags = module.tags.merged_tags
  }
}

Multi-Account Setup

Tagging policy for AWS Organizations multi-account

variable "account_name" {
  type        = string
  description = "AWS account name"
}

variable "account_type" {
  type        = string
  description = "Account type (workload, shared, security)"
}

module "org_tags" {
  source  = "registry.patterneddesigns.ca/standardnat/tagging-policy/aws"
  version = "1.1.0"

  required_tags = [
    "Environment",
    "Owner",
    "CostCenter",
    "AccountName",
    "AccountType"
  ]

  default_tags = {
    ManagedBy    = "terraform"
    Organization = "acme-corp"
    AccountName  = var.account_name
    AccountType  = var.account_type
  }

  tag_values = {
    AccountType = ["workload", "shared", "security", "sandbox"]
    Environment = ["dev", "staging", "prod", "shared"]
  }

  enforce_lowercase = true
}

# Apply across all providers
provider "aws" {
  alias  = "primary"
  region = "us-east-1"

  default_tags {
    tags = module.org_tags.merged_tags
  }
}

provider "aws" {
  alias  = "secondary"
  region = "us-west-2"

  default_tags {
    tags = module.org_tags.merged_tags
  }
}