Skip to content

Instantly share code, notes, and snippets.

@askulkarni2
Created December 6, 2017 19:34
Show Gist options
  • Save askulkarni2/ba5b0be5c2335f919fe7215e4d897f4e to your computer and use it in GitHub Desktop.
Save askulkarni2/ba5b0be5c2335f919fe7215e4d897f4e to your computer and use it in GitHub Desktop.
from __future__ import print_function
import boto3
ec2 = boto3.client('ec2')
ssm = boto3.client('ssm')
tags ={'Key': os.environ['Tagname'], 'Value': os.environ['Tagvalue']}
def find_viable_window ():
#checks if the Tag Key value pair matches with an existing Maintenance Window
wins = ssm.describe_maintenance_windows()['WindowIdentities']
for win in wins:
tgts = ssm.describe_maintenance_window_targets(WindowId=win['WindowId'])['Targets']
for tgt in tgts:
t = tgt['Targets'][0]['Key'].split(':')
v = tgt['Targets'][0]['Values'][0]
if len(t) > 1 and t[0] == 'tag' and t[1] == tags['Key'] and v == tags['Value']:
return True;
return False;
def lambda_handler(event, context):
volume =(event['resources'][0].split('/')[1])
if event['detail']['result'] == 'completed':
attach=ec2.describe_volumes(VolumeIds=[volume])['Volumes'][0]['Attachments']
if attach:
instance = attach[0]['InstanceId']
filter={'key': 'InstanceIds', 'valueSet': [instance]}
info = ssm.describe_instance_information(InstanceInformationFilterList=[filter])['InstanceInformationList']
if info:
ec2.create_tags(Resources=[instance],Tags=[tags])
if not find_viable_window():
print "WARNING: the proposed tags {0} : {1} are not a valid target in any maintenance window \n The changes will not be automatically applied".format(tags['Key'],tags['Value'])
print "{0} Instance {1} has been tagged for maintenance".format(info[0]['PlatformName'], instance)
else:
raise Exception('Instance ' + instance + ' is not managed through SSM')
else:
raise Exception('Volume ' + volume + ' not currently attached to an instance')
else:
print "Change to the Volume {0} is not yet completed; instance will not be tagged for maintenance".format(volume)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment