Route 53: Apex domain quick fix

apex, bare domain ( domain without www) and www.domain.com should point to the same IP. In amazon load balance or some of the cloud solution, they do not provide us a static IP, they provide us a name and asks us to point our domain to their domain. There is no problem with www.domain.com , but we can’t point apex domain (domain.com) to a cname record, it ‘s because the current DNS RFC does not allow this.

There are many dns hosting providers customized their DNS to support this feature. If you are using Route53, you can use the script below to monitor the difference between the 2 records, if it see a difference , it will update the record automatically.

This use boto library.

apt-get install python-boto

You also need to update your aws credentials in .aws folder.

 

#!/usr/bin/python
#this script is used to  compare the  2 records domain.com and www.domain.com , if there is mistmatch  it will get the records from www to update to  domain.com
#this is to fix the issue where apex domain can not have a cname record
import boto.route53
import json
import socket
conn = boto.route53.connect_to_region("us-east-1")

zones = conn.get_all_hosted_zones()

#zones = zones.json()
#print (zones)

for zone in zones["ListHostedZonesResponse"]["HostedZones"]:
  Id = zone["Id"].replace('/hostedzone/', '')
  zone_name = zone["Name"]
#  print zone_name,Id

  ip_www_list = socket.gethostbyname_ex("www."+zone_name)[-1]
  ip_list=  socket.gethostbyname_ex(zone_name)[-1]
  
  for ip in ip_list:
     if ip not in ip_www_list:
         print zone_name,"IP ",ip ," not in the list", ip_www_list
         changes = boto.route53.record.ResourceRecordSets(conn, Id)
         change = changes.add_change("UPSERT", zone_name, "A")
         for new_ip in ip_www_list:
             change.add_value(new_ip)
         result = changes.commit()
         break

 

Leave a Reply

Your email address will not be published. Required fields are marked *