Terraform 30: 3: A naming + tagging convention (env-service-type)
easy⏱ 5 mincourseterraform
Name by env-service-type
A predictable name tells you at a glance what a resource is and where it lives. The pattern {env}-{service}-{type} yields names like dev-orders-rg, prod-billing-kv, uat-widgets-sa. Build names in one place (a local or a module) so every resource is consistent.
# variables.tf
variable "environment" { type = string } # dev | uat | prod
variable "service" { type = string } # orders | billing | widgets
# main.tf — build the name prefix once
locals {
name_prefix = "${var.environment}-${var.service}"
}
resource "azurerm_resource_group" "this" {
name = "${local.name_prefix}-rg" # dev-orders-rg
location = var.location
}
One shared tag map
Define common tags once in a local and spread them onto every resource with tags = local.common_tags. Tags power cost reports, ownership, and automated cleanup. Always include at least environment, service, and managed_by = "terraform".
locals {
common_tags = {
environment = var.environment
service = var.service
managed_by = "terraform"
cost_center = var.cost_center
}
}
resource "azurerm_storage_account" "this" {
name = "${var.environment}${var.service}sa" # no dashes allowed
resource_group_name = azurerm_resource_group.this.name
location = var.location
account_tier = "Standard"
account_replication_type = "LRS"
tags = local.common_tags
}
Mind per-resource name rules
Some resources reject dashes or have tight length limits — storage account names are lowercase alphanumeric, 3-24 chars, no hyphens. Keep your generic scheme but strip separators where the provider demands it, e.g. devorderssa instead of dev-orders-sa.
Build a standardized name
Write resourceName(env, service, type) returning "{env}-{service}-{type}". Log resourceName("prod", "billing", "kv") — it must print prod-billing-kv.