Retrieving GCE metadata map

GCE metadata can be a useful tool for getting the information about the running instance.

One needs to know exactly where to look for a particular key however. For that, the metadata actually supports a ?recursive=true parameter, returning a json of all the data available.

Observe:

# Quick helper function to pretty print JSON 
$ alias to_j="ruby -e \"require 'json';puts JSON.pretty_generate(JSON.parse(STDIN.read))\""

$ curl -s http://metadata/computeMetadata/v1/instance/?recursive=true -H "Metadata-Flavor: Google" |  to_j
{
  "attributes": {
  },
  "cpuPlatform": "Intel Broadwell",
  "description": "",
  "disks": [
    {
      "deviceName": "persistent-disk-0",
      "index": 0,
      "mode": "READ_WRITE",
      "type": "PERSISTENT-SSD"
    }
  ],
  "hostname": "vagrant-test.c.graphite-sandbox.internal",
  "id": 77747111415725975,
  "image": "projects/ubuntu-os-cloud/global/images/ubuntu-1804-bionic-v20180617",
  "licenses": [
    {
      "id": "7777592092274602096"
    }
  ],
  "machineType": "projects/1086402177777/machineTypes/n1-standard-1",
  "maintenanceEvent": "NONE",
  "name": "vagrant-elastic",
  "networkInterfaces": [
    {
      "accessConfigs": [
        {
          "externalIp": "35.189.77.77",
          "type": "ONE_TO_ONE_NAT"
        }
      ],
      "dnsServers": [
        "169.254.169.254"
      ],
      "forwardedIps": [

      ],
      "gateway": "10.7.0.1",
      "ip": "10.7.0.7",
      "ipAliases": [

      ],
      "mac": "42:77:0a:98:77:77",
      "network": "projects/1086402177777/networks/default",
      "subnetmask": "255.255.240.0",
      "targetInstanceIps": [

      ]
    }
  ],
  "preempted": "FALSE",
  "remainingCpuTime": -1,
  "scheduling": {
    "automaticRestart": "TRUE",
    "onHostMaintenance": "MIGRATE",
    "preemptible": "FALSE"
  },
  "serviceAccounts": {
  },
  "tags": [

  ],
  "virtualClock": {
    "driftToken": "0"
  },
  "zone": "projects/1086402177777/zones/australia-southeast1-c"
}
 
1
Kudos
 
1
Kudos

Now read this

What’s making me happy 18-06-2017

Visiting NorCal and Seattle area over the past 2 weeks, it’s been nice to visit some of my old friends. Those moments, although brief do warm my heart. Took some time walking around Stanford. The Dish trail turned out to be a great walk.... Continue →