A software development company is migrating its infrastructure management from local scripts to Terraform on Google Cloud. During continuous integration runs, multiple automated build workers occasionally attempt to apply changes to the same regional infrastructure concurrently, resulting in state file race conditions and loss of state revision history. Which remote backend strategy should the cloud architect implement to ensure safe concurrent execution and historical state recovery?
- Configure the Terraform backend to use a Cloud Storage bucket with Object Versioning enabled to support automatic state locking and state history preservation.Answer
- BStore the Terraform state file on an ephemeral local disk inside the build container and commit updated state files to a version-controlled Git repository after each execution.
- CGrant the primitive Owner role to the pipeline service account to enable it to forcibly overwrite locked state objects whenever lock contention occurs.
- DGrant the pipeline service account the Service Account Admin role so that it can dynamically provision per-execution lock management service accounts.
Answer
Configure the Terraform backend to use a Cloud Storage bucket with Object Versioning enabled to support automatic state locking and state history preservation.
The standard GCP solution for Terraform remote state storage is a Cloud Storage (GCS) bucket. The `gcs` backend natively handles state locking using Cloud Storage strongly consistent operations and object metadata locks, preventing parallel modifications. Enabling Object Versioning on the bucket ensures that all state changes create historical revisions, allowing administrators to recover previous states if needed.
Step-by-Step Solution
Key Concept
Terraform Remote State Management and Cloud Storage Locking