You are viewing a free preview of this lesson.
Subscribe to unlock all 10 lessons in this course and every other course on LearningBro.
Service accounts are special identities designed for applications, VMs, containers, and automated processes — not for humans. They are one of the most important and most frequently misconfigured aspects of GCP IAM.
A service account is:
Format: <name>@<project-id>.iam.gserviceaccount.com
Example: api-backend@my-project.iam.gserviceaccount.com
| Type | Created By | Example | Can Delete? |
|---|---|---|---|
| User-managed | You | my-sa@proj.iam.gserviceaccount.com | Yes |
| Default | GCP (when APIs are enabled) | PROJECT_NUMBER-compute@developer.gserviceaccount.com | Yes (but not recommended) |
| Google-managed | Google (internal) | *@cloudservices.gserviceaccount.com | No |
When you enable the Compute Engine API, GCP creates a default compute service account:
<PROJECT_NUMBER>-compute@developer.gserviceaccount.com
Important: The default compute service account has the roles/editor basic role. This is far too broad for production. Best practice is to:
# Create a service account
gcloud iam service-accounts create api-backend \
--display-name="API Backend Service Account" \
--description="Used by the API backend application"
# List service accounts in a project
gcloud iam service-accounts list --project=my-project
# Describe a service account
gcloud iam service-accounts describe \
api-backend@my-project.iam.gserviceaccount.com
# Update display name
gcloud iam service-accounts update \
api-backend@my-project.iam.gserviceaccount.com \
--display-name="API Backend SA (Production)"
# Disable a service account (temporarily block access)
gcloud iam service-accounts disable \
api-backend@my-project.iam.gserviceaccount.com
# Enable a disabled service account
gcloud iam service-accounts enable \
api-backend@my-project.iam.gserviceaccount.com
# Delete a service account
gcloud iam service-accounts delete \
api-backend@my-project.iam.gserviceaccount.com
A service account acts as a member — you grant it roles just like a user:
# Grant Cloud Storage read access
gcloud projects add-iam-policy-binding my-project \
--member="serviceAccount:api-backend@my-project.iam.gserviceaccount.com" \
--role="roles/storage.objectViewer"
# Grant Cloud SQL client access
gcloud projects add-iam-policy-binding my-project \
--member="serviceAccount:api-backend@my-project.iam.gserviceaccount.com" \
--role="roles/cloudsql.client"
Attach the service account directly to a GCP resource. The resource receives credentials automatically via the metadata server — no key file needed.
# Attach to a Compute Engine VM
gcloud compute instances create my-vm \
--zone=europe-west2-a \
--service-account=api-backend@my-project.iam.gserviceaccount.com \
--scopes=cloud-platform
# Attach to a Cloud Run service
gcloud run deploy my-service \
--image=gcr.io/my-project/my-image \
--service-account=api-backend@my-project.iam.gserviceaccount.com
# Attach to a Cloud Function
gcloud functions deploy my-function \
--runtime=nodejs20 \
--service-account=api-backend@my-project.iam.gserviceaccount.com
Workload Identity maps Kubernetes service accounts to GCP service accounts without key files:
Subscribe to continue reading
Get full access to this lesson and all 10 lessons in this course.