Automating Compute Engine initialization is foundational for scalable cloud infrastructure. While passing an inline startup script directly via VM metadata works for quick experiments, externalizing bootstrap scripts to Google Cloud Storage is the enterprise standard. It decouples configuration updates from immutable Instance Templates, enforces strict IAM boundaries via dedicated Service Accounts, and integrates cleanly with Shielded VMs.
Remote Script Execution & Least-Privilege IAM Architecture
Step 1: Setting up the Environment & Dedicated Service Account
Using the default Compute Engine service account (`PROJECT_NUMBER-compute@developer.gserviceaccount.com`) with legacy OAuth scopes is a security anti-pattern because it often inherits broad `Editor` permissions. Instead, we create a dedicated, least-privilege IAM Service Account scoped strictly to reading our bootstrap bucket and writing Cloud Logging/Monitoring telemetry:
export PROJECT_ID=$(gcloud config get-value project)
export REGION="us-central1"
export ZONE="us-central1-a"
export BUCKET_NAME="gs://${PROJECT_ID}-startup-scripts"
export SA_NAME="gce-web-bootstrap-sa"
export SA_EMAIL="${SA_NAME}@${PROJECT_ID}.iam.gserviceaccount.com"
gcloud config set compute/region $REGION
gcloud config set compute/zone $ZONE
# Create dedicated least-privilege Service Account
gcloud iam service-accounts create $SA_NAME \
--display-name="GCE Bootstrap Least-Privilege SA"
for ROLE in roles/logging.logWriter roles/monitoring.metricWriter; do
gcloud projects add-iam-policy-binding $PROJECT_ID \
--member="serviceAccount:${SA_EMAIL}" \
--role="$ROLE"
done
Step 2: Provisioning the Hardened Cloud Storage Bucket
We use gcloud storage buckets create with Uniform Bucket-Level Access and Public Access Prevention enforced, then grant roles/storage.objectViewer exclusively to our VM's service account:
gcloud storage buckets create $BUCKET_NAME \
--location=$REGION \
--uniform-bucket-level-access \
--public-access-prevention
gcloud storage buckets add-iam-policy-binding $BUCKET_NAME \
--member="serviceAccount:${SA_EMAIL}" \
--role="roles/storage.objectViewer"
Step 3: Writing an Idempotent Startup Script with Ops Agent
Startup scripts execute on every system boot. To prevent package re-installation or service downtime during routine maintenance reboots, scripts must be strictly idempotent:
Idempotent Execution Flow
cat << 'EOF' > install-web-optimized.sh
#!/bin/bash
set -euo pipefail
if ! command -v apache2 &> /dev/null; then
echo "Apache not found. Running initial bootstrap..."
apt-get update
apt-get install -y apache2
cat << 'INDEX' > /var/www/html/index.html
<!doctype html>
<html><body>
<h1>Hello from a Shielded Debian 12 GCE Instance!</h1>
<p>Provisioned via idempotent remote startup script.</p>
</body></html>
INDEX
systemctl enable --now apache2
else
echo "Apache is already installed. Skipping initialization."
fi
EOF
gcloud storage cp install-web-optimized.sh $BUCKET_NAME/install-web-optimized.sh
Step 4: Launching a Shielded Debian 12 VM Instance
We provision our Compute Engine VM using the modern Debian 12 (Bookworm) image family, attach our dedicated service account with --scopes=cloud-platform (letting IAM govern permissions rather than legacy access scopes), and enable Shielded VM Secure Boot & vTPM integrity monitoring:
gcloud compute instances create web-server-vm \
--machine-type=e2-micro \
--image-family=debian-12 \
--image-project=debian-cloud \
--service-account=$SA_EMAIL \
--scopes=cloud-platform \
--shielded-secure-boot \
--shielded-vtpm \
--shielded-integrity-monitoring \
--tags=http-server \
--metadata=startup-script-url=${BUCKET_NAME}/install-web-optimized.sh
Step 5: Configuring VPC Firewall Rules & Inspecting Serial Logs
Finally, we allow ingress TCP port 80 traffic to instances tagged http-server and verify execution via the guest serial port output:
gcloud compute firewall-rules create default-allow-http \
--direction=INGRESS \
--priority=1000 \
--network=default \
--action=ALLOW \
--rules=tcp:80 \
--source-ranges=0.0.0.0/0 \
--target-tags=http-server
# Audit startup script execution directly from the VM serial port
gcloud compute instances get-serial-port-output web-server-vm --zone=$ZONE | grep -i "startup-script"
Summary
Externalizing startup scripts to Cloud Storage while enforcing idempotency, dedicated IAM Service Accounts, and Shielded VM integrity checks provides a clean, auditable foundation for Google Compute Engine automation at scale.