An engineer needs to configure a custom service account for an application running on a Google Compute Engine VM instance in project `prod-data-pipeline`. The application requires permission to write objects to Cloud Storage buckets within the project. What is the correct sequence of steps to configure least-privilege access and attach the service account to the VM instance?
- 1Create the custom service account `app-writer` using `gcloud iam service-accounts create app-writer --display-name="App Storage Writer"`.
- 2Grant the Storage Object Admin role to the service account using `gcloud projects add-iam-policy-binding prod-data-pipeline --member="serviceAccount:[email protected]" --role="roles/storage.objectAdmin"`.
- 3Provision the Compute Engine VM instance with the service account attached using `gcloud compute instances create app-vm --service-account="[email protected]" --scopes="cloud-platform"`.
- 4Execute application code inside the VM instance to automatically retrieve short-lived credentials from the metadata server using Application Default Credentials (ADC).
Answer
The correct sequence of steps is: First, create the custom service account (`app-writer`). Second, grant the required IAM role (`roles/storage.objectAdmin`) to the service account email. Third, provision the Compute Engine VM instance with the service account attached and `cloud-platform` scope. Fourth, run application code on the VM to authenticate via Application Default Credentials (ADC).
The correct deployment order follows the GCP resource dependency lifecycle: First, create the service account resource to generate its unique email identifier. Second, grant the required predefined IAM role to the service account identity at the project level. Third, create the Compute Engine VM instance, attaching the custom service account with the `cloud-platform` scope so that IAM controls access. Fourth, launch the workload application which retrieves short-lived access tokens from the instance metadata server via Application Default Credentials.
Step-by-Step Solution
Key Concept
Provisioning and attaching custom service accounts to Compute Engine instances using gcloud CLI and least-privilege IAM bindings.