Skip to content

Instantly share code, notes, and snippets.

@vindir
Forked from emilisto/ec2_tags.py
Created February 17, 2017 16:11
Show Gist options
  • Save vindir/6858fddcc409f300fed689cf258444fb to your computer and use it in GitHub Desktop.
Save vindir/6858fddcc409f300fed689cf258444fb to your computer and use it in GitHub Desktop.
Grain for Salt that exposes all EC2 instance tags in grains['tags'], free for all to use and distribute. Tweet me @svammel if you have questions.
"""
ec2_tags - Exports all EC2 tags in a 'tags' grain
For Salt (http://saltstack.org)
Author: Emil Stenqvist <[email protected]>
Usage:
1. Put ec2_tags.py in roots/_grains/
2. Add the following to your salt-minion and/or salt-master config:
ec2_tags:
aws:
access_key: <your AWS access key with read-permission on EC2>
secret_key: <secrey key>
region: <region>
3. Try it out:
$ salt '*' saltutil.sync_grains
$ salt '*' grains.get tags
(Inspired by https://github.com/dginther/ec2-tags-salt-grain)
"""
import boto.ec2
import boto.utils
import logging
import salt.log
log = logging.getLogger(__name__)
def _get_instance_id():
return boto.utils.get_instance_metadata()['instance-id']
def ec2_tags():
try:
aws = __opts__['ec2_tags']['aws']
REGION = aws['region']
AWS_ACCESS_KEY=aws['access_key']
AWS_SECRET_KEY=aws['secret_key']
except KeyError:
log.warning("ec2_tags: aws configuration required in minion and/or minion config for grain to work")
return None
# Connect to EC2 and parse the Roles tags for this instance
conn = boto.ec2.connect_to_region(REGION,
aws_access_key_id=AWS_ACCESS_KEY,
aws_secret_access_key=AWS_SECRET_KEY)
instance_id = _get_instance_id()
tags = {}
try:
reservation = conn.get_all_instances(instance_ids=[ instance_id ])[0]
instance = reservation.instances[0]
tags = instance.tags
except IndexError, e:
log.error("Couldn't find information about current instance: %s", e)
return None
return { 'tags': tags }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment