This tutorial explains how to manage infrastructure as code with Terraform and Cloud Build using the popular GitOps methodology. GitOps uses a Git repository to store the desired environment state. Terraform enables predictable creation, change, and improvement of cloud infrastructure using code. Cloud Build, a Google Cloud continuous integration service, automatically applies Terraform manifests to your environment.
This tutorial is for developers and operators looking for an elegant strategy to predictably make changes to infrastructure. Familiarity with Google Cloud, Linux, and GitHub is assumed.
This tutorial will help you with:
- Version control
- Continuous integration
- Continuous delivery
- Continuous testing
This tutorial uses GitHub branches—dev
and prod
—to represent environments. These environments are defined by Virtual Private Cloud (VPC) networks within a Google Cloud project.
The process starts when you push Terraform code to either the dev
or prod
branch. Cloud Build triggers and then applies Terraform manifests to achieve the desired state in the respective environment. For other branches, Cloud Build runs terraform plan
without applying any changes.
- Set up your GitHub repository.
- Configure Terraform to store state in a Cloud Storage bucket.
- Grant permissions to your Cloud Build service account.
- Connect Cloud Build to your GitHub repository.
- Change your environment configuration in a feature branch.
- Promote changes to the development environment.
- Promote changes to the production environment.
This tutorial uses the following billable components of Google Cloud:
- Cloud Build
- Cloud Storage
- Compute Engine
- Create a Google Cloud account and project.
- Ensure billing is enabled.
- Activate Cloud Shell.
- Fork the solutions-terraform-cloudbuild-gitops repository.
- Clone the forked repository:
cd ~ git clone https://github.com/YOUR_GITHUB_USERNAME/solutions-terraform-cloudbuild-gitops.git cd ~/solutions-terraform-cloudbuild-gitops
- Create a Cloud Storage bucket:
PROJECT_ID=$(gcloud config get-value project) gsutil mb gs://${PROJECT_ID}-tfstate gsutil versioning set on gs://${PROJECT_ID}-tfstate
- Replace the
PROJECT_ID
placeholder interraform.tfvars
andbackend.tf
files:cd ~/solutions-terraform-cloudbuild-gitops sed -i s/PROJECT_ID/$PROJECT_ID/g environments/*/terraform.tfvars sed -i s/PROJECT_ID/$PROJECT_ID/g environments/*/backend.tf
- Retrieve the email for your project's Cloud Build service account:
CLOUDBUILD_SA="$(gcloud projects describe $PROJECT_ID --format 'value(projectNumber)')@cloudbuild.gserviceaccount.com"
- Grant the required access:
gcloud projects add-iam-policy-binding $PROJECT_ID --member serviceAccount:$CLOUDBUILD_SA --role roles/editor
- Install the Cloud Build GitHub app and configure it to connect your repository.
- Create a new branch and fix a typo in the
modules/firewall/main.tf
file. - Create a pull request and review the terraform plan.
- Merge the pull request into the
dev
branch. - Verify the deployment and the terraform state in Cloud Storage.
- Create a pull request to merge the
dev
branch into theprod
branch. - Review the changes and merge the pull request.
- Verify the deployment and the terraform state in Cloud Storage.
To avoid continued billing, delete the resources you created.
- Go to the Manage resources page.
- Select your project and click Delete.
- Navigate to the main page of your forked repository.
- Go to Settings > Branches and delete branch protection rules.
- Optionally, uninstall the Cloud Build app from GitHub.
- Delete the repository from GitHub.
- Add deployments for separate use cases.
- Create additional environments.
- Use a project per environment.
For a detailed guide, refer to the complete tutorial.