Move terraform.tfstate from local disk to an Azure Storage backend to make the setup:
- team-ready
- CI-ready
- lock-safe (Azure Blob lease)
Workdir:
infra/workload/01-local-state
Backend values (from Step 01):
- RG:
rg-tfstate-runbook - Storage:
sttfstate260221me01 - Container:
tfstate - Key:
runbook.tfstate
File: infra/workload/01-local-state/backend.tf
terraform {
backend "azurerm" {
resource_group_name = "rg-tfstate-runbook"
storage_account_name = "sttfstate260221me01"
container_name = "tfstate"
key = "runbook.tfstate"
}
}Verify file exists:
Get-ChildItem
type .\backend.tfTerraform CLI does not allow -reconfigure and -migrate-state together.
terraform init -reconfigureterraform init -migrate-stateConfirm with yes when prompted
az storage blob exists `
--account-name sttfstate260221me01 `
--container-name tfstate `
--name runbook.tfstate `
--auth-mode login `
-o jsonExpected:
{ "exists": true }terraform state pull | Select-Object -First 10
terraform planExpected:
-
state pull returns JSON (remote state content)
-
plan shows No changes
--
Remove-Item -Force .\terraform.tfstate, .\terraform.tfstate.backup -ErrorAction SilentlyContinueVerify:
Get-ChildItem -ForceExpected:
-
no terraform.tfstate anymore
-
.terraform/ and .terraform.lock.hcl remain
Before proceeding to Step 04:
-
runbook.tfstate exists in Azure Blob container tfstate
-
terraform state pull works
-
local terraform.tfstate removed