Guide to using a downloaded Google Cloud service account JSON key locally with gcloud CLI to authenticate, verify permissions, adjust roles, and clean up credentials.
Welcome back. In a previous lesson we created:
a custom IAM role that allows listing Cloud Storage buckets and objects,
a service account, and
a service account key (JSON).
This article shows how to use that downloaded service account key locally so the gcloud CLI acts as the service account while you develop or test. Follow the steps below to authenticate, verify permissions, adjust role permissions if needed, and clean up local credentials.
Service account keys are sensitive. Store them only on trusted machines, delete them when no longer needed, and rotate keys if they are exposed.
What the service account key looks like
Below is an example of the JSON key file you download from the Cloud Console. Your file will include the same fields; private_key is shortened here.
Prerequisite: gcloud CLI
Install and configure the gcloud CLI first: https://cloud.google.com/sdk/docs/installOn macOS, if you see Python-related errors (for example ModuleNotFoundError: No module named 'imp'), point the Cloud SDK to a compatible Python binary. Example:
Step 1 — Activate the service account locally
With the JSON key file in your working directory (here named kodekloud-gcp-training-79980d36ed10.json), run:
Activated service account credentials for: [kodekloud-test-sa@kodekloud-gcp-training.iam.gserviceaccount.com]
Step 2 — Set the active project
Set the project for subsequent gcloud commands. Permissions are evaluated against the project and resources in this context:
gcloud config set project kodekloud-gcp-training
If you do not have access you might be prompted to confirm:
WARNING: You do not appear to have access to project [kodekloud-gcp-training] or it does not exist.Are you sure you wish to set property [core/project] to kodekloud-gcp-training?Do you want to continue (Y/n)? YUpdated property [core/project].
Step 3 — Verify the service account permissions
Test an operation the custom role allows (list Cloud Storage buckets):
gcloud storage ls
Example output (lists buckets visible to the service account):
Now test an operation that the custom role does not include (list Compute Engine instances):
gcloud compute instances list
If the service account lacks compute.instances.list, you’ll see an error like:
ERROR: (gcloud.compute.instances.list) Some requests did not succeed: - Required 'compute.instances.list' permission for 'projects/kodekloud-gcp-training'
Step 4 — Add missing permission to the custom role (if needed)
If you require additional permissions, edit the custom role in the Cloud Console:
Go to IAM & Admin → Roles.
Open your custom role (e.g., kodekloud.storage.viewer).
Click Edit role → Add permissions.
Search for compute.instances.list, add it, and Update.
After updating the role, rerun:
gcloud compute instances list
If the service account now has compute.instances.list but there are no instances in the project, the command returns:
Listed 0 items.
This confirms that local authentication and role permission changes are effective.Step 5 — Cleaning up and revoking local credentials
When finished, list active accounts:
gcloud auth list
Example output:
Credentialed AccountsACTIVE ACCOUNT* kodekloud-test-sa@kodekloud-gcp-training.iam.gserviceaccount.comTo set the active account, run: $ gcloud config set account `ACCOUNT`
Revoke the service account credentials from your local gcloud auth store:
Warning: revoking a service account token only removes local credentials. Service account tokens cannot be force-revoked like user OAuth tokens; they will expire automatically. To immediately prevent key use, delete the specific service account key or disable/delete the parent service account.
Revoking local credentials does not expire the underlying service account key. If a key is compromised, delete the key (or disable/delete the service account) immediately.
To delete the key you created:
gcloud iam service-accounts keys delete KEY_ID \ --iam-account=kodekloud-test-sa@kodekloud-gcp-training.iam.gserviceaccount.com
Quick reference — common gcloud commands used here