Question

Difficulty: HardImplementing Infrastructure as Code using Terraform and Deployment Manager

Your team needs to migrate a monolithic local Terraform state file (`terraform.tfstate`) managing production GCP resources to an enterprise remote backend hosted on Google Cloud Storage (GCS) with object versioning and state locking. Place the steps in the correct order to perform this state migration safely without losing resource state or creating concurrency issues.

  1. 1Configure the `backend "gcs"` block inside the Terraform configuration file specifying the dedicated GCS bucket and state prefix.
  2. 2Run `terraform init` to detect the new backend configuration and prompt for backend initialization.
  3. 3Confirm 'yes' at the CLI prompt to copy the existing local state data to the new GCS remote backend.
  4. 4Verify remote lock functionality using `terraform plan` and archive or delete the local `terraform.tfstate` file.

Answer

The correct sequence for migrating local Terraform state to a GCS remote backend begins by adding the backend block, running terraform init, approving the automatic state copy prompt, and validating remote locking before removing local state files.
The migration process requires declaring the backend configuration first, invoking backend initialization via terraform init, approving the state transfer to GCS, and finally validating state lock functionality before removing local state backups.

Step-by-Step Solution

1
Add the backend 'gcs' configuration block to the Terraform code.
Defines the target Google Cloud Storage bucket and prefix for remote state management.
Terraform requires explicit backend block definitions to locate target state storage.
2
Execute 'terraform init' in the workspace directory.
Terraform identifies the backend change and initiates backend migration workflows.
The init command parses new provider and backend settings to set up state transfer.
3
Confirm the state copy operation when prompted by Terraform.
Local state entries are safely copied into the target GCS bucket.
Interactive confirmation ensures state tracking continuity without destroying managed resource mappings.
4
Run 'terraform plan' to test remote state locking and clean up the local tfstate file.
Remote backend operation is validated and local state file duplication is eliminated.
Prevents future accidental usage of local state files while validating GCS backend permissions.

Key Concept

Migrating local Terraform state to remote Google Cloud Storage (GCS) backend with state locking.
Rate this question