- Lab
- A Cloud Guru
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.
Path Info
Table of Contents
-
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
andcd
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 adev
directory with amain.tf
file - Add a
terraform
block todev/main.tf
, setting the backend to the bucket you created - Create a
web_app
module from the module directory — be sure to set theenv
variable, and set an output for the host IP - In the
ci-app
directory, copy thecloudbuid.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]"
andgit config --global user.name "Wes"
- Commit the code with:
git add --a
andgit commit -m 'added module code'
- Push the code to the CSR:
git push --set-upstream origin dev
- Enable Source Repos and Cloud Build:
-
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
-
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 aweb_app
module from the module directory — be sure to set theenv
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
- In Cloud Shell, create a
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.