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

Reading large files in Ruby

I needed to slurp up some very large files into a ruby app recently and noticed some interesting behaviour in IO.foreach method. While it is supposed to read file line by line without loading it up into memory, memory usage is quite... Continue →