Skip to content

Instantly share code, notes, and snippets.

@hack-r
Last active July 17, 2023 23:41
Show Gist options
  • Save hack-r/19bb86d3f714dbe287a32c4cf02222be to your computer and use it in GitHub Desktop.
Save hack-r/19bb86d3f714dbe287a32c4cf02222be to your computer and use it in GitHub Desktop.
#!/bin/bash
# Set the project and regions variables
PROJECT_ID="myproject"
REGIONS=("us-central1" "us-east1")
# Get a list of all Cloud Functions in the project
ALL_FUNCTIONS=$(gcloud functions list --project $PROJECT_ID --format='value(name, region)')
# Loop through each region
for REGION in "${REGIONS[@]}"; do
# Filter the functions based on the current region
FUNCTIONS=$(echo "$ALL_FUNCTIONS" | awk -v region="$REGION" '$2 == region { print $1 }')
# Create an API Gateway for each Cloud Function in the current region
for FUNCTION_NAME in $FUNCTIONS; do
# Create the API config
echo "swagger: '2.0'
info:
title: Cloud Function API
version: 1.0.0
paths:
/$FUNCTION_NAME:
post:
summary: Invoke $FUNCTION_NAME Cloud Function
operationId: invoke$FUNCTION_NAME
x-google-backend:
address: https://$REGION-$PROJECT_ID.cloudfunctions.net/$FUNCTION_NAME
responses:
'200':
description: Successful response
" > $FUNCTION_NAME.yaml
gcloud api-gateway api-configs create $FUNCTION_NAME-config \
--api=cloudfunctions.googleapis.com \
--openapi-spec=$FUNCTION_NAME.yaml \
--project=$PROJECT_ID
# Create the API Gateway
gcloud api-gateway gateways create $FUNCTION_NAME-gateway \
--api=cloudfunctions.googleapis.com \
--api-config=$FUNCTION_NAME-config \
--location=$REGION \
--project=$PROJECT_ID
done
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment