Skip to content

Instantly share code, notes, and snippets.

@robbiet480
Last active October 23, 2019 18:13
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save robbiet480/a87fe549883f2c1138a5 to your computer and use it in GitHub Desktop.
A consul-template plugin to get EC2 metadata

ec2-consul-template-plugin

About

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.

Usage

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.

Examples

Raw JSON (not very useful):

{{ plugin "ec2-consul-template-plugin.py" }}

Loop over the full JSON to get a specific key:

{{ with $d := plugin "ec2-consul-template-plugin.py" | parseJSON }}{{$d.placement.region}}{{end}}

Get a single key direct from the script:

{{ plugin "ec2-consul-template-plugin.py" "placement/region" }}

Get multiple keys direct from the script:

{{ with $d := plugin "ec2-consul-template-plugin.py" "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
try:
for x in path.strip("/").split("/"):
elem = elem.get(x)
except:
pass
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])
else:
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)
else:
if isinstance(xpath_get(metadata, sys.argv[1]), basestring):
print xpath_get(metadata, sys.argv[1])
else:
print json.dumps(xpath_get(metadata, sys.argv[1]))
else:
print json.dumps(metadata)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment