Last active
March 5, 2017 02:20
-
-
Save takenoko-str/61ed44bcd8b5cc4c15904e0be83ad1ef to your computer and use it in GitHub Desktop.
AWS Lambda function to sync tag name between ebs volumes and the attached instance.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import json | |
import sys | |
import botocore.exceptions | |
client = boto3.client('ec2') | |
ec2 = boto3.resource('ec2') | |
def get_instance_volume_list(client, data={}, filters=[]): | |
response = client.describe_volumes(Filters=filters) | |
# EBSボリュームが一つもないという状態は仮定していない。 | |
volumes = response.get(u'Volumes') | |
for vol in volumes: | |
attachments = vol.get(u'Attachments') | |
# インスタンスが紐づいていないボリュームには名づけようがない。 | |
if attachments == []: | |
continue | |
instanceid = attachments[0][u'InstanceId'] | |
volumeid = attachments[0][u'VolumeId'] | |
tags = vol.get(u'Tags') | |
if not tags:# Tagsがそもそもない場合。インスタンスのtag:Nameを名前とする。 | |
data = set_volumeid(data, instanceid, volumeid) | |
data = get_hostname(client, data, instanceid) | |
continue | |
for tag in tags:# {'Key': u'Name', 'Value': u''}の場合。インスタンスのtag:Nameを名前とする。 | |
if tag.get(u'Key') == u'Name' and tag.get(u'Value') == u'': | |
data = set_volumeid(data, instanceid, volumeid) | |
data = get_hostname(client, data, instanceid) | |
break | |
return data | |
def set_volumeid(data, instanceid, volumeid): | |
data.setdefault(instanceid, []).append({}) | |
data[instanceid][0].setdefault('volumeid', []).append(volumeid) | |
return data | |
def get_hostname(client, data, instanceid): | |
data.setdefault(instanceid, []).append({}) | |
response = client.describe_instances( | |
InstanceIds = [instanceid] | |
) | |
reservations = response.get(u'Reservations', [{}])[0] | |
instances = reservations.get(u'Instances', [{}])[0] | |
for tag in instances.get(u'Tags', [{}]): | |
if u'Name' in tag[u'Key']: | |
data[instanceid][0].setdefault('hostname', []).append(tag[u'Value']) | |
return data | |
def create_ebs_tag(ec2, data={}, result={}, dryrun=True): | |
for instanceid, ebsinfo in data.iteritems(): | |
volumeids = ebsinfo[0]["volumeid"] | |
hostname = ebsinfo[0]["hostname"][0] | |
for vid in volumeids: | |
ebsvolume = ec2.Volume(vid) | |
try: | |
response = ebsvolume.create_tags( | |
DryRun=dryrun, | |
Tags=[{ | |
'Key': 'Name', | |
'Value': '{}'.format(hostname) | |
}] | |
) | |
result.setdefault(vid, []).append(response) | |
except botocore.exceptions.ClientError as e: | |
if e.response['Error'].get('Code') != 'DryRunOperation': | |
print "failure" | |
return result | |
def lambda_handler(event, context): | |
filters = [{'Name': 'status','Values': ['in-use']}] | |
volume_data = get_instance_volume_list(client, filters) | |
result = create_ebs_tag(ec2, volume_data) | |
return json.dumps(dict(get_intance_volume_list=volume_data, create_ebs_tag=str(result))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment