Skip to content

Instantly share code, notes, and snippets.

@cosimo
Created January 12, 2022 16:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cosimo/0b416b5ad41cd9b58ac2eeac8f3fa332 to your computer and use it in GitHub Desktop.
Save cosimo/0b416b5ad41cd9b58ac2eeac8f3fa332 to your computer and use it in GitHub Desktop.
Random gcloud command line snippets
#!/bin/bash
# =============================================================================
#
# Very random gcloud related snippets
#
# =============================================================================
set -e
set -o nounset
PROJECT_ID="$1"
# -----------------------------------------------------------------------------
# Create a role of which you have a policy file dumped with:
# `gcloud iam roles describe ...`.
#
# Before running this you need to enable the IAM API
# https://console.cloud.google.com/apis/enableflow?project=${PROJECT_ID}
#
gcloud iam roles create SomeRole --project $PROJECT_ID --file ./SomeRole.yaml
# -----------------------------------------------------------------------------
# Create a service account
#
gcloud iam service-accounts create --project="$PROJECT_ID" sa-name \
--display-name "Some Service Account" \
--description="Used by Blah to setup whatever infrastructure"
# -----------------------------------------------------------------------------
# Create a new key for a service account
#
gcloud iam service-accounts keys create --project="$PROJECT_ID" \
--iam-account=sa-name@${PROJECT_ID}.iam.gserviceaccount.com \
output-file.json
# -----------------------------------------------------------------------------
# List all keys used by a service account
#
gcloud iam service-accounts keys list --project="$PROJECT_ID" \
--iam-account="sa-name@${PROJECT_ID}.iam.gserviceaccount.com"
# -----------------------------------------------------------------------------
# List all APIs that it's possible to enable for a project
#
gcloud services list --available --project="$PROJECT_ID" | tee gcloud_apis.txt
# -----------------------------------------------------------------------------
# Enable a set of APIs in a project
#
# You can look up or search the API names in `gcloud_apis.txt`
#
gcloud services enable --project "$PROJECT_ID" \
compute.googleapis.com \
iam.googleapis.com \
secretmanager.googleapis.com \
pubsub.googleapis.com
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment