Skip to content

Instantly share code, notes, and snippets.

Forked from robbiet480/
Created August 28, 2019 18:08
Show Gist options
  • Save marceloalmeida/02cff247f282d2eefb65c5848fc90ed5 to your computer and use it in GitHub Desktop.
Save marceloalmeida/02cff247f282d2eefb65c5848fc90ed5 to your computer and use it in GitHub Desktop.
A consul-template plugin to get EC2 metadata



This is a simple little Python script to let you query EC2 metadata from consul-template. It's only requirement is boto. It uses the EC2 internal metadata service so it does not require any API keys or even a region. The only caveat is that this can only be run on a machine on EC2.


You can give no arguments for full dictionary output or one or more arguments to get specific key(s). Put it somewhere on your machine, chmod +x it and give the full path to consul-template.


Raw JSON (not very useful):

{{ plugin "" }}

Loop over the full JSON to get a specific key:

{{ with $d := plugin "" | parseJSON }}{{$d.placement.region}}{{end}}

Get a single key direct from the script:

{{ plugin "" "placement/region" }}

Get multiple keys direct from the script:

{{ with $d := plugin "" "local_ipv4" "hostname" | parseJSON }}{{$d.local_ipv4}} {{$d.hostname}}{{end}}

It supports xpath like queries. No array indexes or anything, just / (which seems to be in line with consul-template's existing functions).

For compatibility with consul-template, all dictionary keys have - replaced with _ i.e. local-ipv4 becomes local_ipv4. There are some deeply nested keys which aren't replaced (like in network).

#!/usr/bin/env python
import sys
import json
import boto.utils
def xpath_get(mydict, path):
elem = mydict
for x in path.strip("/").split("/"):
elem = elem.get(x)
return elem
def mangle_keys(obj):
for key in obj.keys():
new_key = key.replace("-","_")
if new_key != key:
if isinstance(obj[key], dict):
obj[new_key] = mangle_keys(obj[key])
obj[new_key] = obj[key]
del obj[key]
return obj
metadata = boto.utils.get_instance_metadata()
metadata['user_data'] = boto.utils.get_instance_userdata()
metadata['identity'] = boto.utils.get_instance_identity()
metadata['placement']['region'] = metadata['identity']['document']['region']
metadata = mangle_keys(metadata)
if(len(sys.argv) > 1):
if(len(sys.argv) > 2):
output = {}
for arg in sys.argv[1:]:
output[arg] = xpath_get(metadata, arg)
print json.dumps(output)
if isinstance(xpath_get(metadata, sys.argv[1]), basestring):
print xpath_get(metadata, sys.argv[1])
print json.dumps(xpath_get(metadata, sys.argv[1]))
print json.dumps(metadata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment