How terraform-google-modules/terraform-example-foundation works
Purpose and execution model
This is a fork-and-customize Terraform blueprint for building a secure Google Cloud enterprise foundation. It is not an application: independent Terraform root projects must be applied in dependency order, first manually for bootstrap and then through CI/CD. The primary entry points are the numbered stage roots, especially 0-bootstrap/main.tf, 1-org/envs/shared/, and per-environment main.tf roots.
0. Bootstrap: state, identities, and deployment platform
0-bootstrap/ establishes the prerequisite deployment boundary in an existing GCP organization: seed/state and CI/CD projects, Terraform service accounts, IAM, and the selected execution integration (Cloud Build by default; GitHub, GitLab, local, and Terraform Cloud examples exist).
• 0-bootstrap/main.tf
• 0-bootstrap/build_cb.tf
• 0-bootstrap/sa.tf
It produces the credentials, remote state location, and pipeline resources consumed by later stages.
1. Organization baseline
The shared organization root creates top-level folders, foundational projects, organization IAM/policies, service controls, logging, SCC notification, and network-project scaffolding. It is the boundary between organization governance and environment-specific resources.
• 1-org/envs/shared/projects.tf
• 1-org/envs/shared/org_policy.tf
• 1-org/envs/shared/log_sinks.tf
Reusable implementations cover centralized logging, CAI monitoring, network setup, and service controls in 1-org/modules/centralized-logging/main.tf, 1-org/modules/cai-monitoring/main.tf, and 1-org/modules/service_control/main.tf.
2. Environment baselines
Development, nonproduction, and production are separate Terraform states and roots. Each creates its environment folder plus shared KMS and Secret Manager resources, consuming prior-stage organization outputs through remote-state configuration.
• 2-environments/envs/development/main.tf
• 2-environments/envs/nonproduction/main.tf
• 2-environments/modules/env_baseline/kms.tf
3. Network architecture alternatives
Choose one mutually alternative stage-3 topology: conventional Shared VPC (3-networks-svpc/) or hub-and-spoke (3-networks-hub-and-spoke/). Each has shared plus per-environment roots, remote-state inputs, and outputs for downstream service projects.
Shared VPC modules establish VPCs, DNS, firewall, NAT, Private Service Connect, and private service access:
• 3-networks-svpc/modules/shared_vpc/main.tf
• 3-networks-svpc/modules/shared_vpc/firewall.tf
• 3-networks-hub-and-spoke/modules/shared_vpc/private_service_connect.tf
Hub-and-spoke additionally models hubs, transitivity, HA VPN, and Dedicated/Partner Interconnect:
• 3-networks-hub-and-spoke/envs/shared/net-hubs.tf
• 3-networks-hub-and-spoke/modules/transitivity/main.tf
• 3-networks-hub-and-spoke/modules/vpn-ha/main.tf
4. Business-unit service projects and pipeline delegation
Stage 4 creates business-unit folders and service projects per environment, attaching eligible projects to the selected Shared VPC. It also creates infrastructure-pipeline projects, separating platform-foundation deployment from application-infrastructure deployment.
• 4-projects/modules/base_env/business_unit_folder.tf
• 4-projects/modules/single_project/main.tf
• 4-projects/modules/infra_pipelines/main.tf
The example roots are under 4-projects/business_unit_1/{shared,development,nonproduction,production}/; these are intended to be replicated/customized for actual business units.
5. Application infrastructure workload layer
Stage 5 is the downstream consumer of the foundation: environment roots deploy application-facing infrastructure into the projects created in stage 4. The included example centers on a Compute Engine workload and supports Confidential Space resources.
• 5-app-infra/business_unit_1/development/main.tf
• 5-app-infra/modules/env_base/main.tf
• 5-app-infra/modules/confidential_space/main.tf
CI/CD, policy, and runtime boundaries
Terraform runs through Cloud Build, GitHub Actions, GitLab CI, or Terraform Cloud patterns; scripts wrap plan/apply and authentication. Cloud Build definitions encode plan/apply workflows, while protected environment branches constrain deployment scope.
• build/cloudbuild-tf-plan.yaml
• build/cloudbuild-tf-apply.yaml
• build/tf-wrapper.sh
Plans are policy-validated with gcloud terraform-tools against OPA/Rego policy-library templates and constraints:
• policy-library/lib/constraints.rego
• policy-library/policies/templates/gcp_iam_allowed_bindings.yaml
• policy-library/policies/constraints/iam_deny_public.yaml
A Go-based deployment helper orchestrates validation, vet, apply, and destroy stages and integrates with GCP, GitHub, and GitLab:
• helpers/foundation-deployer/main.go
• helpers/foundation-deployer/stages/executor.go
• helpers/foundation-deployer/gcp/gcp.go
Main entry points: projects.tf Stage 1 organization baseline · development/main.tf Stage 2 environment baselines · single_project/main.tf Stage 4 service projects · 0-bootstrap/main.tf Stage 0 bootstrap
Components 20
Foundation stages
Terraform roots applied in dependency order.
- Stage 0 bootstrap · Creates state, identities, and deployment platform.
0-bootstrap/main.tf - Cloud Build integration · Default CI/CD execution integration.
0-bootstrap/build_cb.tf - Terraform service accounts · Deployment identities for later stages.
0-bootstrap/sa.tf - Stage 1 organization baseline · Organization governance and foundational projects.
1-org/envs/shared/projects.tf - Organization policies
1-org/envs/shared/org_policy.tf - Centralized logging
1-org/modules/centralized-logging/main.tf - Service controls
1-org/modules/service_control/main.tf - Stage 2 environment baselines · Separate development, nonproduction, and production states.
2-environments/envs/development/main.tf - KMS and Secret Manager
2-environments/modules/env_baseline/kms.tf - Stage 4 service projects · Business-unit folders and environment service projects.
4-projects/modules/single_project/main.tf - Infrastructure pipeline projects · Delegated application-infrastructure deployment projects.
4-projects/modules/infra_pipelines/main.tf - Stage 5 application infrastructure · Application-facing infrastructure in service projects.
5-app-infra/business_unit_1/development/main.tf - Confidential Space
5-app-infra/modules/confidential_space/main.tf
Stage 3 network choices
Select one topology.
- Shared VPC topology · Conventional Shared VPC option.
3-networks-svpc/modules/shared_vpc/main.tf - VPC network services · Firewall, DNS, NAT, PSC, and private service access.
3-networks-svpc/modules/shared_vpc/firewall.tf - Hub-and-spoke topology · Hub-and-spoke network option.
3-networks-hub-and-spoke/envs/shared/net-hubs.tf - Transitivity and connectivity · Transitivity, HA VPN, and interconnect capabilities.
3-networks-hub-and-spoke/modules/transitivity/main.tf
Delivery and policy
Execution, validation, and deployment controls.
- Cloud Build plan/apply · Plan and apply workflow definitions.
build/cloudbuild-tf-plan.yaml - OPA policy validation · Validates Terraform plans against Rego constraints.
policy-library/lib/constraints.rego - Foundation deployer · Orchestrates validation, vet, apply, and destroy.
helpers/foundation-deployer/main.go
Connections 21
- Stage 0 bootstrap → Cloud Build integration: provisions
- Stage 0 bootstrap → Terraform service accounts: creates
- Stage 0 bootstrap → Stage 1 organization baseline: state and credentials
- Terraform service accounts → Cloud Build plan/apply: authenticates
- Stage 1 organization baseline → Organization policies: applies
- Stage 1 organization baseline → Centralized logging: configures
- Stage 1 organization baseline → Service controls: configures
- Stage 1 organization baseline → Stage 2 environment baselines: remote-state inputs
- Stage 2 environment baselines → KMS and Secret Manager: creates
- Stage 2 environment baselines → Shared VPC topology: select topology
- Stage 2 environment baselines → Hub-and-spoke topology: select topology
- Shared VPC topology → VPC network services: includes
- Hub-and-spoke topology → Transitivity and connectivity: includes
- Shared VPC topology → Stage 4 service projects: VPC attachment outputs
- Hub-and-spoke topology → Stage 4 service projects: network outputs
- Stage 4 service projects → Infrastructure pipeline projects: separates deployment
- Stage 4 service projects → Stage 5 application infrastructure: target projects
- Stage 5 application infrastructure → Confidential Space: optionally deploys
- Cloud Build plan/apply → OPA policy validation: validates plans
- OPA policy validation → Stage 1 organization baseline: gates Terraform changes
- Foundation deployer → Cloud Build plan/apply: orchestrates execution