Question

Difficulty: MediumCreating and Managing Service Accounts

A cloud engineer needs to configure a Compute Engine virtual machine to securely export application logs to Cloud Logging using a dedicated service account and least privilege access. What is the correct sequence of steps to establish this service account authentication flow?

  1. 1Create a new user-managed service account in the project using `gcloud iam service-accounts create`.
  2. 2Grant the `roles/logging.logWriter` role to the newly created service account using `gcloud projects add-iam-policy-binding`.
  3. 3Attach the service account to the Compute Engine instance using `gcloud compute instances set-service-account`.
  4. 4Execute the application using Google Cloud Client Libraries relying on Application Default Credentials (ADC) to automatically acquire identity tokens.

Answer

The correct operational sequence begins by creating the user-managed service account identity, binding the required least-privilege IAM role (`roles/logging.logWriter`) to that identity at the project level, attaching the service account to the target Compute Engine VM instance, and finally configuring application code to authenticate using Application Default Credentials (ADC).
Establishing a service account workflow follows a strict lifecycle: first, the identity must be created in IAM. Next, access permissions (the predefined `roles/logging.logWriter` role) are bound to the service account. After authorization is configured, the identity is attached to the virtual machine instance so the metadata server can issue tokens. Finally, the application consumes these tokens seamlessly via Application Default Credentials without downloading explicit key files.

Step-by-Step Solution

1
Create the Service Account
A unique service account identity email is generated within the Google Cloud project.
An identity must exist before IAM policy bindings or resource attachments can reference it.
2
Delegate IAM Roles
The service account is granted `roles/logging.logWriter` on the target project resource.
Least-privilege authorization must be established so the identity has permission to write logs when authenticated.
3
Attach to Compute Engine Instance
The VM instance metadata server is configured to provide access tokens for the dedicated service account.
Attaching the service account allows workloads running on the virtual machine to inherit its identity securely.
4
Leverage Application Default Credentials
Application code automatically retrieves short-lived OAuth 2.0 access tokens from the local instance metadata server.
Using ADC avoids exporting and managing persistent service account JSON keys.

Key Concept

Creating, granting permissions to, attaching, and consuming user-managed service accounts using Application Default Credentials on Compute Engine.
Rate this question