Sketch: #6
Update SPOT ip DNS entry
General |
Video List |
Video Latest |
Carousel |
Carousel Recent |
D&D 5e |
Fantasy Grounds |
Insult |
Secrets |
Sketches |
Sensor Readings
I need to work on the formatting of the code block, but the base is there for now.
#!/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()