Your cloud architecture team needs to migrate a local Terraform state file containing critical Google Cloud infrastructure to a centralized, multi-user Google Cloud Storage (GCS) remote backend with state locking and service account impersonation. What is the correct sequence of steps to perform this state migration securely?
- 1Provision a Cloud Storage (GCS) bucket configured with Object Versioning enabled and Uniform Bucket-Level Access.
- 2Add a `backend "gcs"` configuration block specifying the bucket name and prefix to the root Terraform module.
- 3Authenticate your environment session to assume the deployment Service Account identity with permissions on the GCS state bucket.
- 4Execute `terraform init -migrate-state` to automatically transfer the local state file entries into GCS and acquire state locks.
Answer
The correct sequence begins with provisioning a GCS bucket with Object Versioning, followed by defining the backend configuration block in code, authenticating via service account impersonation, and finally running terraform init with the -migrate-state flag to copy local state to GCS.
Migrating Terraform state requires establishing the underlying storage bucket first, updating the codebase to point to the new backend, authenticating the CLI session with short-lived service account credentials, and invoking terraform init with the -migrate-state flag to copy local state objects safely.
Step-by-Step Solution
Key Concept
Terraform Remote State Migration to GCS with Locking and Versioning