Created
August 12, 2011 03:35
-
-
Save jawnb/1141379 to your computer and use it in GitHub Desktop.
Script to populate route53 DNS records from ec2 instance id and instance names.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import boto | |
from boto.route53.record import ResourceRecordSets | |
import logging | |
conn = boto.connect_ec2() | |
DNS_EXCLUSION_TAG = 'ExcludeFromDNS' # If this tag exists on an instance, no DNS values will be populated | |
DNS_TAGS = ['ShortName', 'Name'] # This is the list of instance tags we want to populate DNS entries from | |
DNS_SUFFIX = 'YOUR_SUBDOMAIN_HERE' # Suffix under which to create DNS records | |
ROUTE53_ZONE_ID = 'YOUR_ZONE_ID_HERE' # The zone id from route53 of the zone we will be adding these entries under | |
LOG_PATH = '/usr/local/scripts/populate_dns_records.log' | |
logging.basicConfig(filename=LOG_PATH, level=logging.WARNING, format='%(asctime)s %(module)s %(message)s') | |
def add_cname(instance, hostname): | |
conn = boto.connect_route53() | |
existing_entries = conn.get_all_rrsets(ROUTE53_ZONE_ID) | |
changes = ResourceRecordSets(conn, ROUTE53_ZONE_ID) | |
for item in existing_entries: | |
# Move to the next entry if we do not match | |
if item.name != hostname and item.name != "CNAME" and item.ttl != 60: | |
continue | |
# If we have nothing to change, return | |
if instance.dns_name in item.resource_records: | |
logging.info("Nothing to change for %s", hostname) | |
return | |
# We're a record that needs updating, delete the existing entry, so we can re-add it | |
for record in item.resource_records: | |
logging.warning("Deleting CNAME {0}/{1}".format(hostname, record)) | |
change = changes.add_change("DELETE", hostname, "CNAME", ttl=60) # Change this at some point to ttl=item.ttl, leaving it hard set so we don't overwrite anything entered by hand | |
change.add_value(record) | |
# We either don't exist or need to update our existing entry (its already been deleted) | |
logging.warning("Adding CNAME entry for %s to %s", hostname, instance.dns_name) | |
change = changes.add_change("CREATE", hostname, "CNAME", ttl=60) | |
change.add_value(instance.dns_name) | |
try: | |
changes.commit() | |
except Exception, e: | |
logging.error(e) | |
if __name__ == '__main__': | |
reservations = conn.get_all_instances() | |
entries_to_make = {} | |
for reservation in reservations: | |
instances = reservation.instances | |
for instance in instances: | |
if instance.tags.get(DNS_EXCLUSION_TAG, None): | |
logging.info("Skipping {0}" .format(instance.id) ) | |
continue | |
hostnames = set() | |
hostnames.add(instance.id + ".ec2" + DNS_SUFFIX) | |
for tag in DNS_TAGS: | |
tag_value = instance.tags.get(tag, None) | |
if tag_value is not None: | |
hostname = tag_value + DNS_SUFFIX | |
tag_value = instance.tags.get(tag, None) | |
if tag_value is not None: | |
hostname = tag_value + DNS_SUFFIX | |
hostnames.add(hostname) | |
for hostname in hostnames: | |
add_cname(instance, hostname) |
You're correct. The code i posted initially is busted. I updated the script I used but never updated the gist. I went ahead and updated the gist with the final version I used. This should fix the issue where things aren't deleted. I hope the script was of some use. Let me know if you run into more issues.
Awesome, i'll give it a shot on Monday - Thanks heaps!! :)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I can't seem to update existing records with this code - It seems like line 32-34 is intended to delete the existing record but from what I can gather from Amazon's API it needs the current value to delete a record - As line 34 uses instance.dns_name, is there a quick change that can be made to pull the actual record value from existing_entries or would it be better to improve the logic of line 27's 'if in' statement, and retrieve the value at that time?