Created
September 5, 2017 20:27
-
-
Save chrisguitarguy/e9cb271f6ac882627d0d61efe03dc8ae 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
#!/usr/bin/env python3 | |
import argparse | |
import http.client as http | |
import os | |
import boto3 as aws | |
def _parse_args(args=None): | |
p = argparse.ArgumentParser(description='Update a hostname record in route53 with the current IP address') | |
p.add_argument('zone_id', help='The DNS zone id to update') | |
p.add_argument('hostname', help='The DNS name to update') | |
return p.parse_args(args) | |
def _get_local_ipv4(): | |
conn = http.HTTPConnection('169.254.169.254', 80) | |
conn.request('GET', '/latest/meta-data/local-ipv4') | |
ipr = conn.getresponse() | |
return ipr.read().decode('utf-8') | |
def _update_dns(ip, zone_id, hostname): | |
dns = aws.client('route53') | |
dns.change_resource_record_sets( | |
HostedZoneId=zone_id, | |
ChangeBatch={ | |
'Comment': 'Update {} record from ASG'.format(args.hostname), | |
'Changes': [{ | |
'Action': 'UPSERT', | |
'ResourceRecordSet': { | |
'Name': hostname, | |
'Type': 'A', | |
'TTL': 60, | |
'ResourceRecords': [{ | |
'Value': ip | |
}], | |
}, | |
}], | |
}, | |
) | |
def main(args=None): | |
args = _parse_args(args) | |
ip = _get_local_ipv4() | |
_update_dns(ip, args.zone_id, args.hostname) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment