Overview and What You Will Learn
In this lab, you will create a Service Account, attach it directly to a Compute Engine VM so the VM authenticates automatically with no key file at all, and separately use impersonation to generate a short-lived token from your own already-authenticated session - two patterns that both avoid ever downloading a permanent credential.
Why This Matters in Production
A developer downloads a Service Account's JSON key file to test something locally, commits it to a repository by accident, and that key - which never expires on its own - remains a valid, fully working credential until someone notices and manually revokes it. Both patterns in this lab exist specifically to remove the need for that key file to ever be created in the first place.
Core Principles
A Service Account is an identity used by applications and infrastructure, not humans, and GCP provides two genuinely safer alternatives to downloading its permanent key file.
+------------------------------------------+| Attaching a Service Account to a VM || The VM authenticates as that identity || automatically - no key file anywhere |+------------------------------------------+| Service Account Impersonation || An already-authenticated user or Service || Account temporarily acts as another one, || using a short-lived token, not a key file |+------------------------------------------+Both patterns eliminate the actual object that causes most Service Account security incidents - a long-lived, downloadable, leakable key file.
Detailed Step-by-Step Practical Lab
- Create a Project and enable the Compute Engine API:
gcloud projects create gcp-sa-lab-2026 --name="Service Account Lab"gcloud config set project gcp-sa-lab-2026gcloud services enable compute.googleapis.com- Create a Service Account with no key file generated at all:
gcloud iam service-accounts create app-backend-sa \ --display-name="Application Backend Service Account"- Grant that Service Account a specific, scoped role:
gcloud projects add-iam-policy-binding gcp-sa-lab-2026 \ --member="serviceAccount:app-backend-sa@gcp-sa-lab-2026.iam.gserviceaccount.com" \ --role="roles/storage.objectViewer"- Create a VM, attaching this Service Account directly - the VM will authenticate as this identity automatically:
gcloud compute instances create vm-app-backend \ --zone=asia-south1-a \ --machine-type=e2-medium \ --image-family=debian-12 \ --image-project=debian-cloud \ --service-account=app-backend-sa@gcp-sa-lab-2026.iam.gserviceaccount.com \ --scopes=cloud-platform- SSH into the VM and confirm it can call GCP APIs using its attached Service Account identity, with no credential file present anywhere on the instance:
gcloud compute ssh vm-app-backend --zone=asia-south1-a gcloud auth list# Confirms the active identity is the Service Account, not a downloaded key- Now demonstrate impersonation - from your own already-authenticated local session, generate a short-lived token acting as this Service Account, without ever downloading its key:
gcloud auth print-access-token \ --impersonate-service-account=app-backend-sa@gcp-sa-lab-2026.iam.gserviceaccount.comNoteFor impersonation to work, your own user identity must first be granted the
roles/iam.serviceAccountTokenCreatorrole on this specific Service Account - impersonation is itself an IAM-controlled action, not an open capability available to anyone who knows a Service Account's name.
- Grant yourself that impersonation permission, then retry step 6 to see it succeed:
gcloud iam service-accounts add-iam-policy-binding \ app-backend-sa@gcp-sa-lab-2026.iam.gserviceaccount.com \ --member="user:your-email@example.com" \ --role="roles/iam.serviceAccountTokenCreator"- Clean up:
gcloud compute instances delete vm-app-backend --zone=asia-south1-a --quietgcloud projects delete gcp-sa-lab-2026 --quietProduction Best Practices & Common Pitfalls
SecurityAvoid creating and downloading Service Account key files whenever a safer alternative exists. A downloaded key file is a long-lived credential that never expires on its own and can leak exactly like an AWS access key or an Azure service principal secret - attaching the Service Account to the VM directly, or using impersonation, removes that risk entirely.
TipIf a key file genuinely must exist for a workload running outside GCP entirely (like a script on a developer's laptop or a server in another cloud), treat it with the same care as any other long-lived secret - store it in Secret Manager rather than a local file, and rotate it on a defined schedule.
--scopes=cloud-platformon the VM creation command controls what the VM's Service Account is technically permitted to attempt - the actual IAM role bindings still determine what it can successfully do. Both need to align for an API call to actually succeed.- Service Account impersonation is itself governed by IAM, specifically the
roles/iam.serviceAccountTokenCreatorrole - granting this role is equivalent in sensitivity to granting whatever permissions the impersonated Service Account itself has.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud iam service-accounts create |
Create a new Service Account |
gcloud compute instances create --service-account= |
Attach a Service Account to a VM |
gcloud auth print-access-token --impersonate-service-account= |
Generate a short-lived token via impersonation |
gcloud iam service-accounts add-iam-policy-binding |
Grant permission to impersonate a Service Account |