Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cleanup script for Google Cloud Functions with Redis and VPC Connector example
#!/usr/bin/env bash
# Reference: https://gist.github.com/mikesparr/8985378640242d8e453cc2212509814d
# 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="10.8.0.0/28"
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-$PROJECT_NUM@gcf-admin-robot.iam.gserviceaccount.com \
--role=roles/viewer
gcloud projects remove-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:service-$PROJECT_NUM@gcf-admin-robot.iam.gserviceaccount.com \
--role=roles/compute.networkUser
gcloud projects remove-iam-policy-binding $PROJECT_ID \
--member=serviceAccount:service-$PROJECT_NUM@gcf-admin-robot.iam.gserviceaccount.com \
--role=$STORAGE_ROLE_NAME
# 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