Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 45 ยท Operations and Delivery

Infrastructure as Code with Terraform

Learn infrastructure as code on Google Cloud with Terraform: the google provider, state in a bucket, plan and apply, modules, and a runnable plan-diff stand-in.

Clicking through the console is fine for learning, but it does not scale. Nobody remembers which boxes were ticked, a second environment means doing it all again, and a mistake leaves no trail. Infrastructure as code (IaC) fixes this: you describe your cloud resources in text files, keep them in Git, and let a tool make reality match the files.

On Google Cloud the most common tool is Terraform (and its open-source fork OpenTofu, which uses the same language). Google's older Deployment Manager service has been retired in favor of Terraform-style tooling, so new projects should not start with it. This lesson covers the Terraform workflow and then lets you build a tiny "plan" engine yourself to see how the idea works.

The idea: desired state

Terraform is declarative. You do not write "create a bucket, then set a label". You write what should exist, and Terraform works out the steps. It compares three things:

  • your configuration (the desired state),
  • the state file (what Terraform believes it created before),
  • the real world (read through the provider's API).

The difference between desired and actual becomes a plan: a list of resources to create, change or destroy. You review the plan, then apply it.

The Terraform loop

Write Describe resources in .tf files.
Plan See exactly what will change.
Apply Make the cloud match the files.

Review the plan like you would review a code change.

1 / 2

The wider IaC landscape

Infrastructure as code means describing servers, networks and services in files that live in version control, so environments are reproducible and reviewable. Two styles exist. Declarative tools (Terraform, CloudFormation, Kubernetes manifests) describe the end result and work out the steps. Imperative tools (a shell script, a Python program calling APIs) list the steps themselves. Declarative is easier to repeat safely, imperative is more flexible, and many teams mix them.

The ecosystem splits by job: provisioning tools create the infrastructure (Terraform, OpenTofu, Pulumi, and Google's own Infrastructure Manager, which runs Terraform for you), and configuration management tools set up what runs inside machines (Ansible, Chef, Puppet, Salt). Kubernetes itself is a declarative system for containers. Real projects combine them: Terraform builds the network and VMs, Ansible or a startup script installs software, or the VMs are replaced by container images.

Benefits include speed, consistency, a history of every change, and an easy way to rebuild after a disaster. Challenges are a learning curve, state that must be stored and locked safely, drift when people change resources by hand, secrets that must stay out of code, and the temptation to make everything one giant configuration. Newer practices extend the idea: GitOps (Git is the only way to change an environment and an agent applies it, as with Config Sync) and AIOps (machine learning that helps detect anomalies and suggest fixes).

Deployment Manager (legacy)

Google Cloud's first IaC service was Deployment Manager, which you may meet in older projects. You wrote a configuration in YAML listing resources and their properties, optionally with templates in Jinja2 or Python that expand into full resource lists. It tracked each deployment's resources, so you could preview, update and delete the whole set as one unit (resource tracking). Its limits were Google-only coverage, slow support for new products, and awkward large configurations, so it did not serve hybrid or multi-cloud setups. Google has ended support for it in favor of Terraform and Infrastructure Manager, so migrate old deployments rather than starting new ones.

YAML
# read-only: a Deployment Manager configuration (legacy)
resources:
  - name: demo-bucket
    type: storage.v1.bucket
    properties:
      location: EU

The google provider

A provider is a plugin that talks to one platform's API. The google provider manages Google Cloud resources. A first configuration (read-only; you need Terraform installed and credentials, see service accounts) looks like this:

HCL
terraform {
  required_version = ">= 1.6"
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 6.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = "europe-west1"
}

variable "project_id" {
  type = string
}

resource "google_storage_bucket" "assets" {
  name                        = "${var.project_id}-assets"
  location                    = "EU"
  uniform_bucket_level_access = true

  labels = {
    env  = "dev"
    team = "web"
  }
}

output "bucket_url" {
  value = google_storage_bucket.assets.url
}

A resource block has a type (google_storage_bucket), a local name (assets) and arguments. You can refer to other resources by type.name.attribute, and Terraform uses those references to build a dependency graph, so it creates things in a valid order. Provider versions change often, so check the current documentation for argument names before you copy any example.

On a laptop you can authenticate with gcloud auth application-default login. In automation, prefer an attached service account or workload identity federation over downloaded key files.

The everyday commands

Terminal
terraform init      # download providers, set up the backend
terraform fmt       # format the files
terraform validate  # check syntax and references
terraform plan      # show what would change
terraform apply     # do it (asks for confirmation)
terraform destroy   # remove everything in this state

In a plan, + means create, ~ means update in place, - means destroy, and -/+ means replace (destroy then create). Always read the replace lines: replacing a database or a disk can mean data loss.

State, and why it lives in a bucket

Terraform records what it manages in a state file. By default that is a local terraform.tfstate, which is dangerous for a team: two people with different copies will fight each other. The fix is a remote backend. On Google Cloud that is a Cloud Storage bucket:

HCL
terraform {
  backend "gcs" {
    bucket = "my-company-tf-state"
    prefix = "prod/network"
  }
}

The gcs backend supports state locking, so two applies cannot run at once on the same state. Good practice for the state bucket:

  • turn on object versioning so you can recover an older state,
  • restrict access tightly, because state can contain secrets in plain text,
  • create the bucket by hand (or with a tiny separate config), since Terraform cannot store its state in a bucket it has not yet created,
  • split states by environment and by area (prod/network, prod/apps) to limit the damage of a bad apply.

See Cloud Storage for versioning and access control.

Variables, outputs and modules

Variables make a configuration reusable, outputs expose values, and modules package a set of resources behind inputs and outputs. A module is just a folder of .tf files that you call:

HCL
module "web_bucket" {
  source     = "./modules/bucket"
  project_id = var.project_id
  name       = "web"
  env        = "prod"
}

Use modules for patterns you repeat (a standard bucket, a network with subnets), not for every single resource. Too many layers make plans hard to read.

Terraform does not replace the console for looking. It is still wise to browse what exists. But if you change a managed resource by hand, the next plan will try to undo your change. This is called drift, and fixing it means either updating the code or reverting the manual edit.

Build a plan engine

The real Terraform is far more elaborate, but the heart of plan is a comparison. This stand-in is not Terraform: it has no providers, no dependency graph and no state file. It simply compares a desired map of resources with an actual one and prints a plan in the same + ~ - style.

JS Stand-in: a tiny plan diff engine

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Notice what is missing from the plan: topic.events matches, so nothing happens. Running the plan again after an apply should always print "no changes". That property is called idempotence, and it is what makes IaC safe to run repeatedly.

Applying with a safety rail

Real Terraform refuses to destroy things quietly if you ask it not to. In configuration you can protect a critical resource with a lifecycle block (prevent_destroy = true). The stand-in below adds a similar guard to our engine and shows apply updating the "actual" world.

JS Stand-in: apply with a destroy guard

Press Run (or Ctrl + Enter) to see the console output here.

No output.

The second run changes nothing except that the protected database keeps being reported, which is the honest behavior: real Terraform would stop the whole apply with an error rather than skip it, so treat this stand-in as a simplification.

Habits that keep Terraform safe

  • Run plan in a pull request and require a human to read it; run apply from a pipeline (Cloud Build works well) with a narrowly scoped service account.
  • Pin provider versions and commit the lock file .terraform.lock.hcl.
  • Never commit state files or credentials; add *.tfstate* and .terraform/ to .gitignore.
  • Use labels on every resource so you can track cost and owners.
  • Import existing resources into state rather than recreating them (terraform import, or import blocks in newer versions).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does terraform plan do?

    Plan compares configuration, state and real resources, and prints the proposed changes. Nothing changes until you apply.
  2. 2. Why is a remote backend such as a Cloud Storage bucket recommended for teams?

    A shared state avoids conflicting local copies, and the gcs backend locks it so two applies cannot run at once.
  3. 3. A plan line shows -/+ for a database. What should you do?

    -/+ means replace. For stateful resources such as databases and disks that can mean data loss.
  4. 4. What is drift?

    Drift appears when someone edits resources outside Terraform. The next plan will show the difference.

Summary

  • Infrastructure as code describes cloud resources in files kept in Git, so changes are reviewable and repeatable.
  • Terraform (and OpenTofu) uses the google provider; Deployment Manager is retired.
  • init, plan and apply form the core loop; read every plan, especially replacements.
  • Keep state in a versioned, locked, tightly protected Cloud Storage bucket and split it by environment.
  • Modules package repeated patterns; the plan engine stand-in shows the core idea of comparing desired and actual state.

Next: Cloud Build, Cloud Deploy and CI/CD