Prepare and test your skills

Prepare and test your skills

Manage Service Account Keys and Security Controls is the practice of avoiding long-lived keys and using Identity and Access Management (IAM) controls when keys must exist. A service account (opens in a new tab) in Google Cloud is a non-human identity used by an application or workload, such as a Compute Engine instance, to make authorized API calls. Each service account is identified by a unique email address and is both a principal that can be granted access and a resource that belongs to a project and must be protected.
Service account keys are downloadable, long-lived private keys that let an application sign a JSON Web Token (JWT) and exchange it for an OAuth 2.0 access token in any environment. Service account keys differ from short-lived credentials, which are created automatically when a workload uses an attached service account or when a developer uses the gcloud flag --impersonate-service-account. Because anyone who holds the private key file can authenticate as the service account, a leaked or unmanaged key creates lasting exposure.
A compromised account or key can be abused in related ways:
Defense must address both sides of the account. Because the service account is a principal, its roles must follow least privilege so compromise causes less harm. Because the service account is also a resource, access to impersonate or attach it must be restricted.
Choosing a safer authentication method means using short-lived, automatically managed credentials instead of creating a service account key whenever possible. Tools that support Application Default Credentials (ADC), including the Google Cloud CLI, Cloud Client Libraries, Terraform, and REST requests, should use an attached identity or federation rather than a downloaded key. Keys are the last resort, used only when no federated or attached option works.
A decision tree that branches on dev environment, Google Cloud, GKE, service account need, and external identity to choose attached service account, Workload Identity Federation, impersonation, user credentials, or keys last.
| Authentication option | Where it applies | How credentials flow |
|---|---|---|
| Attached service account | Code running in Google Cloud on Compute Engine or similar resources | The workload authenticates as the account attached to the resource, and Google Cloud manages short-lived tokens |
| Workload Identity Federation for GKE | Containers in Google Kubernetes Engine (GKE) (opens in a new tab) | Federation attaches service accounts to Kubernetes pods, so pods get short-lived credentials without keys |
| Workload Identity Federation | Workloads on-premises or on another cloud with a supported external identity | The external workload federates its existing identity to impersonate a service account and get short-lived credentials |
| Service account impersonation | Single-user development on a workstation, Cloud Shell, or virtual desktop | The developer signs in with user credentials, then impersonates the service account for temporary access |
| Service account key | Only when no federated or attached option is available | The app uses the downloaded key to sign a JWT and exchange it for an access token, and the owner must secure the key |
When code runs in Google Cloud, first attach a service account to the resource and grant only the IAM roles it needs. When code runs outside Google Cloud, first configure Workload Identity Federation (opens in a new tab) so the outside workload can use a service account without storing a key.
Exam tip: Service account keys are the last choice; if key creation fails, key creation is likely disabled by organization policy and you should use an attached service account or Workload Identity Federation instead.
Security controls that limit exposure combine organization policy, least privilege, and careful handling of the service account as a resource. The constraint constraints/iam.automaticIamGrantsForDefaultServiceAccounts prevents default service accounts from automatically receiving the Editor role (roles/editor) on the project. Google Cloud services use service agents, not default service accounts, to act on your behalf, so removing that automatic grant does not break platform operation.
Least privilege starts with single-purpose, user-managed accounts instead of one powerful shared account. Create a dedicated account for each application, avoid the Compute Engine and App Engine default accounts for production, and grant only predefined or custom roles instead of Owner, Editor, or Viewer. Because the account is also a resource, grant the Service Account User role (roles/iam.serviceAccountUser) only to principals that must attach it to resources, and grant the Service Account Admin role (roles/iam.serviceAccountAdmin) only to principals that must view, edit, disable, or delete it. Do not put sensitive terms in the email address, because allow policies that grant cross-project access can be viewed by roles including Viewer. For Compute Engine, set the cloud-platform access scope on the virtual machine (VM) so the attached account can call the APIs its IAM roles allow, and remember that anyone who can log in to the VM can use all permissions of its attached account.
The service account lifecycle covers creation, tracking, disabling, deletion, and safe replacement together with its workload. The service account should be managed with the same process, lifecycle, and tools as its associated VM or application, not in isolation. Use a naming and documentation convention such as a vm-, wlif-, wlifgke-, or onprem- prefix plus the application name, and use the description or display name for owner, purpose, and documentation links.
Unused accounts and keys should be disabled first and deleted only after verification. Service account insights flag accounts that have not authenticated in the past 90 days, Activity Analyzer shows the most recent authentication of an account or key, and Event Threat Detection can alert Security Command Center Premium customers when a dormant account inactive for more than 180 days becomes active. Disable the unused account so it cannot be used for lateral movement or privilege escalation, keep it in a disabled state while you confirm nothing breaks, and only then move it to deleted. Deletion has permanent effects because IAM bindings use an internal unique ID, not the email address. A deleted binding appears with a deleted: prefix and does not transfer to a new account with the same name, the new account is not automatically attached to old resources, and deleted default accounts cannot be recreated. If an account was deleted by mistake, try to undelete it instead of creating a same-named replacement that requires re-granting roles and re-attaching resources.
Create and Configure Service Accounts is the process of creating a non-human workload identity and giving it only the access it needs to operate. A service account (opens in a new tab) is identified by a unique email address and lets an application make authorized API calls as itself. The most common pattern is to attach the account to the resource running the workload, then grant Identity and Access Management (IAM) roles to the account so the workload inherits that access.
A service account is both a principal and a resource in IAM. As a principal, a service account can be granted IAM roles to access resources such as a Cloud Storage bucket or a project. As a resource, a service account can itself be accessed by other principals who can be allowed to impersonate it, attach it to resources, or manage it. Service accounts have no password, cannot be used for browser-based sign-in, and belong to a Google Cloud project rather than to Cloud Identity or Google Workspace.
Permission to create service accounts comes from the Create Service Accounts role (roles/iam.serviceAccountCreator) on the project. An administrator grants this role, although custom roles or other predefined roles can also provide the needed permission. To also grant the new account access to the project, the creator needs the Project IAM Admin role (roles/resourcemanager.projectIamAdmin). IAM basic roles can also create service accounts, but they should not be used in production and are only suitable for development or test environments.
Names, display names, and descriptions are the identity fields assigned to every user-managed service account. The service account ID must be 6 to 30 characters long, contain only lowercase alphanumeric characters and dashes, and cannot be changed after creation. The ID becomes part of the email address in the format SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com, while Google Cloud also assigns a permanent numeric ID that IAM uses internally for role bindings.
The display name is the friendly name shown in the Google Cloud console, and the description documents purpose, contact person, or documentation links. Best practice is to create single-purpose accounts, avoid sensitive terms in the email address, and use a convention such as a vm- prefix plus the application name. After creation, wait 60 seconds or more before using the account because reads are eventually consistent, retry with exponential backoff on errors, and check the Service Account Count quota if a project needs many accounts.
Creating a service account in the Google Cloud console is a guided workflow that collects identity information first and access controls second. The console generates the permanent ID from the display name, but the ID can be edited before creation. The same IAM API fields used by code and the command line are collected through the console form.
Creating a service account with the Google Cloud CLI uses the gcloud iam service-accounts create command with the same ID, display name, and description. Cloud Shell already includes the Google Cloud CLI and sets the current project, while a local shell requires installed credentials and Application Default Credentials. The IAM API and client libraries create the same resource with the same fields for automation.
gcloud iam service-accounts create SERVICE_ACCOUNT_NAME --description="DESCRIPTION" --display-name="DISPLAY_NAME" with the chosen ID, description, and display name.gcloud projects add-iam-policy-binding PROJECT_ID --member="serviceAccount:SERVICE_ACCOUNT_NAME@PROJECT_ID.iam.gserviceaccount.com" --role="ROLE_NAME" to give the new identity access, repeating the command for each additional role.Initial IAM roles define what the service account can do as a principal and who can use it as a resource. Granting a role on the project, such as Storage Object Viewer (roles/storage.objectViewer), lets the service account and applications using it access those resources. Granting Service Account User (roles/iam.serviceAccountUser) lets a principal attach the account to resources such as Compute Engine instances, while Service Account Admin (roles/iam.serviceAccountAdmin) lets a principal view, edit, disable, or delete it.
Secure operational use starts with least privilege and single-purpose accounts rather than sharing one powerful account. Avoid granting Owner, Editor, or Viewer in production and instead grant predefined or custom roles that match only needed resources and operations. Avoid the Compute Engine default service account with the Editor role for new workloads, because changes to a shared account affect every workload that uses it.
Exam tip: A new service account created with the same email as a deleted account does not inherit the old role bindings or resource attachments, because IAM bindings use the internal unique numeric ID, not the email address.
Differentiate Service Account Types and Their Roles means recognizing which accounts you manage and which accounts Google manages. A service account is a special account used by an application or compute workload, such as a Compute Engine (opens in a new tab) instance, rather than by a person. Each service account is identified by a unique email address and has no password, so it cannot be used for browser sign-in. Applications authenticate as the service account to make authorized API calls to resources the account has permission to access.
Google Cloud divides service accounts into user-managed accounts that you control and service agents that Google controls:
| Account type | Created by | Managed by | Use for |
|---|---|---|---|
| Custom user-managed | You in your project | You | Workload identity for a specific app or VM |
| Default | Google automatically when you enable a service | You after creation | Quick start for App Engine or Compute Engine jobs |
| Service agent | Google automatically | Letting a Google service act on your behalf |
A custom user-managed service account is a service account that you create and fully manage in your project. You create the account with the Identity and Access Management (IAM) API, Google Cloud console, or Google Cloud CLI, and you can update, disable, enable, and delete it. The name you choose appears in its email address in the format service-account-name@project-id.iam.gserviceaccount.com.
Custom accounts are often used as identities for workloads. The common pattern is to attach the account to the resource running the application, such as a Compute Engine instance, and then grant IAM roles to the account so the application inherits that access. A single VM can have only one attached service account, but the same account can be attached to multiple VMs. Choose a custom account when you want a single-purpose identity for one application, because sharing one account across apps makes lifecycles, permissions, and audit logs harder to trace.
Default service accounts are user-managed accounts that Google creates automatically when you enable or use certain services. You are responsible for managing default accounts after they are created, just like accounts you create yourself. The two main defaults are the App Engine default account at project-id@appspot.gserviceaccount.com and the Compute Engine default account at project-number-compute@developer.gserviceaccount.com.
A default account lets a service deploy jobs that access other Google Cloud resources without extra setup. For example, the Compute Engine default account is attached by default to VMs created with the console or gcloud CLI, unless you specify a different account or no account. Depending on organization policy, a default account might automatically receive the Editor role (roles/editor) on the project, which is very broad when shared across apps. Best practice is to disable that automatic grant with the iam.automaticIamGrantsForDefaultServiceAccounts organization policy constraint, which is enforced by default for organizations created after May 3, 2024, and replace Editor with less permissive roles.
A service agent is a service account that Google Cloud creates, owns, and manages to let a Google service access your resources on your behalf. Service agents are not created in your projects, so they do not appear in your project service-account list, although you may see them in allow policies, audit logs, and the IAM page. For example, when you use Cloud Run to run a container, a service agent gives the service access to the Pub/Sub topics that trigger the container.
Service agents run internal Google processes and typically have some level of access to your resources. Every project enabled for Compute Engine includes the Google APIs Service Agent at project-number@cloudservices.gserviceaccount.com, which is owned by Google and hidden from the Service Accounts section of the console. You cannot attach a service agent to a Compute Engine instance. Choose service agents when the need is Google-to-Google automation, not your own application code.
Exam tip: Only user-managed accounts, including default accounts, can be attached to a Compute Engine VM; a service agent cannot be attached.
How service accounts work with IAM roles reflects their two directions because they are both a principal and a resource. As a principal, a service account can be granted access to resources, so granting the Storage Object Viewer role (roles/storage.objectViewer) on a Cloud Storage bucket lets any workload authenticated as that account read objects. As a resource, a service account can itself be accessed by other principals, so granting a user the Service Account User role (roles/iam.serviceAccountUser) lets that user attach the account to resources, while the Service Account Admin role (roles/iam.serviceAccountAdmin) allows viewing, editing, disabling, and deleting it.
Access from a workload flows from the attached account to the target resource through IAM. First the application authenticates as the attached service account without embedded keys or user credentials, then IAM checks the roles granted to that account on the target. On Compute Engine, both the IAM roles on the account and the access scopes set on the instance must allow the action before the application can reach the resource. This dual check means a broad scope does not override narrow IAM roles, and a broad IAM role does not override a narrow scope.
User-managed service accounts are accounts you create or that Google creates automatically as default accounts and that you manage for workload identity, while service agents are accounts Google creates, owns and manages to let a Google service act on your behalf. Only user-managed accounts, including default accounts, can be attached to a Compute Engine virtual machine, and a service agent cannot be attached.
Service account keys are downloadable, long-lived private keys and are the last authentication choice to use only when no federated or attached option is available. When code runs in Google Cloud, first attach a service account to the resource and grant only the Identity and Access Management (IAM) roles it needs, and when code runs outside Google Cloud, first configure Workload Identity Federation so the outside workload can use a service account without storing a key.
Permission to create service accounts comes from the Create Service Accounts role (roles/iam.serviceAccountCreator) on the project. To also grant the new account access to the project, the creator needs the Project IAM Admin role (roles/resourcemanager.projectIamAdmin).
Identity and Access Management (IAM) bindings use an internal unique numeric ID, not the email address, so a new account with the same email does not inherit the old role bindings or resource attachments. A deleted binding appears with a deleted prefix and the replacement requires re-granting roles and re-attaching resources.
You need to create a new user-managed service account named app-processor using the Google Cloud CLI. The configuration must satisfy the following operational requirements:
App Processor and a description of Processes incoming storage objects.roles/storage.objectViewer) at the project level in project my-corp-prod.Which sequence of gcloud commands should you execute to accomplish this?
Last updated