Created
August 4, 2016 21:39
-
-
Save emalloy/0473150f654a884f02d476632c429816 to your computer and use it in GitHub Desktop.
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
from __future__ import print_function | |
import boto3 | |
route53 = boto3.client('route53') | |
ec2 = boto3.resource('ec2') | |
def lambda_handler(event, context): | |
instance_id = event['detail']['instance-id'] | |
state = event['detail']['state'] | |
print('Received notification of event ' + state +' for instance ' + instance_id) | |
if state not in ['shutting-down', 'stopping', 'stopped', 'terminated']: | |
print('Aborting- instance state: ' + state + '.') | |
exit() | |
instance = ec2.Instance(instance_id) | |
if instance is None: | |
print('Instance not found: ' + instance_id) | |
exit() | |
try: | |
for t in instance.tags: | |
if t['Key'] == 'Name': | |
host_name = t['Value'] | |
if t['Key'] == 'Environment': | |
environment = t['Value'] | |
except BaseException as e: | |
print('Could not retrieve tags from ' + instance_id) | |
exit() | |
if environment is None or host_name is None: | |
print('Aborting- instance does not have an Environment tag : ' + instance_id) | |
exit() | |
if '.' not in host_name: | |
print('Aborting- invalid hostname: ' + host_name) | |
exit() | |
host_role = host_name.split('-', 1)[0] | |
host_domain = host_name.split('.', 1)[1] | |
if host_role == 'master': | |
private_dns_name = host_role + '-a' + '.' + host_domain | |
else: | |
private_dns_name = host_role + '-' + instance_id + '-a' + '.' + host_domain | |
zone_id = get_zone_id(environment + '.kube.') | |
if zone_id is None: | |
print('Hosted zone not found: ' + environment + '.kube.') | |
exit() | |
rrsets = route53.list_resource_record_sets( | |
HostedZoneId=zone_id, | |
StartRecordName=private_dns_name) | |
record_found = False | |
for r in rrsets['ResourceRecordSets']: | |
if r['Name'] == private_dns_name + '.': | |
print('Found existing record.') | |
record_found = True | |
private_ip = r['ResourceRecords'][0]['Value'] | |
if record_found is False: | |
print('Aborting- instance ' + instance_id + ' does not appear to have an existing record in ' + zone_id) | |
exit() | |
delete_resource_record(zone_id, private_dns_name, 'A', private_ip) | |
def delete_resource_record(zone_id, host_name, type, private_ip): | |
"""This function deletes resource records from the hosted zone passed by the calling function.""" | |
print('Deleting ' + type + ' record ' + host_name + 'value: ' + private_ip + ' in zone ' + zone_id) | |
if host_name[-1] != '.': | |
host_name = host_name + '.' | |
try: | |
route53.change_resource_record_sets( | |
HostedZoneId=zone_id, | |
ChangeBatch={ | |
"Comment": "Updated by Lambda host-route53-shutdown", | |
"Changes": [ | |
{ | |
"Action": "DELETE", | |
"ResourceRecordSet": { | |
"Name": host_name, | |
"Type": type, | |
"TTL": 60, | |
"ResourceRecords": [ | |
{ | |
"Value": private_ip | |
}, | |
] | |
} | |
}, | |
] | |
} | |
) | |
except BaseException as e: | |
print(e) | |
def get_zone_id(zone_name): | |
"""This function returns the zone id for the zone name that's passed into the function.""" | |
if zone_name[-1] != '.': | |
zone_name = zone_name + '.' | |
hosted_zones = route53.list_hosted_zones() | |
x = filter(lambda record: record['Name'] == zone_name, hosted_zones['HostedZones']) | |
try: | |
zone_id_long = x[0]['Id'] | |
zone_id = str.split(str(zone_id_long),'/')[2] | |
return zone_id | |
except: | |
return None |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment