Skip to content

Instantly share code, notes, and snippets.

@rohan-molloy
Last active March 3, 2018 12:37
Show Gist options
  • Save rohan-molloy/581b976f87081bd7f438a6da9512e42a to your computer and use it in GitHub Desktop.
Save rohan-molloy/581b976f87081bd7f438a6da9512e42a to your computer and use it in GitHub Desktop.
Instance Metadata

Cloud metadata

Many cloud providers such as Amazon AWS, Digitalocean and Vultr provide an instance metadata service.

Guest instances can discover their own parameters by accessing an API. By convention, this API is accessible from the address 169.254.169.254

To get the value of key 'k', we send an HTTP request to the relative location /$k If the key corresponds to a tuple, the request is rewritten to key/ which is an index. If no value or tuple exists, it simply returns not found

THe entire metadata collection can also be pulled as a single JSON

curl -sL http://169.254.169.254/metadata/v1/
<a href="/metadata/v1/">Moved Permanently</a>.

Using curl (Recommended for general use)

export metadata_cmd=\
'curl -sL http://169.254.169.254/metadata/v1/json'

Using a raw socket : (Recommended for bootstrapping)

export metadata_cmd=\
'exec 3<>/dev/tcp/169.254.169.254/80;printf "GET /metadata/v1.json HTTP/1.1\r\nHost: 169.254.169.254\r\nUser-Agent: curl/7.47.0\r\nAccept: */*\r\n\r\n" >&3; grep ^{ <&3'

Pretty printing and retrieving data

$ jq -r "dns" <(bash -c "$metadata_cmd")
{
"nameservers": [
 	"2001:4860:4860::8844",
 	"2001:4860:4860::8888",
 	"8.8.8.8"
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment