Prepare and test your skills

Prepare and test your skills

Identity and Access Management (IAM) (opens in a new tab) controls what a service account (opens in a new tab) can do by bundling permissions into roles and granting those roles to the account. A service account is treated as a principal, which is an authenticated identity that can be listed in an allow policy attached to a project, folder, organization, or specific resource. When an application runs as a service account on a Compute Engine instance, the application receives only the authorization conferred by the roles granted to that service account.
Basic roles are broad, legacy IAM roles that grant wide capabilities across a project. The basic roles include Owner (roles/owner), Editor, and Viewer, and they give a service account far more permissions than most workloads need. The Compute Engine default service account might automatically receive the Editor role on its project, which allows it to view and modify almost all resources. Basic roles should not be granted in a production environment because they violate least privilege, although they can be used in a development or test environment.
Predefined roles are Google-managed IAM roles that grant specific capabilities for a particular service or use case. Granting a predefined role to a service account limits the account to only the permissions included in that role, and multiple predefined roles can be combined to match the workload. Common examples that show this focused scope include:
roles/compute.instanceAdmin.v1 for managing Compute Engine instancesroles/storage.objectViewer for reading Cloud Storage objects without changing themroles/compute.networkAdmin for managing networks, subnets, firewall rules, and devices such as Cloud Router and Cloud VPNWhen a service account holds focused roles together, applications using that account cannot perform actions outside those roles, even with a broad access scope. Other focused predefined roles include Service Account Admin (roles/iam.serviceAccountAdmin) for managing service accounts, Project Creator (roles/resourcemanager.projectCreator) for creating projects, and Service Usage Admin (roles/serviceusage.serviceUsageAdmin) for enabling APIs.
Custom roles are customer-defined IAM roles that contain a hand-picked set of permissions when no predefined role fits the need. A custom role is useful when predefined roles are too broad or too narrow for a service account that performs one specific job. The custom role is then granted to the service account in the same way as a predefined role, through the allow policy on the relevant resource. Choose a custom role when the workload needs a minimum set of permissions that does not match an existing predefined role.
IAM roles and access scopes together determine what a service account on a virtual machine (VM) instance can actually do. IAM roles granted to the service account define the maximum permissions available, while the instance access scopes define the default OAuth scopes used by the gcloud CLI and client libraries and can further limit API access. The best practice is to set the full cloud-platform access scope (https://www.googleapis.com/auth/cloud-platform) on the instance and then control access by granting IAM roles to the service account. If a restrictive scope is used instead, such as Cloud Storage read-only (https://www.googleapis.com/auth/devstorage.read_only), requests cannot manage objects even if the service account holds roles/storage.objectAdmin, because the scope does not authorize the change.
A Venn diagram with IAM roles on the left and VM access scopes on the right; only their overlap is effective allowed access, with examples of cloud-platform and read-only scopes.
Least privilege means granting a service account only the minimum permissions required for its workload and nothing more. For production, this means creating a user-managed service account, attaching it to the resource that runs the code, and granting only the predefined or custom roles the application needs to function. If the default service account already has the Editor role, replace it with less permissive roles and use Policy Simulator to see the impact before changing grants, plus role recommendations to downscope permissions over time.
The three role types compare differently for least-privilege design:
| Role type | Scope of permissions | Use for a service account when |
|---|---|---|
| Basic | Very broad access across a project | Only in development or test, never for production least privilege |
| Predefined | Focused capabilities for a service or task | A Google-managed role matches the workload, such as object viewer or workflows invoker |
| Custom | Exact set chosen by the administrator | No predefined role matches and a smaller, tailored set is needed |
To prevent overly broad access from the start, enforce the iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint to disable automatic Editor grants to default service accounts. Organizations created after May 3, 2024 have this constraint enforced by default, and administrators in other organizations must then decide which narrower roles to grant themselves.
Exam tip: Do not grant Owner, Editor, or Viewer to a service account in production; grant a predefined or custom role that contains only the required permissions.
Security controls for restricting service account use are IAM and configuration settings that limit what a service account (opens in a new tab) can access and who can act as that account. A service account is both a principal that can be granted access to resources and a resource that other principals can access or impersonate. Because of this dual nature, controls must limit the account's privileges and protect the account itself from unauthorized use.
Service account abuse happens when an attacker gains the permissions of a service account or hides activity behind it. A compromised service account can affect every workload that uses it, because applications running with that account inherit its IAM roles. Common abuse patterns include privilege escalation, identity hiding, and information leakage:
jenkins@deployment-project-123.iam.gserviceaccount.com revealing a project ID and software type.Limiting privileges reduces harm if a service account is compromised, while limiting who can access the service account reduces the chance of compromise. Both sides must be configured together to enforce least privilege.
Least privilege with IAM role grants means giving a service account only the permissions its workload needs and nothing more. When an application authenticates as a service account, it receives access to all resources that the service account can access, so extra roles directly expand the blast radius. In production, avoid granting the basic Owner, Editor, or Viewer roles and instead grant predefined roles or custom roles that match the task, such as Storage Object Viewer (roles/storage.objectViewer) for reading objects in a Cloud Storage bucket.
Default service accounts need special attention because they may automatically receive the Editor role (roles/editor) on the project, which allows reading and modifying all resources in the project. To block this broad grant, enforce the constraints/iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint, which disables automatic IAM grants for default service accounts. If a service account already has overly broad access, replace the broad role with narrower roles and use tools like Policy Simulator and role recommendations to check impact and remove unused permissions.
Access scopes are an additional control for applications running on a Compute Engine (opens in a new tab) virtual machine (VM) instance with an attached service account. Authorization on a VM is limited by two separate configurations, and both the IAM roles granted to the service account and the access scopes set on the instance must allow access before the application can reach a resource. Access scopes define the default OAuth scopes for requests made through the gcloud CLI and client libraries, apply per VM for the life of the VM, and do not apply to calls made with gRPC.
The best practice is to set the full cloud-platform scope (https://www.googleapis.com/auth/cloud-platform) on the instance and then control access with IAM roles. A narrow scope can further restrict OAuth requests even when IAM would allow them, while a broad scope does not expand access beyond IAM.
| Control | What it controls | Where and how it applies |
|---|---|---|
| IAM roles | Which APIs and resources the service account can use, based on granted permissions | Applies to the service account everywhere it is used; inherited by all VMs and workloads using that account |
| Access scopes | Which OAuth methods the VM can call through gcloud CLI and client libraries | Applies per VM for the life of the VM; further limits OAuth but does not extend to gRPC and has no effect if the related API is not enabled |
For example, a VM with the cloud-platform scope and only roles/compute.instanceAdmin.v1, roles/storage.objectViewer, and roles/compute.networkAdmin cannot perform actions outside those three roles. In contrast, a VM with only the Cloud Storage read-only scope (https://www.googleapis.com/auth/devstorage.read_only) cannot manage objects even if the service account has roles/storage.objectAdmin, because the scope does not authorize writes.
Exam tip: Access succeeds only when both IAM and access scopes allow it, so set the broad cloud-platform scope and restrict with IAM roles.
Service account impersonation occurs when an authenticated principal, such as a user or another service account, authenticates as a service account to gain that account's permissions. Impersonation lets the principal access whatever the service account can access without changing IAM policies on the target resources. Only principals with the appropriate permissions can impersonate, which makes those permissions a key security boundary.
To prevent unauthorized use, grant impersonation-related roles only to principals that need them. The Service Account User role (roles/iam.serviceAccountUser) with the iam.serviceAccounts.actAs permission lets a principal attach the service account to resources and act as it, while the Service Account Token Creator role (roles/iam.serviceAccountTokenCreator) with the iam.serviceAccounts.getAccessToken permission lets a principal create short-lived tokens for local testing or elevation. Avoid granting the Service Account Admin role (roles/iam.serviceAccountAdmin), domain-wide delegation, or broad shell access unless required, and use OS Login or Identity-Aware Proxy controls so VM login stays at least as restrictive as access to the attached service account.
Service account keys are long-lived credentials used to sign a JSON Web Token (JWT) and exchange it for an access token, and they create risk if the private key is leaked or poorly managed. The safer pattern is to avoid keys and use automatically obtained short-lived credentials, such as an attached service account with Application Default Credentials (ADC), Workload Identity Federation for workloads outside Google Cloud, or Workload Identity Federation for GKE for pods. If key creation must be blocked, disable it with organization policy under secure-by-default controls so users cannot create new user-managed keys.
Unused service accounts also expand risk because they may retain old role bindings that enable lateral movement or privilege escalation. Disable a service account as soon as its workload is removed, and delete it only after confirming it is no longer needed. Disabling preserves IAM bindings so the account can be re-enabled safely, while deleting and recreating an account with the same name creates a different internal identity that does not inherit the old bindings or attachments.
Identity and Access Management (IAM) (opens in a new tab) controls who can do what in Google Cloud by attaching an allow policy to each resource. An allow policy is a list of bindings, and each binding pairs a principal with a role that contains permissions. A service account (opens in a new tab) is a special kind of principal used by applications and workloads, so managing its IAM policies means controlling both what the service account can access and who can use the service account.
A service account has two IAM identities because it acts as both a principal that uses resources and a resource that others can use. As a principal, the service account is granted roles on buckets, projects, instances, and other resources so its workload is authorized to act. As a resource, the service account has its own allow policy that controls which users or other service accounts can view it, edit it, attach it to compute resources, or impersonate it.
| Service account role | What the policy controls | Example |
|---|---|---|
| Service account as principal | What the service account can access | Grant the Storage Object Viewer role (roles/storage.objectViewer) on a Cloud Storage bucket to the service account |
| Service account as resource | Who can use or manage the service account | Grant the Service Account User role (roles/iam.serviceAccountUser) on the service account to a user who will attach it to a virtual machine (VM) |
A common split is to grant a workload role to the service account on a project or bucket, and then grant a usage role on the service account itself to the developer. For example, the service account receives access to project resources, while a user receives roles/iam.serviceAccountUser on the service account email SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com so that user can attach that service account when creating a Compute Engine instance.
The Google Cloud resource hierarchy organizes access from broad containers down to specific items in the order of organization, then folder, then project, then service-specific resources. Container resources are projects, folders, and organizations, while service-specific resources include Compute Engine instances, disks, subnetworks, images, and Cloud Storage buckets. A role granted on a container resource is inherited by every service-specific resource it contains, so a project-level grant applies throughout that project.
Inheritance means the level where a policy is attached decides how broad the service account permission becomes. Granting a role at the organization or folder level gives the service account the same permissions in every child project, which is useful for central administration but increases risk. Granting the same role on a single bucket, image, or instance limits the service account to just that resource and provides more granular, least-privilege control.
IAM access is changed by editing the allow policy that is attached to the target resource to add, update, or remove a principal and role. In the Google Cloud console, an administrator selects the principal, clicks Edit principal, clicks Add another role, selects a role such as Workflows Invoker or Cloud Tasks Enqueuer, and then clicks Save. The change takes effect because IAM checks that updated allow policy the next time the principal requests the resource, and additional roles can be added or revoked later in the same way.
The gcloud command line adds the same bindings without using the console, with one command form for giving access to a service account and another for giving access on a service account. To give a service account access to a project, run a binding where the project is the destination and the service account is the member:
gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role=ROLE
To let a user attach or manage an existing service account, run a binding where the service account is the destination and the user is the member:
gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com --member="user:USER_EMAIL" --role=roles/iam.serviceAccountUser
The same pattern works at other hierarchy levels by changing the target, such as gcloud organizations add-iam-policy-binding for an organization or gcloud compute images add-iam-policy-binding for a specific image. Member identifiers name both the identity type and the address, and supported formats include the following:
user:test-user@gmail.com for a Google Account emailgroup:admins@example.com for a Google groupserviceAccount:test123@example.domain.com for a service accountdomain:example.domain.com for a Google Workspace domainprincipal://iam.googleapis.com/locations/global/workforcePools/POOL/subject/USER for an external workforce identityREST API changes follow an ordered read-edit-write workflow that preserves the rest of the existing policy. The workflow moves policy data from the resource to the administrator and back to the resource:
getIamPolicy request to the resource, such as POST https://cloudresourcemanager.googleapis.com/v1/projects/PROJECT_ID:getIamPolicy for a project."members": ["group:admins@example.com"] with "role": "roles/compute.storageAdmin".setIamPolicy request to the same resource, such as POST https://cloudresourcemanager.googleapis.com/v1/projects/PROJECT_ID:setIamPolicy, with the full updated policy in the request body.Exam tip: In production, do not grant the basic Owner, Editor, or Viewer roles to a service account; grant a predefined role or custom role that meets the need instead.
iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint disables automatic Editor grants to default service accounts and is enforced by default for organizations created after May 3, 2024.Basic roles grant very broad access across a project, predefined roles grant focused capabilities for a particular service or use case, and custom roles contain an exact administrator-chosen set of permissions. Basic roles include Owner, Editor, and Viewer and should not be granted in a production environment because they violate least privilege. Predefined roles are Google-managed and can be combined to match the workload, while a custom role is useful when predefined roles are too broad or too narrow and is granted through the allow policy like a predefined role.
Access from a virtual machine (VM) instance succeeds only when both the Identity and Access Management (IAM) roles granted to the attached service account and the instance access scopes allow the request. Identity and Access Management (IAM) roles define the maximum permissions available, while access scopes define the default OAuth scopes used by the gcloud CLI and client libraries and can further limit API access. The best practice is to set the full cloud-platform access scope on the instance and then control access by granting Identity and Access Management (IAM) roles to the service account.
A service account is both a principal that is granted roles to access resources and a resource that has its own allow policy controlling who can use or impersonate it. As a principal, it receives roles on buckets, projects, instances, and other resources so its workload is authorized to act. As a resource, specific users can receive roles such as Service Account User on the service account itself to attach it when creating a Compute Engine instance.
To enforce least privilege, grant the role on a single bucket, image, or instance to limit the service account to just that resource. A role granted on an organization, folder, or project is inherited by every contained resource, so a project-level grant applies throughout that project. Grants at the organization or folder level give the same permissions in every child project, which helps central administration but increases risk.
A backend data processing application runs on a Compute Engine virtual machine and authenticates to Google Cloud services using a dedicated service account. The application only needs to read data objects from Cloud Storage buckets within a production project and does not interact with any other service.
You need to assign an Identity and Access Management (IAM) role to the service account following Google-recommended best practices and the principle of least privilege.
Which role should you assign to the service account?
The basic Editor role (roles/editor) on the project
The predefined Storage Object Viewer role (roles/storage.objectViewer) on the target bucket or project
The basic Viewer role (roles/viewer) on the project
A custom IAM role created to mirror the Storage Object Viewer permissions, because custom roles are always preferred over predefined roles in production
Last updated