Bootstrapping and maintaining a Google Cloud organization · Topic 3 of 6

Infrastructure as Code, Blueprints and Automation Scripts

Manage Google Cloud infrastructure with Terraform, Infrastructure Manager, Cloud Foundation Toolkit, Config Connector, GitOps, Helm and scripts.

  • 5 min
In a nutshell
  • Terraform state belongs in a versioned Cloud Storage backend, applied only by a pipeline.
  • Infrastructure Manager is managed Terraform with stored state and revisions.
  • Config Connector manages Google Cloud resources as Kubernetes resources and corrects drift.

What it is

Infrastructure as code (IaC) means describing infrastructure in files kept in version control, then letting a tool create and change it. Changes are reviewed like code, repeatable across environments and easy to roll back.

Google Cloud supports several tools. The exam expects you to pick the right one for the team and to make changes through a pipeline, not the console.

Git repoTerraform or manifestsPull requestplan and reviewMergeApplyInfrastructure Manager or Config SyncGoogle Cloud resources

How it works

The tools

ToolWhat it isTypical use
TerraformDeclarative IaC with the Google provider; keeps a state fileMost Google Cloud infrastructure
Infrastructure ManagerManaged service that runs Terraform for you and stores stateTerraform without running your own runners or state bucket
Cloud Foundation ToolkitGoogle's library of Terraform modules and blueprintsProject factory, networks, the enterprise foundation
Config ConnectorKubernetes add-on with custom resources for Google Cloud servicesTeams that manage everything as Kubernetes manifests
GitOpsCluster pulls desired state from Git (Config Sync, Argo CD)Keeping clusters and their config in sync with a repo
HelmPackage manager for Kubernetes applications (charts)Installing and versioning apps on GKE

Terraform on Google Cloud

Terraform compares the configuration with its state and plans the changes. Keep state remote, locked and versioned, never on a laptop.

terraform {
  backend "gcs" {
    bucket = "acme-tf-state"
    prefix = "prod/network"
  }
}

resource "google_compute_network" "prod" {
  name                    = "prod-vpc"
  auto_create_subnetworks = false
}

Turn on Object Versioning for the state bucket so a corrupted state can be recovered. Run terraform plan on every pull request and terraform apply only from the pipeline, using a service account rather than a person's credentials.

Infrastructure Manager

Infrastructure Manager runs Terraform plan and apply as a managed service, using Cloud Build under the hood. You give it the configuration from a Git repository or a local source, input values and a service account, and it keeps the state and a revision history of each deployment.

It suits teams that want Terraform without operating runners, state buckets and locking themselves. Preview runs show what a change would do before it is applied.

Config Connector and GitOps

Config Connector lets you declare a Cloud Storage bucket, Pub/Sub topic or Cloud SQL instance as a Kubernetes resource. A controller in the cluster creates and continuously reconciles the real resource, so manual drift is corrected.

apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
metadata:
  name: acme-shop-assets
  namespace: shop
spec:
  location: EU
  uniformBucketLevelAccess: true

With GitOps, a cluster agent such as Config Sync or Argo CD watches a Git repository and applies what is there. Combine it with Config Connector and the repository becomes the single source of truth for apps and infrastructure.

Google-recommended practices and blueprints

PracticeWhy
Start from the enterprise foundations blueprint and Cloud Foundation Toolkit modulesTested hierarchy, networking, logging and IAM patterns
Use a project factory moduleEvery new project gets the same APIs, labels, budgets and IAM
Small, separate states per layer and environmentA mistake in one stack cannot destroy another
Validate plans against policy before applyCatch violations in the pipeline, not in production
Never change managed resources in the consoleManual changes cause drift that the next apply reverts

gcloud beta terraform vet checks a Terraform plan against a policy library so the pipeline can block a non-compliant change.

Automation with scripting

Some tasks do not fit declarative IaC: reports, clean-ups and one-off migrations. Use the Cloud Client Libraries for Python or Go with Application Default Credentials, so the same script runs on a laptop and as a service account.

from google.cloud import compute_v1

client = compute_v1.InstancesClient()
request = compute_v1.AggregatedListInstancesRequest(project="shop-web-dev")
for zone, scoped in client.aggregated_list(request=request):
    for vm in scoped.instances:
        if "owner" not in vm.labels:
            print(zone, vm.name)

Run scheduled scripts as a Cloud Run job triggered by Cloud Scheduler, under a dedicated service account.

A worked example

A platform team creates projects by hand, and settings differ between them. The engineer adopts the Cloud Foundation Toolkit project factory and stores each team's project definition in Git.

A pull request runs terraform plan and policy validation; after approval, Infrastructure Manager applies it with a dedicated service account. Application teams on GKE declare their buckets and topics with Config Connector, synced by Config Sync.

Project requestpull requestPlan and policy checkApprovalInfrastructure ManagerapplyNew projectlabels, budget, IAM

How the exam asks about this

  • "The team wants to use Terraform without managing state storage or build runners." Use Infrastructure Manager.
  • "A Kubernetes-centric team wants to manage Cloud SQL instances with the same manifests and tooling as their apps." Use Config Connector, ideally with Config Sync.
  • "Every new project must have the same labels, APIs and budget alerts." Use a project factory module from the Cloud Foundation Toolkit in a pipeline.

What to remember for the exam

  • Terraform state belongs in a versioned Cloud Storage backend, applied only by a pipeline.
  • Infrastructure Manager is managed Terraform with stored state and revisions.
  • Config Connector manages Google Cloud resources as Kubernetes resources and corrects drift.
  • Config Sync and Argo CD are GitOps: the cluster pulls desired state from Git.
  • Start from Google blueprints and Cloud Foundation Toolkit modules, not from scratch.
Watch out

A fix made in the console to a Terraform-managed resource is undone by the next apply. Make the change in code, or the incident comes back.

Common mistakes

Running Terraform from engineers' laptops. Credentials and state spread, and changes skip review. Run plan and apply in a pipeline with a service account.

One huge state for the whole organization. Every change risks everything and plans become slow. Split state by layer and environment.

Choosing Helm to create cloud resources. Helm packages Kubernetes applications. To manage Google Cloud resources from Kubernetes, use Config Connector.

Check yourself

  • Why store Terraform state in a versioned Cloud Storage bucket?

    It is shared and locked for the team, and versioning lets you recover an earlier state if one is corrupted.

  • What does Infrastructure Manager add over plain Terraform?

    It runs Terraform as a managed service, storing state and a revision history for each deployment so you do not run your own runners or state backend.

  • What happens when someone edits a Config Connector managed resource by hand?

    The controller reconciles the resource back to the state declared in the Kubernetes manifest.

Topic 3 of 6 in Bootstrapping and maintaining a Google Cloud organization

Test yourself on Bootstrapping and maintaining a Google Cloud organization

The full Google Cloud Professional Cloud DevOps Engineer question bank has 751 questions, with timed practice exams and an explanation for every answer.

This study guide is independent and is not affiliated with, endorsed by, or sponsored by Google Cloud. Vendor names, certification names and exam codes are trademarks of their respective owners and are used for identification only.