Most serious breaches in Google Cloud do not start with a zero-day exploit. They start with identity. Misconfigured google cloud service accounts, overly broad IAM roles, and long-lived keys left in a repository are the top security risk in many GCP projects. If an attacker obtains a key for an over-privileged account, they effectively own the project. This guide explains how IAM really works, how to apply least privilege in practice, and how to audit what you have already granted.
Good identity design is not complicated, but it does require discipline. Every human and every workload should have its own identity, only the permissions it needs, and credentials that expire quickly. The steps below follow that principle from the first policy review to impersonation and custom roles.
Prerequisites – Google Cloud IAM Roles and Service
- A Google Cloud project with billing enabled and Owner or IAM Admin access for the initial setup.
- The gcloud CLI installed and authenticated, or access to Cloud Shell.
- A test Compute Engine VM where you can practise attaching an identity.
- Basic familiarity with Linux and SSH. If you need a refresher, see how to generate SSH keys on Linux and how to SSH into a Linux server.
Google Cloud Service Accounts and IAM Concepts – Google Cloud IAM Roles and Service
Three core concepts explain almost every IAM decision. A member is who is asking: a Google account, a group, a domain, or a service account. A role is a collection of permissions such as storage.objects.get or compute.instances.start. A policy binds members to roles on a resource. Permissions are never granted directly to a member; they always come through a role.
Roles come in three types. Basic roles — Viewer, Editor, and Owner — are legacy, very broad, and should be avoided in production. Predefined roles are maintained by Google for a specific service and job function, for example roles/storage.objectViewer. Custom roles let you build an exact permission set when predefined roles are still too broad, although they require ongoing maintenance as services change.
Resources form a hierarchy: organisation, folder, project, then individual resources such as buckets and instances. A binding at a higher level is inherited downwards. That inheritance is convenient and dangerous at the same time: granting Editor at organisation level gives Editor everywhere below it.
What google cloud service accounts actually are
Unlike a user account, a service account is an identity for applications and virtual machines. It has an email address, it can be granted roles, and other identities can be allowed to impersonate it. User-managed service accounts are created by you for a clear purpose. Default service accounts, in particular the default Compute Engine service account, are created automatically and historically received the Editor role on the project. A workload running with that identity can therefore read, modify, and delete far more than it needs. Replace it on every production VM.

Step 1: View the Current IAM Policy
Before changing anything, record what exists. Export the project policy so you can review bindings and keep a copy for rollback.
gcloud projects get-iam-policy PROJECT_ID --format=json > iam-policy.json
gcloud projects get-iam-policy PROJECT_ID
Look specifically for basic roles, for allUsers or allAuthenticatedUsers, and for default accounts holding Editor or Owner. Those findings are your priority list. Official terminology and policy structure are documented in the Google Cloud IAM documentation.
Step 2: Grant a Predefined Role at Project Level
Grant roles to groups rather than individuals where possible, and grant them at the lowest level that works. For example, give a support group read-only storage access on one project:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="group:[email protected]" \
--role="roles/storage.objectViewer"
Verify the binding appears in a fresh get-iam-policy output before you continue. Small, verified changes are safer than a large policy rewrite.
Creating Dedicated Google Cloud Service Accounts Step by Step
Create one account per workload, with a name that states its purpose. A billing exporter, a backup job, and a web application should never share an identity.
gcloud iam service-accounts create app-backend \
--display-name="App backend service account" \
--description="Identity for the backend VM only"
Now grant that account only the role it needs. If the application only writes logs and reads one bucket, it does not need Editor:
gcloud projects add-iam-policy-binding PROJECT_ID \
--member="serviceAccount:app-backend@PROJECT_ID.iam.gserviceaccount.com" \
--role="roles/logging.logWriter"
Attach the account to a VM instead of using keys
The safest credential is one that is never downloaded. Attach the dedicated account when you create the VM, and the platform supplies short-lived tokens automatically through the metadata server:
gcloud compute instances create app-1 \
--zone=us-central1-a \
--service-account=app-backend@PROJECT_ID.iam.gserviceaccount.com \
--scopes=https://www.googleapis.com/auth/cloud-platform
For an existing instance, stop it, set the service account with gcloud compute instances set-service-account, and start it again. Application Default Credentials in client libraries will find the attached identity without any key file on disk.
Service Account Keys: Why to Avoid Them
A downloaded JSON key does not expire by default, is easy to leak in Git history or CI logs, and is difficult to rotate safely. Prefer attached accounts, workload identity, or impersonation. Then prevent new keys with an organisation policy so the safe path becomes the default for everyone:
# Organisation policy constraint that disables service account key creation:
# constraints/iam.disableServiceAccountKeyCreation
gcloud org-policies describe constraints/iam.disableServiceAccountKeyCreation \
--organization=ORGANIZATION_ID
Apply the constraint in the console under Organization Policies once you have migrated existing workloads, and inventory old keys first so you do not break a legacy job without warning.
Impersonation and Short-Lived Credentials
Impersonation lets an authorised identity obtain a short-lived token for a service account without holding its key. Grant roles/iam.serviceAccountTokenCreator only to the people or CI systems that truly need it:
gcloud iam service-accounts add-iam-policy-binding \
app-backend@PROJECT_ID.iam.gserviceaccount.com \
--member="user:[email protected]" \
--role="roles/iam.serviceAccountTokenCreator"
gcloud auth print-access-token \
--impersonate-service-account=app-backend@PROJECT_ID.iam.gserviceaccount.com
Tokens typically last one hour. That short lifetime limits the damage if a token leaks and creates a clean audit trail of who impersonated what, and when.
Custom Roles, Recommender and Policy Analyzer
When a predefined role grants ten permissions and you need two, create a custom role in the console under IAM > Roles, or copy a predefined role and remove the excess. Review custom roles quarterly because new product features will not be added automatically. Use IAM Recommender to find permissions that have not been used in 90 days and safely remove them, and use Policy Analyzer to answer who has access to a resource and what a given member can actually do, including inherited bindings. Together they turn auditing from guesswork into a repeatable routine.
Troubleshooting
Permission denied errors
Note the exact permission named in the error, then check whether the correct member has a role containing it on the correct resource. Wait a minute after a change: IAM propagation is fast but not instant. Also check for deny policies and organisation constraints that can override an allow binding.
Role granted at the wrong level
A binding on the project will not help with a folder-level task, and a binding on one bucket does not apply to another. Trace inheritance from organisation down to the resource in Policy Analyzer instead of adding broader roles until the error disappears.
Default service account is over-privileged
If your VM’s default account holds Editor, create a dedicated account, grant the minimal roles, attach it, test the workload, and only then remove Editor. Never remove the old binding before the new identity is proven.
Conclusion
Secure google cloud service accounts are built on a few repeatable habits: one identity per workload, predefined roles at the lowest possible level, no downloaded keys, impersonation with short-lived tokens, and regular audits with Recommender and Policy Analyzer. Apply them project by project, starting with anything holding Editor or Owner, and your largest GCP security risk becomes one of your strongest controls.