Question

Difficulty: Very hardDeploying Infrastructure using Deployment Manager or Terraform

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?

  1. 1Configure the `backend "gcs"` block in `main.tf` and run `terraform init` to initialize state storage.
  2. 2Add a skeleton `resource "google_compute_instance" "legacy_vm" {}` block to the Terraform configuration file.
  3. 3Execute `terraform import google_compute_instance.legacy_vm projects/PROJECT_ID/zones/ZONE/instances/legacy-app-vm`.
  4. 4Populate the resource block arguments in `main.tf` to reflect the imported configuration attributes.
  5. 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

1
Initialize Remote Backend
State storage is linked to the designated GCS bucket.
Initializing the GCS backend ensures state locking and guarantees that imported resource metadata persists directly to remote state.
2
Declare Skeleton Resource Identifier
Terraform recognizes `google_compute_instance.legacy_vm` as a valid target address.
The `terraform import` CLI command fails if the target resource address is missing from configuration files.
3
Import Existing GCP Resource
Live VM metadata is written to the GCS remote state.
This binds the actual GCP instance object to the Terraform resource address without modifying live infrastructure.
4
Align HCL Code with Live Attributes
HCL definitions match all imported properties such as machine type, disk settings, and network interfaces.
Terraform state holds the imported values, but code files must be manually matched to prevent drift.
5
Validate Parity with `terraform plan`
Terraform reports 'No changes. Your infrastructure matches the configuration.'
Running plan verifies that future execution of `terraform apply` will not attempt to update or recreate the imported instance.

Key Concept

Terraform Resource Import & GCS State Synchronization
Rate this question