Question

Difficulty: HardDeploying and Managing Compute Engine Virtual Machines

A system administrator is writing a deployment script using the Google Cloud CLI (`gcloud`) to launch non-critical stateless batch processing Virtual Machine instances on Compute Engine. The VM instances must execute a bash initialization script located on the local disk at `./scripts/init.sh` upon first boot and execute using a dedicated custom IAM service account named `[email protected]`. Which TWO `gcloud compute instances create` flags must be used to meet these requirements?

  1. --service-account=batch-runner@my-project.iam.gserviceaccount.comAnswer
  2. --metadata-from-file=startup-script=./scripts/init.shAnswer
  3. C
  4. D
    --metadata=startup-script=./scripts/init.sh

Answer

The deployment command must include `--service-account=batch-runner@my-project.iam.gserviceaccount.com` to attach the custom IAM service account identity and `--metadata-from-file=startup-script=./scripts/init.sh` to read and load the local shell script into the VM metadata startup key.
To provision a Compute Engine instance attached to a custom service account and execute a local initialization script on startup, `gcloud compute instances create` requires two specific flags: `--service-account` to define the IAM identity, and `--metadata-from-file` with the `startup-script` key to read the local script contents into the VM metadata.

Step-by-Step Solution

1
Identify the flag required to attach an IAM service account identity to a Compute Engine VM.
The `gcloud compute instances create` command uses `--service-account=[SA_EMAIL]` to bind a specific service account identity.
By default VMs receive the Compute Engine default service account unless explicit identity configuration is specified.
2
Identify the flag required to upload local file content as startup script metadata.
The `--metadata-from-file=startup-script=[FILE_PATH]` flag reads local files and attaches their contents to instance metadata.
Using literal `--metadata` only stores the file path string itself, whereas `--metadata-from-file` ingests the file contents.

Key Concept

Compute Engine gcloud CLI flags for identity binding and startup script configuration
Rate this question