Skip to content

Instantly share code, notes, and snippets.

@chrisguitarguy
Created September 5, 2017 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisguitarguy/e9cb271f6ac882627d0d61efe03dc8ae to your computer and use it in GitHub Desktop.
Save chrisguitarguy/e9cb271f6ac882627d0d61efe03dc8ae to your computer and use it in GitHub Desktop.
#!/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