Implement Event-Driven Messaging and Automation Workflows
Solution for Implement Event-Driven Messaging and Automation Workflows. 1 lab: ARC113. Fast copy-paste commands for Google Cloud.
ARC113 — Implement Event-Driven Messaging and Automation Workflows: Challenge Lab
Estimated time: 1 hour 15 minutes
# 🚀 Implement Event-Driven Messaging and Automation Workflows: Challenge Lab > ⚠️ **Disclaimer:** This is an independent, community-made walkthrough created for educational purposes, hands-on practice, and Google Cloud certification preparation. This guide is designed to help learners understand event-driven messaging, automation workflows, and practical Google Cloud services such as Pub/Sub, Cloud Scheduler, schemas, snapshots, and Cloud Functions. Always attempt the lab yourself first and fo
# ==============================================================================
# ORBIT OF OPS - ARC113 (FORM 1: SCHEDULER)
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
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: ARC113 (FORM 1) INITIATED <<<${RESET}\n"
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 project-info describe --format="value(commonInstanceMetadata.items[google-compute-default-zone])" 2>/dev/null | tail -n 1)
export REGION=${ZONE%-*}
echo -e "${BLUE}${BOLD}[Orbit of Ops] Enabling Cloud Scheduler API...${RESET}"
gcloud services enable cloudscheduler.googleapis.com --quiet
echo -e "${CYAN}${BOLD}[Orbit of Ops] Provisioning Pub/Sub Topics & Subscriptions...${RESET}"
gcloud pubsub topics create cloud-pubsub-topic --quiet || true
gcloud pubsub subscriptions create cloud-pubsub-subscription --topic=cloud-pubsub-topic --quiet || true
echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Creating Scheduled Cron Job...${RESET}"
gcloud scheduler jobs create pubsub cron-scheduler-job \
--schedule="* * * * *" \
--topic=cloud-pubsub-topic \
--message-body="Hello World!" \
--location=$REGION \
--quiet || true
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Force-running job and pulling messages...${RESET}"
gcloud scheduler jobs run cron-scheduler-job --location=$REGION --quiet || true
sleep 5
gcloud pubsub subscriptions pull cloud-pubsub-subscription --limit 5 --quiet || true
echo -e "\n${GREEN}${BOLD}🎉 FORM 1 COMPLETE! You can now click 'Check my progress' on all tasks.${RESET}"# ==============================================================================
# ORBIT OF OPS - ARC113 (FORM 2: SCHEMA & FUNCTION)
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
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: ARC113 (FORM 2) INITIATED <<<${RESET}\n"
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 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)
export REGION=${ZONE%-*}
echo -e "${BLUE}${BOLD}[Orbit of Ops] Enabling APIs and Instantiating Agents...${RESET}"
gcloud services enable pubsub.googleapis.com eventarc.googleapis.com run.googleapis.com cloudfunctions.googleapis.com build.googleapis.com --quiet
gcloud beta services identity create --service=pubsub.googleapis.com --project=$PROJECT_ID 2>/dev/null || true
gcloud beta services identity create --service=eventarc.googleapis.com --project=$PROJECT_ID 2>/dev/null || true
sleep 15
export COMPUTE_SA="${PROJECT_NUMBER}[email protected]"
export PUBSUB_SA="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
export EVENTARC_SA="service-${PROJECT_NUMBER}@gcp-sa-eventarc.iam.gserviceaccount.com"
RAW_SA=$(gsutil kms serviceaccount -p $PROJECT_NUMBER)
CLEAN_SA=$(echo "$RAW_SA" | sed 's/serviceAccount://g' | tr -d '[:space:]')
echo -e "${CYAN}${BOLD}[Orbit of Ops] Applying Strict IAM Roles for Eventarc...${RESET}"
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:${CLEAN_SA}" --role="roles/pubsub.publisher" --quiet >/dev/null
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:${COMPUTE_SA}" --role="roles/eventarc.eventReceiver" --quiet >/dev/null
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:${COMPUTE_SA}" --role="roles/artifactregistry.reader" --quiet >/dev/null
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:${PUBSUB_SA}" --role="roles/iam.serviceAccountTokenCreator" --quiet >/dev/null
gcloud projects add-iam-policy-binding $PROJECT_ID --member="serviceAccount:${EVENTARC_SA}" --role="roles/eventarc.serviceAgent" --quiet >/dev/null
echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Creating Schemas and Topics...${RESET}"
gcloud pubsub schemas create temperature-schema --type=avro --definition='{"type":"record","name":"Avro","fields":[{"name":"city","type":"string"},{"name":"temperature","type":"double"},{"name":"pressure","type":"int"},{"name":"time_position","type":"string"}]}' --quiet || true
gcloud pubsub topics create temp-topic --message-encoding=JSON --schema=temperature-schema --quiet || true
gcloud pubsub topics create gcf-topic --quiet || true
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Generating Cloud Function Code...${RESET}"
mkdir -p ~/orbit-pubsub && cd ~/orbit-pubsub
cat > index.js <<'EOF'
const functions = require('@google-cloud/functions-framework');
functions.cloudEvent('helloPubSub', cloudEvent => {
const base64name = cloudEvent.data.message.data;
const name = base64name ? Buffer.from(base64name, 'base64').toString() : 'World';
console.log(`Hello, ${name}!`);
});
EOF
cat > package.json <<'EOF'
{
"name": "gcf_hello_world",
"version": "1.0.0",
"main": "index.js",
"dependencies": {
"@google-cloud/functions-framework": "^3.0.0"
}
}
EOF
echo -e "${BLUE}${BOLD}[Orbit of Ops] Deploying Cloud Function (Takes ~2 mins)...${RESET}"
export DEPLOY_CMD="gcloud functions deploy gcf-pubsub --gen2 --runtime=nodejs20 --region=$REGION --source=. --entry-point=helloPubSub --trigger-topic=gcf-topic --max-instances=1 --quiet"
eval $DEPLOY_CMD || { echo -e "${RED}Retrying deployment to allow IAM propagation...${RESET}"; sleep 45; eval $DEPLOY_CMD; }
echo -e "\n${GREEN}${BOLD}🎉 FORM 2 COMPLETE! You can now click 'Check my progress' on all tasks.${RESET}"# ==============================================================================
# ORBIT OF OPS - ARC113 (FORM 3: SNAPSHOTS)
# ==============================================================================
GREEN='\e[1;32m'
CYAN='\e[1;36m'
YELLOW='\e[1;33m'
BLUE='\e[1;34m'
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: ARC113 (FORM 3) INITIATED <<<${RESET}\n"
echo -e "${BLUE}${BOLD}[Orbit of Ops] Provisioning Pub/Sub Topics & Subscriptions...${RESET}"
gcloud pubsub topics create gcloud-pubsub-topic --quiet || true
gcloud pubsub subscriptions create pubsub-subscription-message --topic=gcloud-pubsub-topic --quiet || true
echo -e "${CYAN}${BOLD}[Orbit of Ops] Publishing Test Message...${RESET}"
gcloud pubsub topics publish gcloud-pubsub-topic --message="Hello World" --quiet
echo -e "${YELLOW}${BOLD}[Orbit of Ops] Waiting for delivery...${RESET}"
sleep 10
gcloud pubsub subscriptions pull pubsub-subscription-message --limit 5 --quiet
echo -e "${MAGENTA}${BOLD}[Orbit of Ops] Creating Snapshot...${RESET}"
gcloud pubsub snapshots create pubsub-snapshot --subscription=pubsub-subscription-message --quiet || true
echo -e "\n${GREEN}${BOLD}🎉 FORM 3 COMPLETE! You can now click 'Check my progress' on all tasks.${RESET}"