Skip to content

Instantly share code, notes, and snippets.

@Dimtree
Created August 23, 2015 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Dimtree/b0bf41823ad1a5107e12 to your computer and use it in GitHub Desktop.
Save Dimtree/b0bf41823ad1a5107e12 to your computer and use it in GitHub Desktop.
Updates a Route53 subdomain to a given IP address, designed for dynamic IPs
#!/usr/bin/python
import boto
from boto.route53.record import ResourceRecordSets
import urllib2
AWS_ACCESS_KEY_ID = 'access_key_here'
AWS_SECRET_ACCESS_KEY = 'secret_key_here'
AWS_R53_ZONE = 'zone_id_here'
AWS_R53_ADDR = 'subdomain.example.com'
ip = urllib2.urlopen('https://icanhazip.com').read().rstrip()
r53 = boto.route53.connect_to_region('universal',
aws_access_key_id=AWS_ACCESS_KEY_ID,
aws_secret_access_key=AWS_SECRET_ACCESS_KEY)
records = r53.get_all_rrsets(AWS_R53_ZONE,'A',AWS_R53_ADDR,maxitems=1)[0]
oldip = records.resource_records[0]
if ip in oldip:
print "%s exists for %s." % (ip, AWS_R53_ADDR)
else:
print "%s does NOT exist in %s." % (ip, AWS_R53_ADDR)
print "Current value is %s." % (oldip)
print "Updating records."
r53rr = ResourceRecordSets(r53, AWS_R53_ZONE)
print "Deleting old record."
d_record = r53rr.add_change("DELETE", AWS_R53_ADDR, "A", 300)
d_record.add_value(oldip)
print "Creating updated record."
c_record = r53rr.add_change("CREATE", AWS_R53_ADDR,"A", 300)
c_record.add_value(ip)
print "Committing changes."
r53rr.commit()
print "Records updated with new IP at %s." % (ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment