Establish Hybrid Network Connectivity with NCC

Solution for Establish Hybrid Network Connectivity with NCC. 1 lab: GSP1318. Fast copy-paste commands for Google Cloud.

GSP1318 — Establish Hybrid Network Connectivity with NCC

Estimated time: 30 minutes

# 🌐 Hybrid Connectivity with Network Connectivity Center > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created to help you understand why each step works. Attempt the challenge yourself first. This guide is provided for educational purposes and is not intended to replace the official lab instructions or your own hands-on learning. It is not affiliated with or endorsed by Google Cloud or Google Cloud Skills Boost. Always follow the official Google Cloud and Qwiklabs te

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: GSP1318 HYBRID CONNECTIVITY WITH NCC          ║${RESET}"
echo -e "${BLUE}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}\n"

# ==============================================================================
# PRE-FLIGHT CHECKS & VARIABLES
# ==============================================================================
echo -e "${BOLD}${YELLOW}[Orbit of Ops] Auto-fetching Project, Zone, and Region...${RESET}"
export PROJECT_ID=$(gcloud config get-value project 2>/dev/null)

export ZONE=$(gcloud compute instances list --filter="name=vm3-onprem" --format="value(zone)" 2>/dev/null | head -n 1)
if [[ -z "$ZONE" ]]; then
    read -p "$(echo -e ${BOLD}${CYAN}"Could not detect zone. Please enter the lab Zone (e.g., us-east1-b): "${RESET})" ZONE
    export ZONE
fi
export REGION=${ZONE%-*}

gcloud config set compute/zone $ZONE 2>/dev/null
gcloud config set compute/region $REGION 2>/dev/null

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

echo -e "${YELLOW}[*] Enabling Network Connectivity API...${RESET}"
gcloud services enable networkconnectivity.googleapis.com --quiet

# ==============================================================================
# TASK 1: HYBRID CONNECTIVITY (VPN & BGP)
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 1: HYBRID CONNECTIVITY (VPN & BGP) ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Configuring Cloud Routers...${RESET}"
gcloud compute routers create "routing-vpc-cr" --region="${REGION}" --network="routing-vpc" --asn="64525" --quiet
gcloud compute routers create "on-prem-router" --region="${REGION}" --network="on-prem-net-vpc" --asn="64526" --quiet

echo -e "${YELLOW}[*] Configuring VPN Gateways...${RESET}"
gcloud compute vpn-gateways create "routing-vpc-vpn-gateway" --region="${REGION}" --network="routing-vpc" --quiet
gcloud compute vpn-gateways create "on-prem-vpn-gateway" --region="${REGION}" --network="on-prem-net-vpc" --quiet

echo -e "${YELLOW}[*] Generating Shared Secret and creating VPN Tunnels...${RESET}"
secret_key=$(openssl rand -base64 24)

# 🛠️ FIXED: --peer-gcp-gateway is now correctly set to on-prem-vpn-gateway
gcloud compute vpn-tunnels create "routing-vpc-tunnel" \
  --vpn-gateway="routing-vpc-vpn-gateway" \
  --peer-gcp-gateway="on-prem-vpn-gateway" \
  --router="routing-vpc-cr" \
  --region="${REGION}" \
  --interface=0 \
  --shared-secret="${secret_key}" --quiet

gcloud compute vpn-tunnels create "on-prem-tunnel" \
  --vpn-gateway="on-prem-vpn-gateway" \
  --peer-gcp-gateway="routing-vpc-vpn-gateway" \
  --router="on-prem-router" \
  --region="${REGION}" \
  --interface=0 \
  --shared-secret="${secret_key}" --quiet

echo -e "${YELLOW}[*] Adding Router Interfaces and BGP Peers...${RESET}"
gcloud compute routers add-interface "routing-vpc-cr" --interface-name="if-hub-to-prem" --ip-address="169.254.1.1" --mask-length=30 --vpn-tunnel="routing-vpc-tunnel" --region="${REGION}" --quiet
gcloud compute routers add-bgp-peer "routing-vpc-cr" --peer-name="bgp-hub-to-prem" --peer-ip-address="169.254.1.2" --interface="if-hub-to-prem" --peer-asn="64526" --region="${REGION}" --quiet

gcloud compute routers add-interface "on-prem-router" --interface-name="if-prem-to-hub" --ip-address="169.254.1.2" --mask-length=30 --vpn-tunnel="on-prem-tunnel" --region="${REGION}" --quiet
gcloud compute routers add-bgp-peer "on-prem-router" --peer-name="bgp-prem-to-hub" --peer-ip-address="169.254.1.1" --interface="if-prem-to-hub" --peer-asn="64525" --region="${REGION}" --quiet

