API Gateway: Qwik Start
Solution for API Gateway: Qwik Start. 1 lab: GSP872. Fast copy-paste commands for Google Cloud.
GSP872 — API Gateway: Qwik Start
Estimated time: 35 minutes
# 🚀 API Gateway: Qwik Start > ⚠️ **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 intended for educational purposes and 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 Google Cloud Skills Boost. Always follow t
# ==============================================================================
# Color Variables & Branding
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
MAGENTA='\e[1;35m'
RED='\e[1;31m'
RESET='\e[0m'
BOLD='\e[1m'
clear
echo -e "${CYAN}${BOLD}"
cat << "EOF"
____ _ _ _ __ ___
/ __ \ | | (_) | / _| / _ \
| | | |_ __| |__ _| |_ ___ | |_ | | | |_ __ ___
| | | | '__| '_ \| | __| / _ \ | _| | | | | '_ \/ __|
| |__| | | | |_) | | |_ | (_) || | | |_| | |_) \__ \
\____/|_| |_.__/|_|\__| \___/ |_| \___/| .__/|___/
| |
|_|
EOF
echo -e "${RESET}"
echo -e "${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP872 PART 1 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Auto-fetching Project, Zone, 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 PROJECT_NUMBER=$(gcloud projects describe $PROJECT_ID --format='value(projectNumber)' 2>/dev/null)
export ZONE=$(gcloud compute project-info describe --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)
if [[ -z "$ZONE" ]]; then
echo -e "${RED}${BOLD}⚠️ Could not auto-detect the default zone via gcloud metadata.${RESET}"
read -p "Please enter the lab Zone (e.g., us-east1-c): " 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 "✅ Region: ${GREEN}$REGION${RESET}\n"
echo -e "${CYAN}${BOLD}[Orbit of Ops] Enabling Required APIs...${RESET}"
gcloud services enable apigateway.googleapis.com run.googleapis.com cloudfunctions.googleapis.com cloudbuild.googleapis.com apikeys.googleapis.com
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Deploying Cloud Function (helloGET)...${RESET}"
git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples.git 2>/dev/null || true
cd nodejs-docs-samples/functions/helloworld/helloworldGet
DEPLOY_CMD="gcloud functions deploy helloGET --runtime nodejs20 --region $REGION --trigger-http --allow-unauthenticated --max-instances=1"
eval $DEPLOY_CMD || { echo -e "${RED}Initial deploy failed. Waiting 45s for IAM sync...${RESET}"; sleep 45; eval $DEPLOY_CMD; }
echo -e "\n${GREEN}${BOLD}✅ Part 1 Complete. Check Task 1 progress in the lab window!${RESET}"echo -e "\n${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP872 PART 2 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 3: Building OpenAPI Spec & Creating Gateway...${RESET}"
cd ~
export API_ID="hello-world-api"
# Writing the OpenAPI Spec dynamically
cat <<EOF > openapi2-functions.yaml
swagger: '2.0'
info:
title: ${API_ID} description
description: Sample API on API Gateway with a Google Cloud Functions backend
version: 1.0.0
schemes:
- https
produces:
- application/json
paths:
/hello:
get:
summary: Greet a user
operationId: hello
x-google-backend:
address: https://${REGION}-${PROJECT_ID}.cloudfunctions.net/helloGET
responses:
'200':
description: A successful response
schema:
type: string
EOF
echo -e "${CYAN}${BOLD}[Orbit of Ops] Provisioning API Gateway Resources (This takes a few minutes)...${RESET}"
gcloud api-gateway apis create $API_ID --project=$PROJECT_ID || true
gcloud api-gateway api-configs create hello-world-config \
--project=$PROJECT_ID \
--api=$API_ID \
--openapi-spec=openapi2-functions.yaml \
--backend-auth-service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com || true
gcloud api-gateway gateways create hello-gateway \
--location=$REGION \
--project=$PROJECT_ID \
--api=$API_ID \
--api-config=hello-world-config || true
echo -e "\n${GREEN}${BOLD}✅ Part 2 Complete. Check Task 3 progress in the lab window!${RESET}"echo -e "\n${MAGENTA}${BOLD}>>> ORBIT OF OPS: GSP872 PART 3 INITIALIZED <<<${RESET}\n"
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 4: Generating API Key...${RESET}"
gcloud services api-keys create --display-name="qwiklabs-key" || true
export KEY_NAME=$(gcloud services api-keys list --format="value(name)" --filter "displayName=qwiklabs-key" | head -n 1)
export API_KEY=$(gcloud services api-keys get-key-string $KEY_NAME --format="value(keyString)")
echo -e "${CYAN}${BOLD}✅ Your API Key is: $API_KEY${RESET}"
MANAGED_SERVICE=$(gcloud api-gateway apis list --format json | jq -r .[0].managedService | cut -d'/' -f6)
gcloud services enable $MANAGED_SERVICE || true
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 5: Updating OpenAPI Spec for Security...${RESET}"
cd ~
cat <<EOF > openapi2-functions2.yaml
swagger: '2.0'
info:
title: ${API_ID} description
description: Sample API on API Gateway with a Google Cloud Functions backend
version: 1.0.0
schemes:
- https
produces:
- application/json
paths:
/hello:
get:
summary: Greet a user
operationId: hello
x-google-backend:
address: https://${REGION}-${PROJECT_ID}.cloudfunctions.net/helloGET
security:
- api_key: []
responses:
'200':
description: A successful response
schema:
type: string
securityDefinitions:
api_key:
type: "apiKey"
name: "key"
in: "query"
EOF
echo -e "${CYAN}${BOLD}[Orbit of Ops] Updating Gateway Configuration...${RESET}"
gcloud api-gateway api-configs create hello-config \
--project=$PROJECT_ID \
--display-name="Hello Config" \
--api=$API_ID \
--openapi-spec=openapi2-functions2.yaml \
--backend-auth-service-account=$PROJECT_NUMBER-compute@developer.gserviceaccount.com || true
gcloud api-gateway gateways update hello-gateway \
--location=$REGION \
--project=$PROJECT_ID \
--api=$API_ID \
--api-config=hello-config || true
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Task 6: Testing Secure API Call...${RESET}"
export GATEWAY_URL=$(gcloud api-gateway gateways describe hello-gateway --location $REGION --format json | jq -r .defaultHostname)
echo -e "\n${CYAN}Testing WITHOUT Key (Should fail):${RESET}"
curl -sL https://$GATEWAY_URL/hello || true
echo -e "\n\n${GREEN}Testing WITH Key (Should return Hello World!):${RESET}"
curl -sL -w "\n" https://$GATEWAY_URL/hello?key=$API_KEY || true
echo -e "\n${GREEN}${BOLD}🎉 All blocks executed successfully! Check your final progress in the lab window!${RESET}"