Terraform 9: Your first resource — azurerm_resource_group
easy⏱ 5 mincourseterraform
Anatomy of a resource block
A resource block has two labels: the type (azurerm_resource_group) and a local name (orders) you choose. The local name is how you reference this resource elsewhere in the config — it never appears in Azure. Inside, arguments like name, location, and tags describe the desired resource.
resource "azurerm_resource_group" "orders" {
name = "myorg-orders-dev-rg"
location = "westus2"
tags = {
environment = "dev"
service = "orders"
managed_by = "terraform"
}
}
Consistent naming is a convention, not a feature
Terraform will not name things for you. Teams adopt a scheme like {org}-{service}-{env}-{type} and encode it once — usually in locals — so every resource name is predictable. A stable convention makes resources searchable in the portal and prevents collisions across environments.
locals {
org = "myorg"
service = "orders"
env = "dev"
prefix = "${local.org}-${local.service}-${local.env}"
}
resource "azurerm_resource_group" "orders" {
name = "${local.prefix}-rg"
location = "westus2"
}
Renaming forces a replace
The name argument of a resource group is immutable — changing it makes Terraform plan a destroy-and-recreate, not an in-place edit. Decide your naming scheme up front. Changing only the local name (the second label) instead moves the resource in state and also triggers a destroy/create unless you terraform state mv it.
Build a standardized resource-group name
Write rgName(org: string, service: string, env: string) that returns "{org}-{service}-{env}-rg", lowercased. Log rgName("myorg", "orders", "dev") — it must print myorg-orders-dev-rg.