Arcade Simulator: Cloud Infrastructure Engineer | GSP156

Solution for Arcade Simulator: Cloud Infrastructure Engineer | GSP156. 1 lab: GSP156. Fast copy-paste commands for Google Cloud.

GSP156 — Terraform Fundamentals

Estimated time: 10 minutes

# Terraform Fundamentals ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide helps learners understand infrastructure-as-code (IaC) using Terraform on Google Cloud. Always attempt the lab yourself first and follow Google Cloud Skills Boost / Qwiklabs Terms of Service. This walkthrough is not affiliated with or endorsed by Google, Google Cloud, or Qwiklabs. Never share cr

CYAN=

\033[0;96m'
GREEN=

\033[0;92m'
YELLOW=

\033[0;93m'
BLUE=

\033[0;94m'
MAGENTA=

\033[0;95m'
WHITE=

\033[0;97m'
BG_BLUE=

\033[44m'
BG_MAGENTA=

\033[45m'
BG_GREEN=

\033[42m'
BOLD=

\033[1m'
RESET=

\033[0m'

clear
echo "${CYAN}${BOLD}"
echo "   ____        _     _ _              __   ___              "
echo "  / __ \      | |   (_) |            / _| / _ \             "
echo " | |  | |_ __ | |__  _| |_   ___    | |_ | | | |_ __  ___   "
echo " | |  | | '__|| '_ \| | __| / _ \   |  _|| | | | '_ \/ __|  "
echo " | |__| | |   | |_) | | |_ | (_) |  | |  | |_| | |_) \__ \  "
echo "  \____/|_|   |_.__/|_|\__| \___/   |_|   \___/| .__/|___/  "
echo "                                               | |          "
echo "                                               |_|          "
echo "${RESET}"

echo "${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${CYAN}${BOLD}║          🚀 ORBIT OF OPS — GSP156 AUTOMATION: FULL LAB         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_BLUE}${WHITE}${BOLD} ❖ TASK 1: CONFIGURE HASHICORP REPO & INSTALL TERRAFORM CLI     ${RESET}"
echo ""

echo "${YELLOW}${BOLD}⚡ [STEP 1.1] Writing ~/.customize_environment & installing Terraform...${RESET}"
cat <<'EOF' > ~/.customize_environment
# Set up HashiCorp repository and install Terraform
wget -O - https://apt.releases.hashicorp.com/gpg | sudo gpg --batch --yes --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] https://apt.releases.hashicorp.com $(grep -oP '(?<=UBUNTU_CODENAME=).*' /etc/os-release || lsb_release -cs) main" | sudo tee /etc/apt/sources.list.d/hashicorp.list
sudo apt update && sudo apt install -y terraform
EOF

bash ~/.customize_environment

if [ -f /usr/local/bin/terraform ] && grep -q "developer.hashicorp.com" /usr/local/bin/terraform 2>/dev/null; then
  sudo rm -f /usr/local/bin/terraform
fi
hash -r

echo ""
echo "${CYAN}${BOLD}🔍 [STEP 1.2] Verifying installed Terraform version:${RESET}"
terraform --version
echo ""

echo "${BG_MAGENTA}${WHITE}${BOLD} ❖ TASK 2: BUILD & APPLY TERRAFORM VM INFRASTRUCTURE            ${RESET}"
echo ""

echo "${BLUE}${BOLD}🌐 [STEP 2.1] Detecting Project ID, Default Zone, and Debian Image...${RESET}"
export PROJECT_ID=$(gcloud config get-value project)
export ZONE=$(gcloud compute project-info describe --project="$PROJECT_ID" --format="value(commonInstanceMetadata.items[google-compute-default-zone])")

if [ -z "$ZONE" ]; then
  export ZONE=$(gcloud config get-value compute/zone 2>/dev/null)
fi
if [ -z "$ZONE" ]; then
  export ZONE="us-east4-c"
fi

export DEBIAN_IMAGE="debian-cloud/debian-12"

echo "${GREEN}${BOLD}  ✔ Project ID   : ${WHITE}${PROJECT_ID}${RESET}"
echo "${GREEN}${BOLD}  ✔ Target Zone  : ${WHITE}${ZONE}${RESET}"
echo "${GREEN}${BOLD}  ✔ Boot Image   : ${WHITE}${DEBIAN_IMAGE}${RESET}"
echo ""

echo "${YELLOW}${BOLD}📝 [STEP 2.2] Creating declarative configuration file 'instance.tf'...${RESET}"
rm -f *.tf
cat <<EOF > instance.tf
resource "google_compute_instance" "default" {
  project      = "${PROJECT_ID}"
  zone         = "${ZONE}"
  name         = "terraform"
  machine_type = "e2-medium"
  boot_disk {
    initialize_params {
      image = "${DEBIAN_IMAGE}"
    }
  }
  network_interface {
    network = "default"
    access_config {
    }
  }
}
EOF
ls -l instance.tf
echo "${GREEN}${BOLD}  ✔ Configuration 'instance.tf' generated.${RESET}"
echo ""

echo "${MAGENTA}${BOLD}⚙️  [STEP 2.3] Initializing Terraform Google provider plugin (terraform init)...${RESET}"
terraform init

echo ""
echo "${BLUE}${BOLD}📋 [STEP 2.4] Generating Terraform execution plan (terraform plan)...${RESET}"
terraform plan

echo ""
echo "${CYAN}${BOLD}🚀 [STEP 2.5] Provisioning VM instance 'terraform' in ${WHITE}${ZONE}${CYAN} (terraform apply)...${RESET}"
terraform apply -auto-approve

echo ""
echo "${YELLOW}${BOLD}🔎 [STEP 2.6] Inspecting deployed Terraform state (terraform show)...${RESET}"
terraform show

echo ""
echo "${GREEN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${GREEN}${BOLD}║      🏆 ALL GSP156 LAB TASKS COMPLETED 100% SUCCESSFULLY       ║${RESET}"
echo "${GREEN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""