Arcade Simulator: Cloud Infrastructure Engineer | GSP345

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

GSP345 — Build Infrastructure with Terraform on Google Cloud: Challenge Lab

Estimated time: 30 minutes

# 🚀 Build Infrastructure with Terraform on Google Cloud: Challenge Lab | GSP345 ## ⚠️ Disclaimer This is a community-made educational walkthrough created for learning, hands-on practice, and Google Cloud certification preparation. This guide is not affiliated with, endorsed by, or sponsored by Google, Google Cloud, or Qwiklabs. - Attempt the lab yourself and follow the official Google Cloud Skills Boost / Qwiklabs instructions and Terms of Service. - Never share credentials, API keys, servi

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 — GSP345 AUTOMATION: STAGE 1/2         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_BLUE}${WHITE}${BOLD} ❖ PREREQUISITE & INPUTS: TERRAFORM CLI & LAB PARAMETERS        ${RESET}"
echo ""

read -p "${YELLOW}${BOLD}🪣 Enter Bucket Name (from Task 3)       : ${WHITE}" INPUT_BUCKET
read -p "${MAGENTA}${BOLD}💻 Enter 3rd Instance Name (from Task 4) : ${WHITE}" INPUT_INSTANCE
read -p "${CYAN}${BOLD}🌐 Enter VPC Name (from Task 6)          : ${WHITE}" INPUT_VPC
echo "${RESET}"

export BUCKET=$(echo "$INPUT_BUCKET" | xargs)
export INSTANCE=$(echo "$INPUT_INSTANCE" | xargs)
export VPC=$(echo "$INPUT_VPC" | xargs)
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)

export ZONE=$(gcloud compute instances list --project="$PROJECT_ID" --filter="name=tf-instance-1" --format="value(zone)" 2>/dev/null | head -n 1)
if [ -z "$ZONE" ]; then
  export ZONE=$(gcloud compute project-info describe --project="$PROJECT_ID" --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)
fi
export REGION="${ZONE%-*}"

sed -i '/export BUCKET=/d' ~/.bashrc
sed -i '/export INSTANCE=/d' ~/.bashrc
sed -i '/export VPC=/d' ~/.bashrc
sed -i '/export PROJECT_ID=/d' ~/.bashrc
sed -i '/export ZONE=/d' ~/.bashrc
sed -i '/export REGION=/d' ~/.bashrc
echo "export BUCKET=$BUCKET" >> ~/.bashrc
echo "export INSTANCE=$INSTANCE" >> ~/.bashrc
echo "export VPC=$VPC" >> ~/.bashrc
echo "export PROJECT_ID=$PROJECT_ID" >> ~/.bashrc
echo "export ZONE=$ZONE" >> ~/.bashrc
echo "export REGION=$REGION" >> ~/.bashrc

echo "${GREEN}${BOLD}  ✔ Project ID   : ${WHITE}${PROJECT_ID}${RESET}"
echo "${GREEN}${BOLD}  ✔ Region / Zone: ${WHITE}${REGION} / ${ZONE}${RESET}"
echo "${GREEN}${BOLD}  ✔ Bucket Name  : ${WHITE}${BUCKET}${RESET}"
echo "${GREEN}${BOLD}  ✔ 3rd Instance : ${WHITE}${INSTANCE}${RESET}"
echo "${GREEN}${BOLD}  ✔ VPC Name     : ${WHITE}${VPC}${RESET}"
echo ""

echo "${YELLOW}${BOLD}⚡ [STEP 1.1] Configuring ~/.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
terraform --version
echo ""

echo "${BG_MAGENTA}${WHITE}${BOLD} ❖ TASK 1: CREATE CONFIGURATION FILES & DIRECTORY STRUCTURE     ${RESET}"
echo ""

cd ~
rm -rf ~/.terraform ~/modules .terraform* *.tf
touch main.tf variables.tf
mkdir -p modules/instances modules/storage
touch modules/instances/instances.tf modules/instances/outputs.tf modules/instances/variables.tf
touch modules/storage/storage.tf modules/storage/outputs.tf modules/storage/variables.tf

cat <<EOF > variables.tf
variable "region" {
  default = "${REGION}"
}

variable "zone" {
  default = "${ZONE}"
}

variable "project_id" {
  default = "${PROJECT_ID}"
}
EOF

cp variables.tf modules/instances/variables.tf
cp variables.tf modules/storage/variables.tf

cat <<'EOF' > main.tf
terraform {
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 6.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
  zone    = var.zone
}
EOF

echo "${BLUE}${BOLD}⚙️  [STEP 1.2] Initializing root Terraform configuration...${RESET}"
terraform init
echo ""

