Automating Deployments to GCP with Terraform

1.5 hours
  • 3 Learning Objectives

About this Hands-on Lab

You are a DevOps engineer at a company that is moving to automated deployments using Infrastructure as Code (IaC). The organization has embraced Terraform modules to develop reusable solutions, and wants to leverage common modules across environments to maintain consistency. You will need to set up a solution using Source Repositories and Cloud Build to automate the deployment of infrastructure using Terraform. The solution will need to support development and production branches and deploy separate infrastructure for each branch. That way, changes can be promoted from development to production entirely in version control.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Set Up a Cloud Source Repository (CSR) with the Terraform Code to Create a ‘dev’ Environment
  • Enable Source Repos and Cloud Build: gcloud services enable sourcerepo.googleapis.com cloudbuild.googleapis.com iam.googleapis.com
  • Create a bucket to store your project’s state: gsutil mb gs://<PROJECT ID>
  • Create a repository: gcloud source repos create ci-app
  • Check out the repo: gcloud source repos clone ci-app
  • Open the repo: cd ci-app
  • Create a dev branch: git checkout -b dev
  • Create modules/web and cd into the directory
  • Copy the module code into the web directory: wget https://raw.githubusercontent.com/linuxacademy/content-advanced-terraform-with-gcp/main/automation_lab/main.tf
  • Copy the template code into the web directory: wget https://raw.githubusercontent.com/linuxacademy/content-advanced-terraform-with-gcp/main/automation_lab/index.html
  • In the ci-app directory, create a dev directory with a main.tf file
  • Add a terraform block to dev/main.tf, setting the backend to the bucket you created
  • Create a web_app module from the module directory — be sure to set the env variable, and set an output for the host IP
  • In the ci-app directory, copy the cloudbuid.yaml file with: wget https://raw.githubusercontent.com/linuxacademy/content-advanced-terraform-with-gcp/main/automation_lab/cloudbuild.yaml
  • Set your git configuration: git config --global user.email "wes@not.real" and git config --global user.name "Wes"
  • Commit the code with: git add --a and git commit -m 'added module code'
  • Push the code to the CSR: git push --set-upstream origin dev
Configure a Cloud Build Trigger Connected to the CSR
  • In the GCP console, go to Cloud Build
  • Click on Triggers in the left column
  • Create a trigger
  • Run the trigger to deploy the dev infrastructure
  • Go to the IP output by the build run
Add a Branch and Configuration File to Create Infrastructure in the ‘prod’ Environment
  • In Cloud Shell, create a prod directory
  • Create prod/main.tf. Create a web_app module from the module directory — be sure to set the env variable, and set an output for the host IP
  • Commit and push the new code to a new upstream branch: origin/prod
  • View the build logs in the console
  • Go to the IP output by the build run

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?