A Cloud Engineer is tasked with bringing an existing, manually created Compute Engine VM instance named `legacy-app-vm` under Terraform management. The infrastructure state must be maintained in a remote Google Cloud Storage (GCS) backend. In what sequence should the engineer execute the following steps to safely import the VM instance without causing resource recreation or downtime?
- 1Configure the `backend "gcs"` block in `main.tf` and run `terraform init` to initialize state storage.
- 2Add a skeleton `resource "google_compute_instance" "legacy_vm" {}` block to the Terraform configuration file.
- 3Execute `terraform import google_compute_instance.legacy_vm projects/PROJECT_ID/zones/ZONE/instances/legacy-app-vm`.
- 4Populate the resource block arguments in `main.tf` to reflect the imported configuration attributes.
- 5Run `terraform plan` to verify that no infrastructure changes or destructions are proposed.
Answer
The correct deployment sequence is: 1) Configure the GCS backend and run `terraform init`, 2) Declare a skeleton resource block in HCL, 3) Execute `terraform import` using the GCP resource identifier, 4) Update the HCL resource arguments to match live attributes, and 5) Run `terraform plan` to confirm zero pending changes.
Importing existing GCP infrastructure into Terraform requires initializing the remote backend first, declaring an empty resource block in code to anchor the import command, pulling live state via `terraform import`, updating the HCL code to match the state, and validating zero plan diffs with `terraform plan`.
Step-by-Step Solution
Key Concept
Terraform Resource Import & GCS State Synchronization