// Why this matters
A gcp landing zone terraform setup is the difference between a cloud estate you can reason about and one that grows by accident. Without a foundation, every team picks its own project layout, grants roles to individuals, creates its own VPC and enables whatever APIs it likes. Audit, cost attribution and incident response all degrade at the same rate.
A module library is the delivery mechanism for that foundation. Instead of a wiki page of conventions, the conventions are code: naming, labels, org policy bindings, the default set of enabled APIs, the default log sinks. Teams consume them, and the platform team improves them in one place.
The lower and upper split mirrors how change is actually risked. Lower environments are where modules are proven and where mistakes are cheap; upper is production and is gated. Keeping them structurally separate (folders, state, pipelines) turns that principle into something a pipeline can enforce.
// The problem
The usual failure pattern is a single flat Terraform root with hundreds of resources: one state file, one blast radius, plan times measured in minutes, and a change to a shared VPC subnet that shows up in an unrelated team's review. The opposite pattern, a repository per team with copy-pasted code, drifts within weeks.
IAM adds its own mess. Direct user grants accumulate and never get removed; project-level roles are broad because the narrow one was hard to find. Organisation policy, such as disabling service account key creation, is applied at the org level once and then forgotten, so new folders silently lack it.
You need a structure that is small enough to review, strongly typed at its seams, and honest about ordering: folders before projects, projects before network attachments, groups before bindings.
// The solution
The foundation is split into root modules that match ownership and state boundaries. The org root owns folders, org policies and log sinks. The network root owns the shared VPC host project, subnets and firewall baselines per environment. The tenant roots (one per environment) read a directory of YAML tenant definitions and instantiate the project factory with for_each. Each root has its own state, so a tenant plan only reads network outputs through data sources, never through shared state.
The project factory module does the repeated work: create the project in the right folder, link billing, enable a baseline list of APIs, attach it to the shared VPC host, create a Cloud Identity group per role (viewer, developer, admin), bind roles to the groups, and apply labels for cost allocation. This is the same idea described in The GCP Project Factory: Folder, Project, VPC and GKE per Tenant, here generalised into a library with sibling modules for folders and org policy.
Policy is bound where it cannot be skipped: a folder module takes a list of boolean and list constraints and creates google_folder_organization_policy resources (or the v2 equivalent). Upper folders get stricter constraints, for example restricting resource locations and denying external IPs on VMs.
We rejected a single mega-module with dozens of variables (impossible to version safely) and Terraform workspaces for environment separation (one backend, one set of credentials, easy to apply to the wrong one). Distinct directories, distinct state buckets, and distinct service accounts per environment cost a little repetition and buy real isolation. Group creation uses the pattern from Making a 2,000-Member Identity Plan Fast, Safe and Authoritative and the access model is explained in One Config, Two Clouds: Group and Role Management as Code.
- Org bootstrapfolders, billing association, log sinks
- Folder factorylower and upper trees per business area
- Org policyconstraints bound at folder scope
- Shared VPC hostnetwork project with subnets per region
- Project factoryservice projects attached to the host
- IAM layergroup-based bindings, no user grants
- Gate: plan review gateupper environment needs a protected-environment approval
- Workload servicesArmor, DNS, Pub/Sub, databases, mesh fleet
- Drift checkscheduled plan fails on out-of-band change
// Before you start
- A Google Cloud organisation with a billing account and permission to create folders and projects.
- Cloud Identity (or Workspace) with a service account allowed to create groups.
- Terraform 1.6+ and the
googleandgoogle-betaproviders 5.x. - A GCS bucket per environment for remote state, with versioning enabled.
- A pipeline identity (workload identity federation preferred) that can impersonate one Terraform service account per environment.
- Familiarity with
for_each, module sources andterraform_remote_stateor data sources.
// Step-by-step implementation
01 Lay out the repository and state boundaries
Decide the roots before writing a resource. A layout that mirrors ownership keeps reviews small. Modules live in a separate versioned repository (or a
modules/tree tagged on release); roots only contain wiring.Each environment root has its own backend prefix and its own service account to impersonate. That is what stops a lower-environment pipeline from ever being able to write to upper state.
02 Create the folder tree and bind organisation policy
Folders carry policy. Build a small folder module that takes a name, a parent and a policy map, and creates the folder plus the constraints. Putting policy in the same module as the folder means it is impossible to create a folder without deciding its guard rails.
Start strict and loosen with evidence: disable service account key creation, require OS Login, restrict public IPs, and constrain resource locations on upper.
03 Stand up the shared VPC host project
One network project per environment owns the VPC and subnets; tenants attach as service projects. This gives central control over CIDR allocation and firewalling while teams keep ownership of their workloads.
Output the subnet self-links by region; tenants will request subnet-level access (
compute.networkUser) for only the subnets they need.04 Write the project factory as a module
The factory is the heart of the library. It creates the project, enables APIs, attaches to the host and returns identifiers. Keep inputs few and typed; anything optional goes in an object with defaults so callers cannot invent new knobs.
Labels are mandatory inputs (team, environment, cost centre), because retrofitting cost attribution is far harder than requiring it.
05 Create groups and bind roles to groups only
Make group creation part of the tenant definition. A tenant has three groups by convention; bindings reference groups, never users, so offboarding is a membership change in the identity system and not a Terraform edit.
Role sets are data too. A small map from role-name to list of IAM roles keeps the policy reviewable in one file.
06 Drive tenants from YAML
Now the tenant root becomes tiny. It reads every YAML file in a directory, decodes it, and loops over the project factory. Adding a team is a new file and a pull request.
Validate the schema in CI (a JSON schema check or
terraform validatewith variable validation blocks) so a typo does not reach plan.07 Promote modules lower first and add a drift check
Module changes are released as tags. The lower root pins a release candidate; once a tenant plan is clean and an apply has soaked, the upper root bumps the pin in its own pull request, which requires approval on a protected environment.
A scheduled pipeline runs
terraform plan -detailed-exitcodeper root. Exit code 2 means drift; open an issue rather than auto-applying.
// How to verify it works
terraform planon a fresh tenant file shows exactly one project, its API enablements, a service-project attachment, three groups and the role bindings, and nothing else.gcloud resource-manager org-policies list --folder=<folder-id>shows every constraint from the folder module in effect.gcloud compute shared-vpc associated-projects list <host-project>includes the new project.- Creating a service account key in a tenant project fails with a constraint violation message.
- Re-running plan immediately after apply reports no changes.
// Design decisions and trade-offs
- Lower and upper environments live under separate folders with separate state, so a lower-environment change can never touch production.
- Modules are small and single-purpose (project, folder, network, IAM binding set) and consumed by version tag, not by branch.
- Org policy constraints are applied at folder level through a module so a project cannot opt out.
- IAM is granted to groups only; groups are created by code from the same tenant definition.
- Tenants are data (YAML) driving for_each, so adding a team is a pull request with no new Terraform.
// Pitfalls and lessons learned
- Organisation policy and API enablement race: a constraint can deny an API you are about to enable. Order folder policy first and test with a throwaway project.
- Deleting a project through Terraform schedules it for deletion; the project id is not reusable for 30 days, which is why the factory adds a random suffix.
- Shared VPC attachment needs the Compute API enabled in both host and service projects, and the pipeline identity needs
compute.xpnAdminat the org or folder. google_project_iam_memberversus_bindingversus_policy: use the additive_memberform in modules, otherwise two modules fight over the same role.- Group creation is eventually consistent; an IAM binding applied seconds later can fail with "principal does not exist". Add a short retry or split into two applies.
- Unpinned module refs turn a library fix into an unreviewed production change. Always pin by tag and bump deliberately.
// Frequently asked questions
How do I structure a gcp landing zone terraform repository?
Split by ownership and state: an org root, a network root per environment and a tenants root per environment, with modules in a separate tagged repository.
Should lower and upper environments share state?
No. Separate folders, buckets and service accounts mean a mistake in lower cannot modify production.
Is a project factory better than one project per Terraform root?
For many similar tenants, yes: one module encodes the standard and for_each scales it, while a root per project multiplies pipelines.
Why bind IAM to groups instead of users?
Membership changes in the identity system take effect without a Terraform run and leave a clean audit trail; user-level bindings drift and are never cleaned up.
How do I enforce org policies for new projects automatically?
Bind them at folder level in the module that creates the folder; projects created beneath inherit them.
// The outcome
A new team gets a compliant project, networking, groups and baseline services from one reviewed change. Policy lives in modules that every project inherits, and upper-environment changes follow a stricter path than lower.
// Where to take it next
- Add Cloud Armor, DNS and database modules to the library and consume them from the tenant YAML.
- Install a service mesh fleet per tenant cluster; see Sidecar-less Multi-Cluster Service Mesh: Istio Ambient with Vault PKI for the mesh side.
- Extend the pipeline with policy-as-code checks (Checkov or OPA on the plan JSON) before apply.
- Compare with the project factory approach in The GCP Project Factory: Folder, Project, VPC and GKE per Tenant and the form-driven version in Five-Minute Customer Onboarding: Forms, Workspaces and a Subnet Allocator.