A Cloud Engineer has a local Terraform configuration managing Google Cloud infrastructure. The engineer wants to migrate the existing local state file (`terraform.tfstate`) to a remote Google Cloud Storage (GCS) backend for team collaboration and state locking. What is the correct sequence of steps to complete this state migration?
- 1Create a Cloud Storage bucket in Google Cloud with object versioning enabled.
- 2Add a `backend "gcs"` configuration block specifying the bucket name inside the `terraform {}` settings block.
- 3Run the `terraform init` command in the working directory.
- 4Type `yes` at the terminal prompt to copy the existing local state to the new GCS backend.
Cevap
The correct sequence requires creating the Cloud Storage bucket with object versioning first, adding the `backend "gcs"` block to the Terraform configuration, running `terraform init` to initialize the backend, and confirming the prompt by typing `yes` to transfer the local state file to the remote bucket.
Migrating a local Terraform state file to a GCS remote backend follows a strict dependency order: the target Cloud Storage bucket must be created first in GCP because Terraform initialization does not provision the backend bucket automatically. Next, the `backend "gcs"` code block must be declared in the Terraform configuration. Executing `terraform init` causes Terraform to detect the new backend configuration and prompt the user to transfer the state file. Finally, typing `yes` confirms the copy action and migrates the local state file to Cloud Storage.
Adım Adım Çözüm
Anahtar Kavram
Terraform Remote Backend Migration to Cloud Storage (GCS)