echo "${BG_BLUE}${WHITE}${BOLD} ❖ TASK 2: IMPORT EXISTING INFRASTRUCTURE (TF-INSTANCE-1 & 2)   ${RESET}"
echo ""

echo "${YELLOW}${BOLD}🔍 [STEP 2.1] Inspecting pre-existing instances in ${WHITE}${ZONE}${YELLOW}...${RESET}"
export ID_1=$(gcloud compute instances describe tf-instance-1 --zone="$ZONE" --project="$PROJECT_ID" --format="value(id)")
export ID_2=$(gcloud compute instances describe tf-instance-2 --zone="$ZONE" --project="$PROJECT_ID" --format="value(id)")
export MT_1=$(gcloud compute instances describe tf-instance-1 --zone="$ZONE" --project="$PROJECT_ID" --format="value(machineType.basename())")
export MT_2=$(gcloud compute instances describe tf-instance-2 --zone="$ZONE" --project="$PROJECT_ID" --format="value(machineType.basename())")

echo "${GREEN}${BOLD}  ✔ tf-instance-1 : ID=${WHITE}${ID_1}${GREEN} (Type: ${WHITE}${MT_1}${GREEN})${RESET}"
echo "${GREEN}${BOLD}  ✔ tf-instance-2 : ID=${WHITE}${ID_2}${GREEN} (Type: ${WHITE}${MT_2}${GREEN})${RESET}"

cat <<'EOF' >> main.tf

module "instances" {
  source = "./modules/instances"
}
EOF

cat <<EOF > modules/instances/instances.tf
resource "google_compute_instance" "tf-instance-1" {
  name         = "tf-instance-1"
  machine_type = "${MT_1}"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}

resource "google_compute_instance" "tf-instance-2" {
  name         = "tf-instance-2"
  machine_type = "${MT_2}"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}
EOF

echo "${CYAN}${BOLD}📥 [STEP 2.2] Initializing module & importing tf-instance-1 and tf-instance-2...${RESET}"
terraform init
terraform import module.instances.google_compute_instance.tf-instance-1 "$ID_1"
terraform import module.instances.google_compute_instance.tf-instance-2 "$ID_2"
terraform plan
terraform apply -auto-approve
echo ""

echo "${BG_MAGENTA}${WHITE}${BOLD} ❖ TASK 3: CONFIGURE REMOTE CLOUD STORAGE BACKEND               ${RESET}"
echo ""

echo "${BLUE}${BOLD}🪣 [STEP 3.1] Creating storage module for bucket '${WHITE}${BUCKET}${BLUE}'...${RESET}"
cat <<EOF > modules/storage/storage.tf
resource "google_storage_bucket" "storage-bucket" {
  name                        = "${BUCKET}"
  location                    = "US"
  force_destroy               = true
  uniform_bucket_level_access = true
}
EOF

cat <<'EOF' >> main.tf

module "storage" {
  source = "./modules/storage"
}
EOF

terraform init
terraform apply -auto-approve

echo "${CYAN}${BOLD}☁️  [STEP 3.2] Migrating Terraform state to GCS backend (${WHITE}gs://${BUCKET}/terraform/state${CYAN})...${RESET}"
cat <<EOF > main.tf
terraform {
  backend "gcs" {
    bucket = "${BUCKET}"
    prefix = "terraform/state"
  }
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 6.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
  zone    = var.zone
}

module "instances" {
  source = "./modules/instances"
}

module "storage" {
  source = "./modules/storage"
}
EOF

echo "yes" | terraform init -migrate-state
echo ""

echo "${BG_BLUE}${WHITE}${BOLD} ❖ TASK 4: MODIFY & UPDATE INFRASTRUCTURE (ADD 3RD INSTANCE)    ${RESET}"
echo ""

echo "${MAGENTA}${BOLD}🚀 [STEP 4.1] Updating instances to e2-standard-2 & creating '${WHITE}${INSTANCE}${MAGENTA}'...${RESET}"
cat <<EOF > modules/instances/instances.tf
resource "google_compute_instance" "tf-instance-1" {
  name         = "tf-instance-1"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}

resource "google_compute_instance" "tf-instance-2" {
  name         = "tf-instance-2"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}

resource "google_compute_instance" "${INSTANCE}" {
  name         = "${INSTANCE}"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}
EOF

terraform init
terraform apply -auto-approve

echo ""
echo "${BG_GREEN}${WHITE}${BOLD} ✔ STAGE 1 COMPLETE! CLICK 'CHECK MY PROGRESS' FOR TASKS 2, 3 & 4 ${RESET}"
echo "${YELLOW}${BOLD}⚠ IMPORTANT: Verify Task 2, Task 3, and Task 4 on the lab page BEFORE running Command 2!${RESET}"
echo ""
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'

