az login
Contributor access
Storage account for backend (created in State Management topic)
Write Terraform config that uses terraform.workspace to drive environment-specific sizing and naming.
1 terraform { 2 required_providers { 3 azurerm = { 4 source = "hashicorp/azurerm" 5 version = "~> 3.110" 6 } 7 } 8 # For team use: add azurerm backend config 9 } 10 11 provider "azurerm" { 12 features {} 13 } 14 15 # ── Workspace-specific configuration ── 16 locals { 17 workspace_config = { 18 dev = { 19 sku = "B1" 20 min_instances = 1 21 max_instances = 2 22 } 23 staging = { 24 sku = "P1v3" 25 min_instances = 1 26 max_instances = 3 27 } 28 prod = { 29 sku = "P3v3" 30 min_instances = 3 31 max_instances = 10 32 } 33 } 34 35 # This will error at plan time if workspace name is not in the map 36 config = local.workspace_config[terraform.workspace] 37 } 38 39 resource "azurerm_resource_group" "main" { 40 name = "rg-${terraform.workspace}" 41 location = "eastus" 42 tags = { environment = terraform.workspace } 43 } 44 45 resource "azurerm_service_plan" "plan" { 46 name = "plan-${terraform.workspace}" 47 resource_group_name = azurerm_resource_group.main.name 48 location = azurerm_resource_group.main.location 49 os_type = "Linux" 50 sku_name = local.config.sku 51 } 52 53 resource "azurerm_linux_web_app" "app" { 54 name = "app-${terraform.workspace}-${random_id.suffix.hex}" 55 resource_group_name = azurerm_resource_group.main.name 56 location = azurerm_resource_group.main.location 57 service_plan_id = azurerm_service_plan.plan.id 58 59 site_config {} 60 61 app_settings = { 62 ENVIRONMENT = terraform.workspace 63 } 64 } 65 66 resource "random_id" "suffix" { 67 byte_length = 4 68 } 69 70 output "app_url" { 71 value = "https://${azurerm_linux_web_app.app.default_hostname}" 72 }
main.tf with workspace-aware locals and resource names
Sign in to share your feedback and join the discussion.