Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Cleanup script for Google Cloud Functions with Redis and VPC Connector example
#!/usr/bin/env bash
# Reference:
# set these to your specific environment
export PROJECT_ID=db-cluster-tests
export REDIS_INSTANCE=myinstance
export REDIS_VERSION="redis_4_0"
export GCP_REGION=us-central1
export GCP_NETWORK=default
export VPC_CONNECTOR=redis-vpc-conn
export VPC_RANGE=""
export FUNCTION_NAME=visit_count
export STORAGE_ROLE=simpleStorageRole
# fetch the project number to use in IAM bindings
export PROJECT_NUM=$(gcloud projects describe $PROJECT_ID --format="value(projectNumber)")
echo "Project number: $PROJECT_NUM"
# delete function
gcloud beta functions delete $FUNCTION_NAME \
--region $GCP_REGION
# remove IAM bindings (replace project number with one you fetched)
gcloud projects remove-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:service-$ \
gcloud projects remove-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:service-$ \
gcloud projects remove-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:service-$ \
# delete custom role for storage permissions
gcloud iam roles delete $STORAGE_ROLE \
--project $PROJECT_ID
# delete VPC connector (use the network name [not full path] and region from above)
gcloud compute networks vpc-access connectors delete $VPC_CONNECTOR \
--region $GCP_REGION
# delete redis cluster
gcloud redis instances delete $REDIS_INSTANCE \
--region $GCP_REGION
# remember to delete downloaded git repository too!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment