Question

Difficulty: MediumBuilding and Managing Infrastructure as Code (IaC)

A fintech startup has manually provisioned a production environment in Google Cloud consisting of Cloud Pub/Sub topics and BigQuery datasets via the GCP Console. To bring these resources under Infrastructure as Code (IaC) governance, the cloud architecture team needs to safely import the resources into Terraform state while setting up a centralized Cloud Storage (GCS) backend for state storage. In what sequence should the engineering team execute the following operational steps to import the existing infrastructure without accidental resource destruction?

  1. 1Write matching HCL configuration blocks for the Cloud Pub/Sub topics and BigQuery datasets and declare the GCS remote backend block in code.
  2. 2Run `terraform init` to download necessary provider plugins and initialize connection to the remote Cloud Storage backend bucket.
  3. 3Execute `terraform import` for each resource using their full Google Cloud resource IDs to associate live GCP infrastructure with state addresses.
  4. 4Run `terraform plan` to verify that the declared HCL code matches the imported state with zero planned changes or resource replacements.
  5. 5Commit the validated HCL code to the team's version control repository to integrate with automated CI/CD deployment pipelines.

Answer

The correct operational sequence is: 1) Write matching HCL configuration and GCS backend declarations, 2) Run terraform init to connect to the remote backend, 3) Execute terraform import using full GCP resource IDs, 4) Run terraform plan to verify zero configuration drift, and 5) Commit the verified HCL code to version control.
Safely bringing existing (brownfield) Google Cloud infrastructure under Terraform management requires first writing corresponding HCL code and backend settings, initializing the backend via `terraform init`, executing `terraform import` with exact GCP resource IDs, validating zero drift using `terraform plan`, and finally committing code to Git for CI/CD integration.

Step-by-Step Solution

1
Declare HCL resource definitions and backend settings
Local HCL files represent the expected resource schemas and point state to GCS.
Terraform needs defined resource addresses and a configured remote backend before state initialization.
2
Initialize Terraform working directory with `terraform init`
Provider plugins are downloaded and the GCS backend connection is established.
Remote state tracking and backend locking must be active before binding live cloud infrastructure.
3
Run `terraform import` targeting GCP resource IDs
Existing BigQuery dataset and Pub/Sub topic state records are populated into the remote GCS state file.
Importing maps real GCP infrastructure to Terraform state keys without recreating resources.
4
Run `terraform plan` to detect drift
Output shows 'No changes. Your infrastructure matches the configuration.'
Guarantees HCL declarations exactly reflect the live resource attributes, preventing accidental deletion upon future applies.
5
Commit HCL configurations to Git source repository
Version control tracks infrastructure state and enables CI/CD automation.
Formalizes IaC management and enables automated pipeline governance.

Key Concept

Infrastructure as Code (IaC) Brownfield Resource Import Workflow
Rate this question