Question

Difficulty: MediumDeploying Infrastructure using Deployment Manager or Terraform

A Cloud Engineer needs to migrate a local Terraform state file to a Google Cloud Storage (GCS) remote backend for a production deployment. Place the steps required to complete this migration in the correct execution order.

  1. 1Create a dedicated Google Cloud Storage bucket with Object Versioning enabled.
  2. 2Add a `backend "gcs"` configuration block specifying the bucket name into the Terraform code.
  3. 3Execute `terraform init` in the root configuration directory.
  4. 4Confirm the CLI prompt to copy the existing local state file to the GCS remote backend.

Answer

To migrate local Terraform state to a GCS remote backend, first provision the GCS bucket with versioning enabled, then define the backend "gcs" block in the Terraform code, run `terraform init` to reconfigure the backend, and lastly confirm the prompt to copy local state to GCS.
The correct sequence starts by provisioning the GCS storage bucket with object versioning enabled. Next, the engineer updates the Terraform code with a `backend "gcs"` block pointing to the bucket. Then, running `terraform init` prompts Terraform to recognize the new remote backend. Finally, confirming the migration prompt transfers existing local state into the GCS bucket.

Step-by-Step Solution

1
Create the GCS bucket with Object Versioning enabled.
A destination GCS bucket is ready to safely store state files with historical recovery capability.
Terraform cannot auto-create the remote backend bucket during backend initialization.
2
Define the backend "gcs" block in the Terraform configuration.
The Terraform root module is configured to point to the created GCS bucket.
Terraform requires explicit backend parameters to locate where state should be persisted.
3
Execute `terraform init`.
Terraform detects the change from local backend to GCS backend.
Re-initialization triggers the backend reconfiguration workflow in Terraform CLI.
4
Approve the state migration prompt.
Existing local state is securely uploaded to the GCS bucket and local state is updated.
Confirming ensures state continuity so existing managed infrastructure resources are not lost or re-created.

Key Concept

Terraform Remote State Migration to Google Cloud Storage Backend
Rate this question