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 "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).