Prepare and test your skills

Prepare and test your skills

A service account (opens in a new tab) is a special Identity and Access Management (IAM) identity used by an application or compute workload instead of by a person. Each service account is identified by a unique email address and gives the workload an identity for making authorized API calls. When an application authenticates as a service account, the application receives access to all Google Cloud resources that the service account has permission to access.
User-managed service accounts (opens in a new tab) are service accounts that you create in your own project for use as workload identities. You create a user-managed service account with the IAM API, the Google Cloud console, or the Google Cloud CLI, and you choose a name that forms the email address service-account-name@project-id.iam.gserviceaccount.com. The number of accounts you can create is limited by the Service Account Count quota for the project. You have full control over these accounts, including updating, disabling, deleting, and controlling which other principals can use them.
Default service accounts are user-managed service accounts that are created automatically when you enable or use certain Google Cloud services. These accounts let the service deploy jobs that access other Google Cloud resources, and after creation you are responsible for managing them just like any account you created yourself. The App Engine default service account uses project-id@appspot.gserviceaccount.com and the Compute Engine (opens in a new tab) default service account uses PROJECT_NUMBER-compute@developer.gserviceaccount.com.
Default service accounts are convenient for getting started because an application running in that environment can use the default credentials to call Google Cloud APIs. Depending on organization policy, a default service account might automatically receive the Editor role (roles/editor) on the project, which lets it read and modify all resources in the project. Because sharing one powerful account across many applications increases risk, the recommended practice is to disable the automatic grant with the iam.automaticIamGrantsForDefaultServiceAccounts constraint and instead create single-purpose accounts with least-privilege roles.
Service agents (opens in a new tab) are service accounts that Google Cloud creates and manages so Google services can access resources on your behalf. A service agent represents the service itself, such as Cloud Run accessing a Pub/Sub topic or Compute Engine performing internal duties, and it is not created in your project. Two common examples are the Google APIs Service Agent (PROJECT_NUMBER@cloudservices.gserviceaccount.com) and the Compute Engine Service Agent (service-PROJECT_NUMBER@compute-system.iam.gserviceaccount.com).
Service agents typically already have the access the Google service needs, such as the compute.serviceAgent role for the Compute Engine Service Agent. You cannot attach a service agent to a Compute Engine instance, and you should generally avoid changing its roles because revoking access can break managed instance groups, autoscaling, or software running on your virtual machines (VMs).
The three service account types differ by who creates them, who maintains them, and whether workloads can use them directly. The following comparison shows the ownership and intended use for each type:
| Type | Created and managed by | Can be attached to a VM | Intended use |
|---|---|---|---|
| User-managed service account | Created and managed by you | Yes | Dedicated identity for one application or workload with only the roles it needs |
| Default service account | Created automatically, then managed by you | Yes | Quick start for App Engine or Compute Engine workloads; replace with dedicated accounts for production |
| Service agent | Created and managed by Google Cloud | No | Lets a Google Cloud service act on your behalf for internal processes |
Use a dedicated user-managed service account when you need clear ownership, least privilege, and traceability for an application. Use a default service account only for simple trials or when you explicitly manage its roles, since sharing it across applications makes lifecycles and audit logs hard to separate.
Attachment to a Compute Engine instance gives applications on that instance the service account credentials for authentication to Compute Engine (opens in a new tab). Only user-managed service accounts, including default service accounts, can be attached to an instance, and an instance can have only one attached service account at a time. You can specify the account when you create the VM or change it later, and the Compute Engine default service account is attached by default to VMs created with the console or Google Cloud CLI unless you choose a different account or no account.
Authorization for the workload depends on two controls that must both allow access: the IAM roles granted to the attached service account and the access scopes set on the instance. Access scopes set the default OAuth scopes for requests from the gcloud CLI and client libraries, so a narrow scope can block an API even when the IAM role allows it. The best practice is to set the full cloud-platform scope on the instance and then control access with IAM roles, and to reuse the same account on multiple VMs only when those VMs truly need identical permissions.
Exam tip: Only user-managed service accounts, including default service accounts, can be attached to a VM; service agents are Google-managed and cannot be attached.
IAM roles granted to an assigned service account (opens in a new tab) control what that account can do after it is attached to a resource. When a Compute Engine virtual machine (VM) or Cloud Run service runs with a service account, code on that resource calls Google Cloud APIs using the service account identity, and IAM allows only the operations included in its roles. Access scopes on the resource, such as the VM access scope, can further limit which APIs are reachable, but they never add permissions beyond the IAM roles.
An attached service account acts in two ways: as a resource that is attached and as a principal that makes requests. As a resource, the service account must be attached to a VM or Cloud Run service by someone with permission on the account itself. As a principal, the service account must have its own IAM roles that permit the API operations the workload needs, such as reading storage, writing logs, or connecting to a database.
| Service account role | What it means | What permission is needed |
|---|---|---|
| As a resource | A deployer attaches the account to a VM, instance, or Cloud Run service or revision | Deployer needs roles/iam.serviceAccountUser, which contains the iam.serviceAccounts.actAs permission on that service account |
| As a principal | Code running on the attached resource calls Google Cloud APIs with the account identity | Service account needs IAM roles for only the APIs and operations the workload will perform |
If a Cloud Run service does not call other Google Cloud services, the service identity does not need additional roles. On Compute Engine, setting the cloud platform scope (https://www.googleapis.com/auth/cloud-platform) on the VM lets the attached service account reach the APIs its IAM roles allow, which appears in the console as Allow full access to all Cloud APIs.
Least privilege for an assigned service account means giving the account only the permissions its workload needs and nothing more. Roles can be granted when the account is created and revoked or changed later, so access can shrink or grow as the workload changes. Google Cloud recommends creating a dedicated user-managed service account instead of reusing the Compute Engine default service account, because a dedicated account keeps permissions separate for each workload.
Least-privilege practices supported for assigned service accounts include:
Restricting who can attach a service account prevents an unauthorized user from deploying a resource that borrows its permissions. A user can attach a service account to a new VM or Cloud Run service only after an administrator grants that user the Service Account User role (roles/iam.serviceAccountUser) on that specific service account. This separation means one set of roles controls who can use the account, while a different set of roles controls what the account can access.
Assigning and using a service account follows an ordered flow:
A four-step process flow showing how to create a user-managed service account, grant it IAM roles, authorize the deployer with roles/iam.serviceAccountUser, and attach the account to a VM with gcloud, followed by a note that access requires both IAM roles and access scopes.
gcloud iam service-accounts create.gcloud projects add-iam-policy-binding, which sets what APIs the workload can call.roles/iam.serviceAccountUser on the service account to the user who will attach it, which permits that user to act as the account.gcloud compute instances create with the --service-account flag set to the account email.Only after step 3 can the deployer complete step 4, so the workload depends on both the service account's own roles and the deployer's attach permission.
Exam tip: In production, do not grant Owner, Editor, or Viewer to a service account; grant a predefined role or custom role that meets the workload's needs.
Google Cloud service accounts (opens in a new tab) provide a workload identity that lets code running on Google Cloud call other Google Cloud services without storing user passwords or long-lived keys. When a service account is attached to a compute resource, such as a Compute Engine virtual machine (VM) instance, the resource uses that service account as its default identity. Any workload running on the resource then presents the service account credentials when it accesses Google Cloud APIs, so Identity and Access Management (IAM) checks the service account permissions, not the developer who deployed the code.
An attached service account is a service account that is bound to a resource so the service account acts as the resource default identity. This approach is the preferred authentication method for code running on a Google Cloud compute resource, because credentials flow automatically from the attached resource to the workload and then to the requested Google Cloud service. Workloads running on Google Cloud can use several identity methods that differ in how credentials reach the target service:
Attachment timing determines whether a workload can receive a new identity after it already exists. For most services, the service account must be attached when the resource that will run the code is created, and the service account cannot be added or replaced later. Compute Engine is an exception because a service account can be attached to a VM instance at any time, including after the instance already exists.
Exam tip: Plan the service account before creating Cloud Run services and most managed resources, because only Compute Engine VMs allow attaching or changing the service account later.
Identity propagation means the permissions of the attached service account travel with every request the workload makes to another Google Cloud service. If the attached service account has been granted a role on a project, bucket, dataset, or topic, then code on the VM or managed resource can perform the actions allowed by that role. If the service account has no role on the target resource, then requests from the workload to that target resource are denied by IAM, even though the workload is running successfully.
Because propagation reuses one identity for all calls from the resource, least privilege depends on the roles granted to the service account. Roles granted to the service account can be revoked or expanded later without recreating the service account or the compute resource. In production environments, do not grant the Owner, Editor, or Viewer roles; instead grant a predefined role or custom role that meets the workload needs.
The Google Cloud CLI workflow creates the identity, defines what the identity can access, defines who can use the identity, and then binds the identity to the compute resource. The person performing these steps needs the Create Service Accounts IAM role (roles/iam.serviceAccountCreator) and the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). The person who will attach the service account to resources also needs the Service Account User role (roles/iam.serviceAccountUser) on that service account.
The creation and attachment workflow runs in this order:
gcloud init to install and initialize the Google Cloud CLI, and sign in with a federated identity first when using an external identity provider.gcloud iam service-accounts create SERVICE_ACCOUNT_NAME to create a service account with the chosen name in the project.gcloud projects add-iam-policy-binding PROJECT_ID with --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" and --role=ROLE to let the service account access project resources, and repeat the command to grant each additional role.gcloud iam service-accounts add-iam-policy-binding SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com with --member="user:USER_EMAIL" and --role=roles/iam.serviceAccountUser to allow that user to attach the service account to other resources.gcloud compute instances create INSTANCE_NAME --zone=ZONE --service-account=SERVICE_ACCOUNT_EMAIL creates a Compute Engine VM in the specified zone with the specified service account as its identity.User-managed service accounts are created and managed by you and default service accounts are created automatically and then managed by you, and both can be attached to a Compute Engine virtual machine (VM) as a dedicated or quick-start workload identity. Service agents are created and managed by Google Cloud so Google services can act on your behalf for internal processes and they cannot be attached to a VM.
Use a dedicated user-managed service account for production workloads that need clear ownership, least privilege, and traceability with only the roles the application needs. Use the Compute Engine default service account only for simple trials or when you explicitly manage its roles, because sharing one powerful account across many applications increases risk and makes lifecycles and audit logs hard to separate.
Access requires both Identity and Access Management (IAM) roles granted to the attached service account and sufficient access scopes set on the instance, and access scopes can further limit which APIs are reachable but never add permissions beyond the IAM roles. The best practice is to set the full cloud-platform scope on the instance and then control access with least-privilege IAM roles.
A user can attach a service account only after an administrator grants that user the Service Account User role (roles/iam.serviceAccountUser) on that specific service account, which contains the iam.serviceAccounts.actAs permission. This separation means one set of roles controls who can use the account while a different set of roles controls what the account can access.
A developer is deploying an internal reporting application on a new Compute Engine virtual machine (VM). The application requires read-only access to objects inside a single Cloud Storage bucket named analytics-reports-prod.
The workload must not have access to any other buckets or services in the project, and the developer must be able to attach the identity to the VM while adhering to the principle of least privilege.
How should you configure the service account, permissions, and VM settings?
Last updated