Overview and What You Will Learn
Google Cloud IAM has three kinds of roles: basic roles (Owner, Editor, Viewer), predefined roles that Google maintains for one service's job functions, and custom roles you build from individual permissions. Least privilege means starting with the narrowest predefined role that fits, and creating a custom role only when no predefined role matches. A role does nothing on its own until a binding attaches it to a member at a scope.
In this lab you will:
- Grant the Editor basic role and count the permissions it carries
- Replace it with a service-scoped predefined role
- Build a custom role for a narrower job: view, start and stop VMs only
- Prove with Policy Troubleshooter what the member can and cannot do
- Update the custom role and check IAM Recommender for unused access
Why This Matters in Production
A new engineer gets Editor "to keep things simple". At the next access review, a payments company such as Razorpay or Stripe finds they could modify almost every resource type in the project, including services they never touch. A role scoped to the real job makes that review a non-event and limits the damage if the engineer's credentials leak.
Core Principles
Basic roles are broad and mostly legacy, predefined roles are scoped to one service, and custom roles let you choose each permission. Move down the list only when the previous option is too broad or too narrow.
Owner, Editor, Viewer] -->|too broad| P[Predefined roles
Google maintained] P -->|still too broad
or too narrow| C[Custom roles
you pick permissions] classDef broad fill:#EF4444,color:#FFFFFF classDef mid fill:#326CE5,color:#FFFFFF classDef exact fill:#10B981,color:#FFFFFF class B broad class P mid class C exact
| Role type | Maintained by |
|---|---|
| Basic | Google, avoid in production |
| Predefined | Google, updated as services add permissions |
| Custom | You, fixed until you change it |
IAM is built from three parts: a member (user, group or service account), a role, and a scope (resource, project, folder or organization). Policies inherit downward, so a grant at a folder applies to every project inside it.
Detailed Step-by-Step Practical Lab
Milestone 1: Create the Lab Project
Compute Engine needs billing linked before you can enable it. Use a suffix so the project ID is unique.
export PROJECT_ID=iam-lab-$(date +%s | tail -c 6)export MEMBER="user:rahul@devops.in" gcloud projects create $PROJECT_ID --name="IAM Lab"gcloud config set project $PROJECT_IDgcloud billing projects link $PROJECT_ID --billing-account=0A1B2C-3D4E5F-6A7B8Cgcloud services enable compute.googleapis.com iam.googleapis.comMilestone 2: Grant Editor and Measure It
Editor looks convenient, so measure what it actually contains.
gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="$MEMBER" \ --role="roles/editor" gcloud iam roles describe roles/editor \ --format="value(includedPermissions)" | tr ';' '\n' | wc -lThe count runs to thousands of permissions across most services. That is the exposure you are removing.
Milestone 3: Switch to a Predefined Role
A predefined role limits access to one service. Remove Editor first so the member does not keep both.
gcloud projects remove-iam-policy-binding $PROJECT_ID \ --member="$MEMBER" \ --role="roles/editor" gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="$MEMBER" \ --role="roles/compute.instanceAdmin.v1" # See how much narrower this isgcloud iam roles describe roles/compute.instanceAdmin.v1 \ --format="value(includedPermissions)" | tr ';' '\n' | wc -lMilestone 4: Build a Custom Role
Suppose this engineer must only see, start and stop VMs, never create or delete them. The predefined admin role still allows creation and deletion, so list the available permissions and choose exactly what is needed.
# Permissions available on the resource, with custom-role support levelgcloud iam list-testable-permissions \ //cloudresourcemanager.googleapis.com/projects/$PROJECT_ID \ --filter="name:compute.instances" \ --format="table(name, customRolesSupportLevel)" gcloud iam roles create customVmOperator \ --project=$PROJECT_ID \ --title="Custom VM Operator" \ --description="View, start, and stop VMs. No create or delete." \ --permissions="compute.instances.get,compute.instances.list,compute.instances.start,compute.instances.stop" \ --stage=GANot every permission can be put in a custom role. Check the support level column and avoid permissions marked as not supported.
Milestone 5: Swap in the Custom Role
Replace the predefined grant with the custom role. Custom roles are referenced by their full path.
gcloud projects remove-iam-policy-binding $PROJECT_ID \ --member="$MEMBER" \ --role="roles/compute.instanceAdmin.v1" gcloud projects add-iam-policy-binding $PROJECT_ID \ --member="$MEMBER" \ --role="projects/$PROJECT_ID/roles/customVmOperator"Milestone 6: Verify What the Member Can Do
Look at the policy, then ask Policy Troubleshooter whether specific permissions are granted. IAM changes can take a few minutes to propagate.
# The member should have only the custom rolegcloud projects get-iam-policy $PROJECT_ID \ --flatten="bindings[].members" \ --filter="bindings.members:rahul@devops.in" \ --format="table(bindings.role)" # Granted: stopping a VMgcloud policy-troubleshoot iam \ //cloudresourcemanager.googleapis.com/projects/$PROJECT_ID \ --principal-email=rahul@devops.in \ --permission=compute.instances.stop # Not granted: deleting a VMgcloud policy-troubleshoot iam \ //cloudresourcemanager.googleapis.com/projects/$PROJECT_ID \ --principal-email=rahul@devops.in \ --permission=compute.instances.deletePolicy Troubleshooter needs its API enabled the first time. The output reports GRANTED or NOT_GRANTED and explains which binding decided it.
Milestone 7: Evolve the Role and Review Unused Access
When a job changes, update the role instead of creating a new one. Then ask IAM Recommender which grants are unused.
# Add a permission the engineer now needs (for example, reset a VM)gcloud iam roles update customVmOperator \ --project=$PROJECT_ID \ --add-permissions="compute.instances.reset" # Recommendations to remove or shrink roles based on observed usagegcloud recommender recommendations list \ --recommender=google.iam.policy.Recommender \ --project=$PROJECT_ID \ --location=global \ --format="table(name.basename(), description)"Recommender needs usage history, so a brand new lab project may return nothing. In long-lived projects it is one of the best ways to find over-granted roles.
Milestone 8: Clean Up
gcloud projects delete $PROJECT_ID --quietProduction Best Practices & Common Pitfalls
- Prefer predefined roles. Google keeps them current, so they gain new permissions as a service grows.
- Create custom roles only for real gaps. Each one is something your team must maintain.
- Grant to groups, not individuals. Membership changes then need no policy edits.
- Grant at the lowest workable scope. Use a resource or project grant before a folder or organization grant.
- Review access on a schedule. Use IAM Recommender and audit logs to remove unused grants.
- Manage roles as code. Keep role definitions in Terraform or YAML files under review.
- Test before rollout. Verify with Policy Troubleshooter, and check Cloud Console pages if people use it.
Common MistakeGranting Editor or Owner by default because it avoids permission errors. These roles span nearly every service in the project, so use a predefined role for the service actually being used.
TipCheck whether a predefined role already fits before writing a custom one. For read-only VM access,
roles/compute.vieweralready exists, and a custom copy only adds maintenance.
RememberA custom role is a fixed list. When Google adds permissions to a service, predefined roles pick them up and custom roles do not.
SecurityRoles that can manage IAM, such as
roles/iam.securityAdminor any role containingsetIamPolicypermissions, can grant themselves anything else. Treat them as equivalent to Owner and restrict them tightly.
Custom roles that work in gcloud may need extra read permissions before Cloud Console pages load cleanly, so test the actual workflow people use. A deleted custom role is retained for a period before it is permanently removed, so plan names accordingly.
Quick Reference & Troubleshooting Commands
| Command | Description |
|---|---|
gcloud iam roles describe ROLE |
Show every permission in a role |
gcloud iam list-testable-permissions RESOURCE |
List permissions available on a resource |
gcloud iam roles create |
Create a custom role |
gcloud iam roles update |
Add or remove permissions on a custom role |
gcloud projects add-iam-policy-binding |
Grant a role at project level |
gcloud projects get-iam-policy |
View the full project IAM policy |
gcloud policy-troubleshoot iam |
Check whether a principal has a permission |
| Symptom | Likely cause and fix |
|---|---|
| Permission denied right after a grant | Propagation delay, wait a few minutes and retry |
| Custom role creation rejects a permission | Permission not supported in custom roles, check the support level |
| Works in gcloud but not Console | Missing supporting read permissions, add the minimum needed |
| Member still has too much access | Inherited grant from a folder, group or organization, inspect the effective policy |