Question

Difficulty: MediumDeploying and Managing Compute Engine Virtual Machines

A cloud engineer is deploying a Compute Engine virtual machine instance named `web-app-frontend` in zone `us-east1-b`. The engineer has written a setup shell script stored on their local workstation at `./scripts/install-web.sh` that must execute automatically whenever the instance boots up for the first time. Which `gcloud` command correctly creates the virtual machine instance and passes the local script file as a startup script?

  1. gcloud compute instances create web-app-frontend --zone=us-east1-b --metadata-from-file=startup-script=./scripts/install-web.shAnswer
  2. B
    gcloud compute instances create web-app-frontend --zone=us-east1-b --metadata=startup-script=./scripts/install-web.sh
  3. C
    gcloud compute instances create web-app-frontend --zone=us-east1-b --startup-script=./scripts/install-web.sh
  4. D
    gcloud compute instances create web-app-frontend --region=us-east1 --metadata-from-file=startup-script=./scripts/install-web.sh

Answer

The command using `gcloud compute instances create web-app-frontend --zone=us-east1-b --metadata-from-file=startup-script=./scripts/install-web.sh` is correct.
To pass the contents of a local file as custom instance metadata (such as a startup script), Google Cloud CLI requires the `--metadata-from-file` flag with the key-value pair `startup-script=<path-to-file>`. Additionally, Compute Engine instances are zonal resources and must specify `--zone`.

Step-by-Step Solution

1
Identify the resource scope required for VM creation.
Compute Engine instances are zonal resources requiring `--zone=us-east1-b`.
Passing `--region` instead of `--zone` causes a flag syntax error.
2
Determine how local file contents are passed to Compute Engine instance metadata.
The `gcloud` CLI uses the `--metadata-from-file` flag to read local files.
The standard `--metadata` flag only accepts direct key-value literal string inputs, while `--metadata-from-file` reads the local file contents into the specified metadata key.
3
Specify the exact metadata key for startup scripts.
The key must be `startup-script` passed as `--metadata-from-file=startup-script=./scripts/install-web.sh`.
This guarantees that Compute Engine's guest environment runs the script content during instance boot.

Key Concept

Compute Engine startup script configuration using gcloud metadata flags
Rate this question