Soru

Zorluk: ZorBuilding and Managing Infrastructure as Code (IaC)

A cloud architecture team at a healthcare informatics enterprise is transitioning manually provisioned Google Cloud VPC networks and subnetworks into an automated Infrastructure as Code (IaC) workflow managed by Terraform. The team must establish remote state management with state locking, avoid resource recreation, and ensure existing production workloads experience zero downtime during the adoption process. What is the correct sequence of steps to safely import these unmanaged GCP infrastructure resources into Terraform state and align the HCL configuration?

  1. 1Provision a Google Cloud Storage bucket with object versioning enabled and define the backend "gcs" block in the Terraform root module.
  2. 2Execute terraform init to configure remote backend state storage and download required provider plugins.
  3. 3Write skeleton HCL resource blocks in the local module matching the resource types of the target VPC network and subnetworks.
  4. 4Execute terraform import commands referencing declared resource addresses and their full GCP resource URIs.
  5. 5Run terraform plan and iteratively align HCL resource arguments until the execution plan outputs zero changes.

Cevap

The correct operational sequence begins with establishing the Cloud Storage backend configuration and versioning, followed by running terraform init to initialize backend locks. Next, empty or skeleton resource blocks must be declared in HCL so that terraform import commands can bind GCP resource URIs to state addresses. Finally, running terraform plan and adjusting HCL attributes until zero differences remain ensures complete state and code parity without service disruption.
The sequence correctly follows Google Cloud and Terraform best practices for brownfield adoption: 1) Backend setup (GCS bucket + code block), 2) Workspace initialization (terraform init), 3) HCL block declaration, 4) Resource state import (terraform import), and 5) Plan validation and attribute alignment (terraform plan).

Adım Adım Çözüm

1
Configure the GCS remote backend bucket with versioning and add the backend "gcs" block to HCL.
Defines the centralized remote state target and locking storage.
Remote state infrastructure must exist prior to initializing Terraform.
2
Run terraform init.
Connects the local workspace to the GCS backend and downloads provider plugins.
State locking and remote storage initialization must precede state modification commands.
3
Declare empty HCL resource blocks (e.g., google_compute_network and google_compute_subnetwork).
Establishes addressable targets within the Terraform configuration.
Terraform import requires an addressable HCL resource block target to bind imported attributes.
4
Execute terraform import commands targeting specific GCP resource URIs.
Populates the GCS remote state file with actual GCP resource attributes.
Brings unmanaged live infrastructure into Terraform state without making runtime changes.
5
Run terraform plan and refine HCL parameters until output indicates 'No changes'.
Achieves complete parity between local code, remote state, and live GCP resources.
Prevents terraform apply from inadvertently modifying or destroying production resources.

Anahtar Kavram

Brownfield Infrastructure Import and IaC State Alignment
Bu soruyu puanla