#!/bin/bash # Deployment script that ensures APIs are enabled before deploying Cloud Functions set -e echo "🚀 Starting Terraform deployment with API enablement..." # Get project ID from terraform.tfvars or environment PROJECT_ID=${GOOGLE_CLOUD_PROJECT:-$(grep 'project_id' terraform.tfvars | cut -d'"' -f2)} if [ -z "$PROJECT_ID" ]; then echo "❌ Error: PROJECT_ID not found. Set GOOGLE_CLOUD_PROJECT or ensure terraform.tfvars has project_id" exit 1 fi echo "📋 Project ID: $PROJECT_ID" # Step 1: Enable APIs first echo "🔧 Step 1: Enabling required APIs..." terraform apply -target=google_project_service.services -auto-approve # Step 2: Wait for APIs to propagate echo "⏳ Step 2: Waiting for APIs to propagate (30 seconds)..." sleep 30 # Step 3: Verify critical APIs are enabled echo "✅ Step 3: Verifying API enablement..." REQUIRED_APIS=( "cloudfunctions.googleapis.com" "eventarc.googleapis.com" "pubsub.googleapis.com" "cloudbuild.googleapis.com" ) for api in "${REQUIRED_APIS[@]}"; do echo " Checking $api..." if gcloud services list --enabled --project="$PROJECT_ID" --filter="name:$api" --format="value(name)" | grep -q "$api"; then echo " ✅ $api is enabled" else echo " ❌ $api is not enabled yet, waiting longer..." sleep 15 fi done # Step 4: Deploy infrastructure echo "🏗️ Step 4: Deploying infrastructure..." terraform apply -target=google_storage_bucket.app_bucket -target=google_firestore_database.database -target=google_compute_instance.vector_db_vm -auto-approve # Step 5: Deploy Pub/Sub resources echo "📨 Step 5: Deploying Pub/Sub resources..." terraform apply -target=google_pubsub_topic.image_processing -target=google_pubsub_topic.image_processing_dlq -auto-approve # Step 6: Deploy Cloud Function echo "⚡ Step 6: Deploying Cloud Function..." terraform apply -target=google_cloudfunctions2_function.image_processor -auto-approve # Step 7: Deploy remaining resources echo "🎯 Step 7: Deploying remaining resources..." terraform apply -auto-approve echo "🎉 Deployment completed successfully!" echo "" echo "📊 Deployment Summary:" terraform output deployment_summary 2>/dev/null || echo " Run 'terraform output' to see all outputs"