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
  • 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
  • Copy the template code into the web directory: wget
  • In the ci-app directory, create a dev directory with a file
  • Add a terraform block to dev/, 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
  • Set your git configuration: git config --global "wes@not.real" and git config --global "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/ 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?