A system administrator needs to provision a Compute Engine virtual machine instance using the Google Cloud CLI (`gcloud`). The VM must execute a local multi-line initialization script on startup and perform read-only operations against a Cloud Storage bucket using a custom service account following the principle of least privilege. In what chronological sequence must the administrator perform the steps to configure and launch this virtual machine?
- 1Create a dedicated user-managed service account using `gcloud iam service-accounts create`.
- 2Grant the `roles/storage.objectViewer` IAM role to the newly created service account for the specific Cloud Storage bucket.
- 3Write and save the environment setup commands into a local executable script file (e.g., `init.sh`) on the administrator's local machine.
- 4Execute `gcloud compute instances create` including `--service-account`, `--scopes=https://www.googleapis.com/auth/cloud-platform`, and `--metadata-from-file=startup-script=init.sh`.
Answer
The correct operational sequence is: 1) Create the dedicated user-managed service account, 2) Grant the Storage Object Viewer role to the service account, 3) Write and save the initialization shell script locally, and 4) Run `gcloud compute instances create` referencing the service account, `cloud-platform` scope, and `--metadata-from-file=startup-script=init.sh`.
Provisioning a secure Compute Engine instance follows a logical dependency chain: identity creation, permission assignment, local asset preparation, and finally resource instantiation. The service account must exist before IAM roles can be granted to it. The IAM permissions must be bound before the VM boots and attempts to perform storage actions. The startup script file must be saved on the administrator's client machine before `gcloud` can read its contents via `--metadata-from-file`. Finally, launching the instance attaches the service account identity and uploads the local script content into instance metadata.
Step-by-Step Solution
Key Concept
Compute Engine VM Provisioning with Custom Service Accounts and Metadata Startup Scripts