Example terraform foundation for imaginary ACME organization based on https://cloud.google.com/docs/terraform/best-practices-for-terraform
This example repository shows how you can deploy foundation resources (folders, projects, VPCs, etc.) in an organization.
This is a green field
type of deployment that assumes that you are starting off with an empty organization and
a superadmin user. The goal of this repo is to deploy and manage all the resources using Terraform Cloud
but the code can be easily adjusted for Terraform Enterprise
as well.
Deployed architecture is simplified Dual SVPC architecture described in the CFT terraform-example-foundation This terraform code will deploy the following infrastructure:
- You can use this project to bootstrap your new GCP organization with GCP recommended best practices
- You can use this project to create new foundation that follows the best practice while you are decomissioning old infrastructure
- If you need terraform code to be integrated with CI/CD and Github this project comes with all of it
- GitOps management of the deployed infrastructure.
You can use this code for the following scenarions
- As a DevOps team/organization you need to bootstrap GCP organization with best practices.
- As a DevOps team/organization you need GCP resources to be managed via CI/CD system and GitOps workflow.
- As a DevOps team/organization you need an easy way to changes infrastructure parameters.
- As a DevOps team/organization you need an easy way to extend the foundation infrastructure. For example, you can easily add environments or subnets.
- Basic knowledge of terraform workflows
- Basic knowledge of terraform syntax
- Basic knowledge of GCP Console UI and gcloud command
In the following steps we will demonstrate how this repo can be used with Terraform Cloud to green field deploy foundation resources without any code changes or any gcp service account keys needed. Everything will be done through the web browser (Chrome). The code in this repository will set up Workload Identity Federation on GCP side in order to avoid crating any service account keys. Furthermore, the GCP organization policies will be set in a way that prevents service account key creation.
The following groups of steps will help you deploy the foundations in your organization. The steps are grouped in the following way:
Before starting the deployment you will need following:
- GitHub account
- GCP account and organization setup with billing
- Terraform Cloud Account with terraform organization and superadmin rights.
- Fork this repo to your GitHub account
- Go to Terraform Cloud and set up the GitHub provider following these instructions: https://developer.hashicorp.com/terraform/tutorials/cloud/github-oauth
- Go to https://console.cloud.google.com/ and set up the superadmin user with the following roles granted on the organization level(!!!This repo assumes that the operator deploying this account is the user logged into GCP console. You should have these roles assigned to you on the organizational level):
- Open the cloud shell (Open this in the new tab: https://console.cloud.google.com/?cloudshell=true)
- Run
gcloud auth print-access-token
and save the value of the token. You will need this in the following section.
This is the bootstrapping workspace that will create all other workspaces with proper variables and also bootstrap GCP with proper TFC project, Workload Identity Federation, GCP Folders, and proper GCP automation service accounts.
- Go to https://app.terraform.io/
- Create workspace
00-tfc-bootstrap
with the following settings:- Version Control Workflow
- For Version Control Provider use the provider that you set up in step 2 of 1 - Presetup. Example:
- Choose the clone of this repository. Example:
- Set Workspace Name text field to 00-tfc-bootstrap
- Press
Advanced Options
button - Set
Terraform Working Directory
to 00-tfc-bootstrap - Press
Create Workspace
- Press
Go to workspace overview button
- Create the following terraform variables for the
00-tfc-bootstrap
:tfc_organization
-> Name of your terraform cloud organizationbilling_account_id
-> GCP Billing Account ID.org_id
-> Your GCP Organization IDgithub_repo
-> Your clone of this repogithub_oauth_client
-> Name of the GitHub VCS Provider. You can get this value by going tohttps://app.terraform.io/app/<TFC_ORGANIZATION_NAME>/settings/version-control
See this image for an example
- Create the environment variable
GOOGLE_OAUTH_ACCESS_TOKEN
and set the value to the value obtained in the step 5 of 1 - Presetup - Go to https://app.terraform.io/app/settings/tokens
- Press
Create an API Token
button - For description enter
workload-identity-federation
and pressCreate API token
- Copy the value of the token and press
Done
(I would advise to save this value somewhere temporarily) - Go back to the
00-tfc-bootstrap
and create an environment variableTFE_TOKEN
with the sensitive value from the previous step. Your variables should look like this - Deploy the workspaces by pressing
Actions-Start new run
button. Once the plan is over you should see around 47 resources to be created. PressConfirm and Apply
button. - Go to the
00-tfc-bootstrap
workspace settings and allow remote state sharing with all other workspaces. Example: - Go to the variables and delete the environment variable
GOOGLE_OAUTH_ACCESS_TOKEN
.
If everything went well you should see the following workspaces created in your terraform cloud organization:
- 01-cloud-administration-global
- 02-global-iam
- 03-org-policies
- 04-shared-services
- 05-hierarchical-firewall-policy
- 10-shared-vpc-projects-dev
- 10-shared-vpc-projects-stg
- 10-shared-vpc-projects-prd
- 20-shared-vpc-networking-dev
- 20-shared-vpc-networking-stg
- 20-shared-vpc-networking-prd
- 30-projects-dev
- 30-projects-stg
- 30-projects-prd
Execute the following workspaces in the following order:
- 01-cloud-administration-global
- 02-global-iam
- 03-org-policies
- 04-shared-services
- 05-hierarchical-firewall-policy
- 10-shared-vpc-projects-dev
- 10-shared-vpc-projects-stg
- 10-shared-vpc-projects-prd
- 20-shared-vpc-networking-dev
- 20-shared-vpc-networking-stg
- 20-shared-vpc-networking-prd
- 30-projects-dev
- 30-projects-stg
- 30-projects-prd
If everything went well you should have the following infrastructure deployed:
If you want to change folder names the best way to do this is by changing variable folders
in 00-tfc-boostrap/terraform.tfvars.
If you need to change the values for certain parts of the infrastructure the best way to do this is by changing variables
in the terraform.tfvars
files in the respective directories. For example, to add firewall rules, change 05-hierarchical-firewall-policy/terraform.tfvars.
If you need to expand this infrastructure template with new components then you need to create new TFC workspaces by chaging the code in 00-tfc-boostrap.