Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Labs

Automating Deployments to GCP with Terraform

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.

Labs

Path Info

Level
Clock icon Advanced
Duration
Clock icon 1h 30m
Published
Clock icon Apr 08, 2022

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    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/ACloudGuru-Resources/content-advanced-terraform-with-gcp/main/automation_lab/main.tf
    • Copy the template code into the web directory: wget https://raw.githubusercontent.com/ACloudGuru-Resources/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/ACloudGuru-Resources/content-advanced-terraform-with-gcp/main/automation_lab/cloudbuild.yaml
    • Set your git configuration: git config --global user.email "[email protected]" 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
  2. Challenge

    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
  3. Challenge

    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

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

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.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans