A cloud engineer needs to update an existing running Compute Engine virtual machine instance named `app-worker-1` to use a custom IAM service account named `[email protected]` instead of the default Compute Engine service account. The configuration update must be completed using the Google Cloud CLI (`gcloud`) without deleting the instance or losing disk state. Which sequence of actions should the cloud engineer execute?
- Stop instance `app-worker-1`, run `gcloud compute instances set-service-account app-worker-1 [email protected] --scopes=cloud-platform`, and start instance `app-worker-1`.Answer
- BRun `gcloud compute instances update app-worker-1 [email protected]` directly while the instance is running.
- CRun `gcloud compute instances add-metadata app-worker-1 --metadata=service-account=app-sa@prod-proj.iam.gserviceaccount.com` while the instance is running.
- DStop instance `app-worker-1`, run `gcloud compute instances set-scopes app-worker-1 --roles=roles/editor`, and start instance `app-worker-1`.
Answer
Stop instance `app-worker-1`, run `gcloud compute instances set-service-account app-worker-1 [email protected] --scopes=cloud-platform`, and start instance `app-worker-1`.
To modify the IAM service account attached to an existing Compute Engine VM instance, the instance must first be stopped. Once stopped, running `gcloud compute instances set-service-account` with the `--service-account` flag successfully updates the attached service account. Restarting the instance completes the deployment without losing persistent data.
Step-by-Step Solution
Key Concept
Modifying Compute Engine VM Service Account Identity