. ~/.bashrc
cd ~

echo ""
echo "${CYAN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${CYAN}${BOLD}║         🚀 ORBIT OF OPS — GSP345 AUTOMATION: STAGE 2/2         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_MAGENTA}${WHITE}${BOLD} ❖ TASK 5: DESTROY 3RD INSTANCE (${INSTANCE})          ${RESET}"
echo ""

echo "${YELLOW}${BOLD}🧹 [STEP 5.1] Removing '${WHITE}${INSTANCE}${YELLOW}' from modules/instances/instances.tf & applying...${RESET}"
cat <<'EOF' > modules/instances/instances.tf
resource "google_compute_instance" "tf-instance-1" {
  name         = "tf-instance-1"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}

resource "google_compute_instance" "tf-instance-2" {
  name         = "tf-instance-2"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network = "default"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}
EOF

terraform init
terraform apply -auto-approve
echo "${GREEN}${BOLD}  ✔ Instance '${INSTANCE}' destroyed.${RESET}"
echo ""

echo "${BG_BLUE}${WHITE}${BOLD} ❖ TASK 6: USE REGISTRY NETWORK MODULE (v10.0.0) & SUBNETS      ${RESET}"
echo ""

echo "${CYAN}${BOLD}🌐 [STEP 6.1] Adding Registry Network Module (v10.0.0) for VPC '${WHITE}${VPC}${CYAN}'...${RESET}"
cat <<EOF > main.tf
terraform {
  backend "gcs" {
    bucket = "${BUCKET}"
    prefix = "terraform/state"
  }
  required_providers {
    google = {
      source  = "hashicorp/google"
      version = "~> 6.0"
    }
  }
}

provider "google" {
  project = var.project_id
  region  = var.region
  zone    = var.zone
}

module "instances" {
  source = "./modules/instances"
}

module "storage" {
  source = "./modules/storage"
}

module "vpc" {
  source  = "terraform-google-modules/network/google"
  version = "10.0.0"

  project_id   = "${PROJECT_ID}"
  network_name = "${VPC}"
  routing_mode = "GLOBAL"

  subnets = [
    {
      subnet_name   = "subnet-01"
      subnet_ip     = "10.10.10.0/24"
      subnet_region = "${REGION}"
    },
    {
      subnet_name   = "subnet-02"
      subnet_ip     = "10.10.20.0/24"
      subnet_region = "${REGION}"
    }
  ]
}
EOF

terraform init
terraform apply -auto-approve

echo "${MAGENTA}${BOLD}🔌 [STEP 6.2] Attaching tf-instance-1 to subnet-01 & tf-instance-2 to subnet-02...${RESET}"
cat <<EOF > modules/instances/instances.tf
resource "google_compute_instance" "tf-instance-1" {
  name         = "tf-instance-1"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network    = "${VPC}"
    subnetwork = "subnet-01"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}

resource "google_compute_instance" "tf-instance-2" {
  name         = "tf-instance-2"
  machine_type = "e2-standard-2"
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = "debian-cloud/debian-12"
    }
  }

  network_interface {
    network    = "${VPC}"
    subnetwork = "subnet-02"
  }

  metadata_startup_script = <<-EOT
        #!/bin/bash
    EOT
  allow_stopping_for_update = true
}
EOF

terraform init
terraform apply -auto-approve
echo ""

echo "${BG_MAGENTA}${WHITE}${BOLD} ❖ TASK 7: CONFIGURE FIREWALL RULE (TF-FIREWALL)                ${RESET}"
echo ""

echo "${BLUE}${BOLD}🛡️  [STEP 7.1] Creating 'tf-firewall' on network 'projects/${PROJECT_ID}/global/networks/${VPC}'...${RESET}"
cat <<EOF >> main.tf

resource "google_compute_firewall" "tf-firewall" {
  name    = "tf-firewall"
  network = "projects/${PROJECT_ID}/global/networks/${VPC}"

  allow {
    protocol = "tcp"
    ports    = ["80"]
  }

  source_tags   = ["web"]
  source_ranges = ["0.0.0.0/0"]
}
EOF

terraform init
terraform apply -auto-approve

echo ""
echo "${GREEN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${GREEN}${BOLD}║      🏆 ALL GSP345 CHALLENGE LAB TASKS COMPLETED 100%          ║${RESET}"
echo "${GREEN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo "${BG_GREEN}${WHITE}${BOLD} ✔ CLICK 'CHECK MY PROGRESS' FOR TASKS 5, 6 & 7 NOW             ${RESET}"
echo ""