Cloud Spanner - Database Fundamentals

Solution for Cloud Spanner - Database Fundamentals. 1 lab: GSP1048. Fast copy-paste commands for Google Cloud.

GSP1048 — Cloud Spanner - Database Fundamentals

Estimated time: 30 Minutes

# 🗄️ Cloud Spanner - Database Fundamentals > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created to help you understand why each step works. Attempt the lab yourself first. This guide is provided for educational purposes and is intended to support hands-on learning with Google Cloud services. It is not intended to replace the official lab instructions or your own understanding of the concepts. This walkthrough is not affiliated with or endorsed by Google Cloud or Goog

GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
MAGENTA='\e[1;35m'
WHITE='\e[1;37m'
RESET='\e[0m'
BOLD='\e[1m'

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

echo -e "${BLUE}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BLUE}${BOLD}║   🌊 WELCOME TO Orbit Of Ops                               ║${RESET}"
echo -e "${BLUE}${BOLD}║   🚀 TARGET: GSP1048 CLOUD SPANNER - GETTING STARTED       ║${RESET}"
echo -e "${BLUE}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}\n"

# ==============================================================================
# PRE-FLIGHT CHECKS & VARIABLES
# ==============================================================================
echo -e "${BOLD}${YELLOW}[Orbit of Ops] Auto-fetching configurations...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)
export ZONE=$(gcloud compute project-info describe --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)

# Fallback in case zone metadata is missing
if [[ -z "$ZONE" ]]; then
    read -p "$(echo -e ${BOLD}${CYAN}"Please enter the lab Zone (e.g., us-east1-b): "${RESET})" ZONE
    export ZONE
fi

export REGION=${ZONE%-*}

echo -e "✅ Project ID: ${GREEN}$PROJECT_ID${RESET}"
echo -e "✅ Zone:       ${GREEN}$ZONE${RESET}"
echo -e "✅ Region:     ${GREEN}$REGION${RESET}\n"

# ==============================================================================
# TASKS 1 & 2: CREATE FIRST INSTANCE & DATABASE
# ==============================================================================
echo -e "${GREEN}${BOLD}▬▬▬▬▬▬ TASK 1 & 2: CREATE INSTANCE & DATABASE ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Creating Spanner Instance (banking-instance) with 1 node...${RESET}"
gcloud spanner instances create banking-instance \
    --config=regional-$REGION \
    --description="Banking Instance" \
    --nodes=1 \
    --quiet

echo -e "${YELLOW}[*] Creating Spanner Database (banking-db)...${RESET}"
gcloud spanner databases create banking-db \
    --instance=banking-instance \
    --quiet

# ==============================================================================
# TASK 3: CREATE SCHEMA
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 3: CREATE SCHEMA ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Building 'Customer' table...${RESET}"
gcloud spanner databases ddl update banking-db \
    --instance=banking-instance \
    --ddl="CREATE TABLE Customer (CustomerId STRING(36) NOT NULL, Name STRING(MAX) NOT NULL, Location STRING(MAX) NOT NULL) PRIMARY KEY (CustomerId);" \
    --quiet

# ==============================================================================
# TASK 4: INSERT DATA
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 4: INSERT DATA ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Inserting 2 required rows into Customer table...${RESET}"
gcloud spanner databases execute-sql banking-db \
    --instance=banking-instance \
    --sql="INSERT INTO Customer (CustomerId, Name, Location) VALUES ('bdaaaa97-1b4b-4e58-b4ad-84030de92235', 'Richard Nelson', 'Ada Ohio'), ('b2b4002d-7813-4551-b83b-366ef95f9273', 'Shana Underwood', 'Ely Iowa');"

# ==============================================================================
# TASK 5: CLI SECOND INSTANCE
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 5: CLI INSTANCE & DATABASE ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Creating 2nd Spanner Instance (banking-instance-2) with 2 nodes...${RESET}"
gcloud spanner instances create banking-instance-2 \
    --config=regional-$REGION \
    --description="Banking Instance 2" \
    --nodes=2 \
    --quiet

echo -e "${YELLOW}[*] Creating 2nd Spanner Database (banking-db-2)...${RESET}"
gcloud spanner databases create banking-db-2 \
    --instance=banking-instance-2 \
    --quiet

echo -e "\n${MAGENTA}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${MAGENTA}${BOLD}║           🎉 AUTOMATION COMPLETED SUCCESSFULLY 🎉          ║${RESET}"
echo -e "${MAGENTA}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}"
echo -e "${CYAN}${BOLD}You may now click 'Check my progress' on all tasks to claim your 100/100!${RESET}\n"