Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Python script to add/update an A record at amazon area53 DNS service, using current IP. (ie, dyndns replacement)
from area53 import route53
from boto.route53.exception import DNSServerError
import requests
import sys
from datetime import datetime
# Modified from
domain = 'domain.tld'
subdomain = 'subdomain_name'
def get_public_ip():
r = requests.get('')
return r.text.rstrip()
fqdn = '%s.%s' % (subdomain, domain)
zone = route53.get_zone(domain)
arec = zone.get_a(fqdn)
new_value = get_public_ip()
datestr = '"Last update %s."' % datetime.utcnow().strftime('%Y-%m-%d %H:%M')
if arec:
old_value = arec.resource_records[0]
if old_value == new_value:
print '%s is current. (%s)' % (fqdn, new_value)
print 'Updating %s: %s -> %s' % (fqdn, old_value, new_value)
zone.update_a(fqdn, new_value, 900)
zone.update_txt(fqdn, datestr, 900)
except DNSServerError:
# This can happen if the record did not already exist. Let's
# try to add_a in case that's the case here.
zone.add_a(fqdn, new_value, 900)
zone.add_txt(fqdn, datestr, 900)
zone.add_a(fqdn, new_value, 900)
zone.add_txt(fqdn, datestr, 900)
Copy link

pjlewisuk commented Oct 28, 2016

Do you still use this script? I noticed it stopped working a couple of days ago - not sure if related to AWS announcing Route53 support for IPv6?

Copy link

dhardiker commented Mar 12, 2017

I use the AWS CLI for this in a simple shell script:

export AWS_ACCESS_KEY_ID="redacted"
export AWS_SECRET_ACCESS_KEY="redacted"

IP=`curl 2>/dev/null`

/path/to/aws route53 change-resource-record-sets --hosted-zone-id $AWS_ROUTE53_ZONEID --change-batch "{ \"Changes\": [ { \"Action\": \"UPSERT\", \"ResourceRecordSet\": { \"Name\": \"$HOSTNAME\", \"Type\": \"A\", \"TTL\": $TTL, \"ResourceRecords\": [ { \"Value\": \"$IP\" } ] } } ] }"
echo "Updated the DNS Zone to $IP"

You'll need to make sure that the AWS user you're using has route53:ChangeResourceRecordSets access allowed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment