A Cloud Architect needs to migrate an existing local Terraform state file containing critical production Google Cloud resources to a secure Google Cloud Storage (GCS) remote backend with state locking and CMEK encryption. Arrange the operational steps in the correct chronological order to safely complete this backend migration while maintaining resource integrity.
- 1Provision a dedicated GCS bucket configured with Object Versioning, CMEK encryption, and uniform bucket-level access, granting IAM access exclusively to the Terraform service account.
- 2Configure the `backend "gcs"` block in the Terraform root module's configuration files specifying the bucket name and state prefix.
- 3Run `terraform init -migrate-state` and approve the prompt to transfer local state records into the remote GCS backend.
- 4Execute `terraform plan` to confirm that the remote state matching produces zero pending infrastructure changes or drift.
- 5Clean up and remove the local `terraform.tfstate` and local state backup files from the local working directory.
Answer
The correct sequence starts with provisioning and securing the remote GCS bucket, adding the `backend "gcs"` configuration to the Terraform code, executing `terraform init -migrate-state` to copy state data, running `terraform plan` to verify zero drift, and finally purging local state file artifacts.
Migrating Terraform state safely requires creating the remote GCS backend infrastructure first, declaring the backend in HCL, using the CLI tool's dedicated `-migrate-state` command to copy existing resource records under a state lock, verifying state synchronization via a plan, and lastly removing legacy local state files.
Step-by-Step Solution
Key Concept
Terraform Remote State Migration to Google Cloud Storage