Soru

Zorluk: OrtaImplementing Infrastructure as Code using Terraform and Deployment Manager

Your enterprise cloud architecture team needs to safely migrate an existing production Terraform deployment from local state management to a centralized remote backend on Google Cloud Storage with state locking and version control enabled. What is the correct sequence of operational steps required to complete this migration without data loss?

  1. 1Provision a dedicated Cloud Storage bucket with Object Versioning enabled and grant least-privilege IAM permissions to the Terraform service account.
  2. 2Add a `backend "gcs"` configuration block specifying the target bucket name and state prefix into the Terraform code.
  3. 3Execute `terraform init` in the working directory to detect the backend change and trigger the backend initialization process.
  4. 4Approve the interactive confirmation prompt to copy the existing local state file into the remote GCS backend.
  5. 5Verify the updated state object in the GCS bucket and safely remove or archive the local `terraform.tfstate` file.

Cevap

The correct order begins with provisioning and securing the target Cloud Storage bucket with versioning enabled, adding the backend configuration block to the Terraform code, executing terraform init to detect backend changes, confirming the state transfer prompt, and finally validating the remote state file while removing the local state file.
Migrating Terraform state to Google Cloud Storage requires preparing the destination infrastructure first (creating the versioned GCS bucket), defining the configuration (`backend "gcs"`), executing `terraform init` to register the new backend and transfer local state, confirming the interactive copy prompt, and finally validating remote state presence prior to local file cleanup.

Adım Adım Çözüm

1
Provision the Cloud Storage bucket with versioning and IAM access controls.
A secure, versioned remote location is available for storing state files safely.
Terraform cannot write state to a non-existent or inaccessible backend bucket.
2
Declare the backend block in the Terraform root module.
The Terraform codebase specifies GCS as its remote backend.
Terraform needs backend definitions in code before it can re-initialize its working state directory.
3
Run terraform init.
Terraform identifies the change in backend provider and initiates state migration.
Backend changes require explicit re-initialization via the CLI.
4
Confirm the state copy prompt when requested by terraform init.
Local state entries are securely transferred to the GCS remote state object.
Ensures continuous tracking of infrastructure resources without resetting state.
5
Validate remote state object creation and remove local terraform.tfstate file.
State management is fully centralized remotely, preventing split-brain or stale state edits.
Leaving stale local state files can cause operational confusion or accidental local applies.

Anahtar Kavram

Terraform Remote State Migration to Google Cloud Storage
Tahmini Süre:1m 30s
Bu soruyu puanla