The GDELT Project

GCP Tips & Tricks: The Compute Engine Metadata Server

The GCP Compute Engine (GCE) metadata server is an extremely powerful and often underappreciated resource. Yesterday we examined how it can be used to obtain a bearer token in a fraction of the time of gcloud – so fast in fact that it allows GCS to be used as a form of simple globally-distributed queuing system.

Here are a few of the metadata values we've found the most useful (see the full list in the GCE documentation). You can wrap these up in shell scripts to make simple analysis scripts fully portable across GCP projects and fleets. Note that these only work on GCE VMs themselves.

Project-Level Values

#GCP Project ID
curl -s "http://metadata.google.internal/computeMetadata/v1/project/project-id" -H "Metadata-Flavor: Google" 

#Numeric GCP Project ID (required by some API calls)
curl -s "http://metadata.google.internal/computeMetadata/v1/project/numeric-project-id" -H "Metadata-Flavor: Google"

Instance-Level Values

#Obtain a bearer token
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token" -H "Metadata-Flavor: Google" | jq -r '.access_token'

#The fully-qualified instance name (including region, zone and project ID depending on your project settings)
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/hostname" -H "Metadata-Flavor: Google" 

#Simple hostname
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/name" -H "Metadata-Flavor: Google" 

#Zone instance runs in
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/zone" -H "Metadata-Flavor: Google" 

#Disk image (for prebuilt images provides the OS, version and build date)
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/image" -H "Metadata-Flavor: Google" 

#Machine type
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/machine-type" -H "Metadata-Flavor: Google" 

#CPU platform
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/cpu-platform" -H "Metadata-Flavor: Google" 

#Internal IP
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/ip" -H "Metadata-Flavor: Google" 

#External IP (for instances with external connectivity)
curl -s "http://metadata.google.internal/computeMetadata/v1/instance/network-interfaces/0/access-configs/0/external-ip" -H "Metadata-Flavor: Google"