Arcade Simulator: Cloud Infrastructure Engineer | GSP752

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

GSP752 — Manage Terraform State

Estimated time: 15 minutes

# Manage Terraform State | GSP752 ⚠️ **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 Terraform state management, local and remote backends, state migration, and state refresh operations 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

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 — GSP752 AUTOMATION: FULL LAB         ║${RESET}"
echo "${CYAN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo ""
echo "${BG_BLUE}${WHITE}${BOLD} ❖ PREREQUISITE: CONFIGURE HASHICORP REPO & INSTALL TERRAFORM    ${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 1: LOCAL BACKEND, GCS STATE MIGRATION & STATE REFRESH   ${RESET}"
echo ""

echo "${BLUE}${BOLD}🌐 [STEP 2.1] Detecting Project ID and Region...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
if [ -z "$PROJECT_ID" ]; then
  export PROJECT_ID="$DEVSHELL_PROJECT_ID"
fi

export ZONE=$(gcloud compute project-info describe --project="$PROJECT_ID" --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)
export REGION=$(gcloud compute project-info describe --project="$PROJECT_ID" --format="value(commonInstanceMetadata.items[google-compute-default-region])" 2>/dev/null | tail -n 1)

if [ -z "$REGION" ] && [ -n "$ZONE" ]; then
  export REGION="${ZONE%-*}"
fi
if [ -z "$REGION" ]; then
  export REGION=$(gcloud config get-value compute/region 2>/dev/null)
fi
if [ -z "$REGION" ]; then
  export REGION="us-central1"
fi

echo "${GREEN}${BOLD}  ✔ Project ID / Bucket : ${WHITE}${PROJECT_ID}${RESET}"
echo "${GREEN}${BOLD}  ✔ Region              : ${WHITE}${REGION}${RESET}"
echo ""

echo "${YELLOW}${BOLD}📝 [STEP 2.2] Creating main.tf with 'local' backend...${RESET}"
cd ~
rm -rf ~/.terraform ~/terraform .terraform* *.tf
cat <<EOF > main.tf
provider "google" {
  project     = "${PROJECT_ID}"
  region      = "${REGION}"
}

resource "google_storage_bucket" "test-bucket-for-state" {
  name        = "${PROJECT_ID}"
  location    = "US"
  uniform_bucket_level_access = true
}

terraform {
  backend "local" {
    path = "terraform/state/terraform.tfstate"
  }
}
EOF

echo "${MAGENTA}${BOLD}🚀 [STEP 2.3] Initializing & applying 'local' backend configuration...${RESET}"
terraform init
terraform apply -auto-approve
terraform show
echo ""

echo "${BLUE}${BOLD}☁️  [STEP 2.4] Updating main.tf to use 'gcs' remote backend...${RESET}"
cat <<EOF > main.tf
provider "google" {
  project     = "${PROJECT_ID}"
  region      = "${REGION}"
}

resource "google_storage_bucket" "test-bucket-for-state" {
  name        = "${PROJECT_ID}"
  location    = "US"
  uniform_bucket_level_access = true
}

terraform {
  backend "gcs" {
    bucket  = "${PROJECT_ID}"
    prefix  = "terraform/state"
  }
}
EOF

echo "${CYAN}${BOLD}🔄 [STEP 2.5] Migrating state from local to gs://${PROJECT_ID}/terraform/state...${RESET}"
echo "yes" | terraform init -migrate-state
echo ""

echo "${YELLOW}${BOLD}🏷️  [STEP 2.6] Adding label 'key=value' to gs://${PROJECT_ID} & refreshing Terraform state...${RESET}"
gcloud storage buckets update "gs://${PROJECT_ID}" --update-labels=key=value
terraform refresh
terraform show

echo ""
echo "${GREEN}${BOLD}╔════════════════════════════════════════════════════════════════╗${RESET}"
echo "${GREEN}${BOLD}║      🏆 ALL GSP752 GRADED TASKS COMPLETED 100% SUCCESSFULLY    ║${RESET}"
echo "${GREEN}${BOLD}╚════════════════════════════════════════════════════════════════╝${RESET}"
echo "${BG_GREEN}${WHITE}${BOLD} ✔ CLICK 'CHECK MY PROGRESS' ON 'WORK WITH BACKENDS' NOW!       ${RESET}"
echo ""