Skip to main content
In CI/CD pipelines, Terraform must store its state remotely. Local state files (for example, terraform.tfstate) cannot persist across ephemeral build agents such as ubuntu-latest, because each pipeline run creates and then destroys a fresh VM or container. If state were stored locally, subsequent pipeline runs would not see the existing infrastructure state — leading Terraform to recreate resources or fail with inconsistent state. This is risky for production environments and teams collaborating on the same infrastructure. Below is a representative Azure DevOps pipeline snippet showing how to initialize Terraform with an Azure Storage backend. The task configures the AzureRM provider, runs the init command, and passes the service connection together with the storage account, container, and key to use for remote state:
This configuration instructs Terraform to store state in Azure Blob Storage rather than on the agent disk. The pipeline authenticates using the configured service connection (backendServiceArm) and connects to the storage account to read, write, and lock the remote state file. When terraform init runs in the pipeline: Why state locking matters
  • CI/CD pipelines are ephemeral and multiple runs (or multiple engineers) can trigger concurrent Terraform applies.
  • Without locking, simultaneous applies can corrupt state or leave resources in an inconsistent state.
  • The Azure Storage backend leverages blob leasing so only one apply can modify the state at any time.
Never rely on ephemeral build agent disks to persist your Terraform state. Use a remote backend with locking to avoid state corruption and accidental resource recreation.
Typical backend setup in Azure
  • Create a dedicated storage account for Terraform state and a private blob container (no public access).
  • Limit access with RBAC so only the pipeline identity (service connection) or specific service principals have permissions.
  • Enable blob versioning on the storage account to help recover from accidental or corrupted state updates.
  • For multiple environments (dev/test/prod), isolate state by using separate containers, keys, or storage accounts — consider one backend per environment/workspace.
Recommended RBAC roles Authentication approaches Configuring access in the Azure portal
  • Identify the storage account you want to use as the Terraform backend.
The image shows a Microsoft Azure portal interface displaying a storage account named "lifecyclestorage75636" with an open blob container titled "reports." The container has a private anonymous access level and is available for lease.
  • Verify the identity that the pipeline will use (for example, an application registration / service principal or managed identity).
The image shows a Microsoft Azure portal interface displaying the overview of an application registration. It includes details such as application IDs, client credentials, and options to manage the application setup.
  • In the storage account, open Access control (IAM), add a role assignment, and grant Storage Blob Data Contributor to the pipeline identity (the service principal or managed identity used by your Azure DevOps service connection).
This image shows a Microsoft Azure portal interface for adding a role assignment in Access Control (IAM) for blob storage. It includes sections for selecting roles, members, and conditions, with a searchable list of available roles.
After assigning the role, the pipeline can authenticate via Entra ID and perform remote state operations (read, write, lock) against the blob container without requiring storage access keys. Next steps and recommendations
  • Configure the Terraform backend in the pipeline init step (example shown above) and confirm the service connection has the required RBAC role on the storage account or container.
  • Structure environments to isolate state: use dedicated containers, distinct keys (for example: dev.tfstate, prod.tfstate), or separate storage accounts per environment.
  • Enable storage blob versioning and consider a backup/retention policy to improve recovery options in case the state becomes corrupted.
  • Audit access and rotate credentials for service principals; consider using managed identities where feasible to reduce secret management.
References and further reading
Using a dedicated, RBAC-protected storage account with Azure Blob versioning and Entra ID authentication gives you secure, durable, and recoverable Terraform state for CI/CD pipelines.

Watch Video