echo -e "${YELLOW}[*] Updating BGP Prefix Advertisements...${RESET}"
gcloud compute routers update "routing-vpc-cr" --advertisement-mode custom --set-advertisement-groups=all_subnets --set-advertisement-ranges="10.0.1.0/24" --region="${REGION}" --quiet
gcloud compute routers update "on-prem-router" --advertisement-mode custom --set-advertisement-groups=all_subnets --region="${REGION}" --quiet
gcloud compute routers update-bgp-peer "on-prem-router" --peer-name="bgp-prem-to-hub" --advertised-route-priority="111" --region="${REGION}" --quiet

# ==============================================================================
# TASK 2: NCC HUB
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 2: NETWORK CONNECTIVITY CENTER HUB ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Creating NCC Hub (mesh-hub)...${RESET}"
gcloud network-connectivity hubs create "mesh-hub" --quiet

# ==============================================================================
# TASK 3: NCC HYBRID AND VPC SPOKES
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 3: NCC HYBRID AND VPC SPOKES ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Configuring Workload VPC as an NCC Spoke...${RESET}"
gcloud network-connectivity spokes linked-vpc-network create "workload-vpc-spoke" \
  --hub="mesh-hub" \
  --vpc-network="workload-vpc" \
  --global --quiet

echo -e "${YELLOW}[*] Configuring VPN Tunnel as an NCC Hybrid Spoke...${RESET}"
gcloud network-connectivity spokes linked-vpn-tunnels create "hybrid-spoke" \
  --region="${REGION}" \
  --hub="mesh-hub" \
  --vpn-tunnels="routing-vpc-tunnel" --quiet

# ==============================================================================
# TASK 4: VERIFY DATA PATH
# ==============================================================================
echo -e "\n${GREEN}${BOLD}▬▬▬▬▬▬ TASK 4: VERIFY DATA PATH ▬▬▬▬▬▬${RESET}"
echo -e "${CYAN}[Orbit of Ops] BGP routes take ~60-90 seconds to propagate across NCC.${RESET}"
echo -e "${YELLOW}[*] SSHing into vm3-onprem to poll 10.0.1.2 automatically...${RESET}"

gcloud compute ssh vm3-onprem --zone=$ZONE --quiet --command="
  echo -e '\nTesting connection to Workload VPC (10.0.1.2)...'
  while ! curl -s --connect-timeout 2 10.0.1.2 > /dev/null; do 
    echo '⏳ Waiting for BGP convergence... Retrying in 10 seconds...'
    sleep 10
  done
  echo -e '✅ Data path verified! Connectivity successfully established.\n'
"

echo -e "\n${MAGENTA}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${MAGENTA}${BOLD}║           🎉 PART 1 COMPLETED SUCCESSFULLY 🎉              ║${RESET}"
echo -e "${MAGENTA}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}"
echo -e "${CYAN}${BOLD}⚠️ CRITICAL: Check your progress in Qwiklabs NOW and wait until you have 100/100 points BEFORE running Part 2! ⚠️${RESET}\n"
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RESET='\e[0m'
BOLD='\e[1m'

export ZONE=$(gcloud compute instances list --filter="name=vm3-onprem" --format="value(zone)" 2>/dev/null | head -n 1)
export REGION=${ZONE%-*}

echo -e "${GREEN}${BOLD}▬▬▬▬▬▬ TASK 5: CLEANUP RESOURCES ▬▬▬▬▬▬${RESET}"
echo -e "${YELLOW}[*] Deleting NCC Spokes...${RESET}"
gcloud network-connectivity spokes delete workload-vpc-spoke --global --quiet
gcloud network-connectivity spokes delete hybrid-spoke --region=$REGION --quiet

echo -e "${YELLOW}[*] Deleting NCC Hub...${RESET}"
gcloud network-connectivity hubs delete mesh-hub --quiet

echo -e "${YELLOW}[*] Deleting HA-VPN Tunnels...${RESET}"
gcloud compute vpn-tunnels delete on-prem-tunnel --region=$REGION --quiet
gcloud compute vpn-tunnels delete routing-vpc-tunnel --region=$REGION --quiet

echo -e "${YELLOW}[*] Deleting Cloud Routers...${RESET}"
gcloud compute routers delete routing-vpc-cr --region=$REGION --quiet
gcloud compute routers delete on-prem-router --region=$REGION --quiet

echo -e "\n${MAGENTA}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${MAGENTA}${BOLD}║            🎉 LAB COMPLETED SUCCESSFULLY 🎉                ║${RESET}"
echo -e "${MAGENTA}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}"