GCP Cheatsheet

Thanks, I hate it.

CLI

Login to account

gcloud auth login

List projects

gcloud projects list

Set project

PROJECT_NAME=yourproject
gcloud config set project $PROJECT_NAME

List credentialed accounts

gcloud auth list

Change credentialed account

ACCOUNT_NAME=account_to_use
gcloud config set account $ACCOUNT_NAME

Set project_id and number

List the projects and simply take the PROJECT_ID from the first column and assign it to an env var:

PROJECT_ID=yourprojectid

Next, set the project number:

PROJECT_NUMBER=$(gcloud projects list \
  --format="value(projectNumber)" \
  --filter="projectId=${PROJECT_ID}")

Resources:
https://stackoverflow.com/questions/64236468/cloud-build-fails-to-deploy-to-google-app-engine-you-do-not-have-permission-to
https://www.bram.us/2020/02/13/google-cloud-build-google-cloud-run-fixing-error-gcloud-run-deploy-permission_denied-the-caller-does-not-have-permission

List instances in a project

gcloud compute instances list

SSH into instance

instance_name=yourinstance
gcloud compute ssh $instance_name

Alternatively, if you want to use plain ssh, simply add the pubkey for your user to the ~/.ssh/authorized_keys file.

List project permissions

gcloud projects get-iam-policy $PROJECT_ID

Resource: https://stackoverflow.com/questions/47006116/how-do-i-list-and-view-users-permissions-with-gcloud

List permissions for a particular account

gcloud projects get-iam-policy $PROJECT_ID --flatten="bindings[].members" --format='table(bindings.role)' --filter="bindings.members:<the account name>"

Resource: https://stackoverflow.com/questions/47006062/how-do-i-list-the-roles-associated-with-a-gcp-service-account

View App Engine logs

View logs:

gcloud app logs read

Stream logs:

gcloud app logs tail

Stream logs for a particular version:

gcloud app logs tail --version=<the version>

Resource: https://stackoverflow.com/questions/49090343/where-i-can-see-the-logs-in-google-app-engine

Logs Explorer

View error logs for a particular version of a gae app:

resource.type="gae_app"
resource.labels.version_id="<version name>"
resource.labels.project_id="<project id>"
resource.labels.zone="<the region you're in, for example: us-west-2-1>"
severity=ERROR