Deploy and Manage Apigee X
Solution for Deploy and Manage Apigee X. 1 lab: GSP349. Fast copy-paste commands for Google Cloud.
GSP349 — Deploy and Manage Apigee X: Challenge Lab
Estimated time: 45 minutes
# 🌐 Deploy and Manage Apigee X: Challenge Lab ⚠️ **Disclaimer:** This guide is for educational purposes only, intended to help you understand Google Cloud services and prepare for certifications. Always follow Qwiklabs' terms of service and YouTube's community guidelines. --- ## 🛠️ Step-by-Step Guide ### 🔹 Step 1 — Access Routing (Task 2 - UI Required) > ⚠️ **Important:** Complete this step in the Google Cloud Console **before running the automation script**. This step creates the Global
clear
CYAN='\e[1;36m'
BLUE='\e[1;34m'
RESET='\e[0m'
BOLD='\e[1m'
RED='\e[1;31m'
GREEN='\e[1;32m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
echo -e "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ ___ __ ___
/ __ \ | | (_) | / _ \ / _|/ _ \
| | | |_ __| |__ _| |_| | | | |_| | | |_ __ ___
| | | | '__| '_ \| | __| | | | _| | | | '_ \/ __|
| |__| | | | |_) | | |_| |_| | | | |_| | |_) \__ \
\____/|_| |_.__/|_|\__|\___/|_| \___/| .__/|___/
| |
|_|
EOF
echo -e "${RESET}"
echo -e "${BLUE}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${BLUE}${BOLD}║ 🚀 BROUGHT TO YOU BY ORBIT OF OPS ║${RESET}"
echo -e "${BLUE}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}\n"
export GOOGLE_CLOUD_PROJECT=$DEVSHELL_PROJECT_ID
export PROJECT_ID=$DEVSHELL_PROJECT_ID
export TOKEN=$(gcloud auth print-access-token)
export INSTANCE_NAME=eval-instance
echo -e "${YELLOW}${BOLD}Starting Automation for Tasks 1, 3, 4, and 5...${RESET}\n"
# ---------------------------------------------------------
# TASK 1: Create Environment & Group
# ---------------------------------------------------------
echo -e "${CYAN}Task 1: Creating 'staging' environment and environment group...${RESET}"
curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-X POST "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/environments" \
-d '{"name":"staging", "apiProxyType":"PROGRAMMABLE", "deploymentType":"PROXY"}' > /dev/null
curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-X POST "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/envgroups" \
-d '{"name":"staging-group", "hostnames":["staging.example.com"]}' > /dev/null
echo "Waiting 20 seconds for Google Cloud to register the new resources..."
sleep 20
curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-X POST "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/envgroups/staging-group/attachments" \
-d '{"environment": "staging"}' > /dev/null
echo -e "${GREEN}✅ Task 1 Completed.${RESET}\n"
# ---------------------------------------------------------
# TASK 3: Create and Activate NAT IP
# ---------------------------------------------------------
echo -e "${CYAN}Task 3: Creating and activating NAT address (apigee-nat-ip)...${RESET}"
curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-X POST "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/instances/$INSTANCE_NAME/natAddresses" \
-d '{"name":"apigee-nat-ip"}' > /dev/null
echo "Waiting 35 seconds for Apigee to provision the IP before activating..."
sleep 35
curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-X POST "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/instances/$INSTANCE_NAME/natAddresses/apigee-nat-ip:activate" \
-d '{}' > /dev/null
echo -e "${GREEN}✅ NAT IP Created and Activated.${RESET}\n"
# ---------------------------------------------------------
# TASK 4: Create Cloud Armor policy & attach to GLB
# ---------------------------------------------------------
echo -e "${CYAN}Task 4: Creating Cloud Armor policy 'protect-apigee'...${RESET}"
gcloud compute security-policies create protect-apigee --description "Protect Apigee from RCE" --quiet
gcloud compute security-policies rules create 1000 \
--security-policy protect-apigee \
--expression "evaluatePreconfiguredExpr('rce-stable')" \
--action "deny-403" --quiet
echo "Locating Backend Service..."
BACKEND_SERVICE=$(gcloud compute backend-services list --global --format="value(name)" | head -n 1)
if [[ -z "$BACKEND_SERVICE" ]]; then
echo -e "${RED}ERROR: Backend service not found. Make sure you finished Task 2 in the UI!${RESET}"
else
gcloud compute backend-services update $BACKEND_SERVICE --security-policy protect-apigee --global --quiet
echo -e "${GREEN}✅ Cloud Armor attached securely.${RESET}\n"
fi
# ---------------------------------------------------------
# TASK 5: Attach the staging environment to the runtime instance
# ---------------------------------------------------------
echo -e "${CYAN}Task 5: Attaching 'staging' environment to eval-instance...${RESET}"
curl -s -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-X POST "https://apigee.googleapis.com/v1/organizations/$PROJECT_ID/instances/$INSTANCE_NAME/attachments" \
-d '{ "environment": "staging" }' > /dev/null
export ATTACHING_ENV=staging
echo -n "Waiting for $ATTACHING_ENV attachment to finalize (this usually takes 2-5 minutes)"
while : ; do
export ATTACHMENT_DONE=$(curl -s -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-X GET "https://apigee.googleapis.com/v1/organizations/${PROJECT_ID}/instances/${INSTANCE_NAME}/attachments" | \
jq -r "select(.attachments != null) | .attachments[] | select(.environment == \"${ATTACHING_ENV}\") | .environment")
if [[ "${ATTACHMENT_DONE}" == "${ATTACHING_ENV}" ]]; then
break
fi
echo -n "."
sleep 15
done
echo -e "\n${GREEN}✅ *** ${ATTACHING_ENV^^} ENVIRONMENT ATTACHED SUCCESSFULLY ***${RESET}\n"
echo -e "${MAGENTA}${BOLD}╔════════════════════════════════════════════════════════════╗${RESET}"
echo -e "${MAGENTA}${BOLD}║ 🎉 AUTOMATION COMPLETED SUCCESSFULLY 🎉 ║${RESET}"
echo -e "${MAGENTA}${BOLD}╚════════════════════════════════════════════════════════════╝${RESET}"
# Subscribe to Orbit of Ops: https://www.youtube.com/@orbitofops/videos