A GCP project has three service accounts. A developer wants to list all service accounts in the project using the gcloud CLI. Which command is correct?
The command `gcloud iam service-accounts list` is the correct and canonical way to list all service accounts in the active project. It returns a table of service account emails, display names, and unique IDs by querying the IAM Service Accounts API. You can specify a non-default project with `--project` or `--filter` to narrow results, making it a reliable and standard administrative operation.
Why this answer
The `gcloud iam service-accounts list` command is the standard gcloud CLI command to list all service accounts in a GCP project. It uses the IAM API to retrieve the service accounts associated with the current project (or a specified project via the `--project` flag). This command is part of the `gcloud iam` group, which manages IAM resources, and the `service-accounts` sub-group specifically handles service account operations.
Exam trap
Google Cloud often tests the exact hierarchical structure of gcloud commands, and the trap here is that candidates may confuse the `gcloud iam` subcommand syntax (where the resource type comes before the verb) with other command groups (like `gcloud compute` where the verb often comes first), leading them to choose Option B or D.
How to eliminate wrong answers
Option A is wrong because `gcloud service-accounts list` is not a valid gcloud command; the correct command structure requires the `iam` group before `service-accounts`. Option B is wrong because `gcloud iam list service-accounts` uses an incorrect subcommand order — the verb `list` must come after the resource type `service-accounts`, not before. Option D is wrong because `gcloud projects list-service-accounts` does not exist; the `gcloud projects` command group is for managing project metadata, not for listing service accounts.