Last active
June 28, 2018 07:53
-
-
Save avi-beetul/1b61c713cf7b2d9caeef6b4c0c70d75c to your computer and use it in GitHub Desktop.
dns_route53.py
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 | |
| """ | |
| This script can be used to create, update single, update batch and delete DNS record-set in aws Route53. | |
| Pass `AWS PROFILE`, `domain name` as command-line argument to create a connection to aws account. | |
| Depending on the action, set params name and/or value as command-line argument. | |
| Example: python dns_route53.py create_record aws_profile domain name type value | |
| Requirements: | |
| sudo pip install boto dnspython | |
| Limits: | |
| 1. A request cannot contain more than 100 Change elements. | |
| 2. A request cannot contain more than 1000 ResourceRecord elements. | |
| 3. The sum of the number of characters (including spaces) in all Value elements in a request cannot exceed 32,000 characters. | |
| 4. You cannot delete the same resource record set more than once in a single change batch. | |
| """ | |
| import os | |
| import sys | |
| import platform | |
| import urllib, urllib2 | |
| from boto.route53.connection import Route53Connection | |
| from boto.route53.connection import ResourceRecordSets | |
| class ManageDNS: | |
| def __init__(self, aws_profile, domain): | |
| self.route53 = Route53Connection(profile_name=aws_profile) | |
| self.domain = domain | |
| self.zone_id = self.route53.get_zone(domain + ".").id | |
| def create_record(self, name, type, value): | |
| changes = ResourceRecordSets(self.route53, self.zone_id) | |
| change = changes.add_change("CREATE", name + "." + self.domain, type, 300) | |
| change.add_value(value) | |
| changes.commit() | |
| def update_record(self, name, newValue): | |
| changes = ResourceRecordSets(self.route53, self.zone_id) | |
| sets = self.route53.get_all_rrsets(self.zone_id, None) | |
| for rset in sets: | |
| if rset.name == name + "." + self.domain + ".": | |
| change = changes.add_change("UPSERT", rset.name, rset.type, rset.ttl) | |
| if rset.alias_dns_name: | |
| change.set_alias(rset.alias_hosted_zone_id, | |
| newValue, alias_evaluate_target_health=False) | |
| else: | |
| change.add_value(newValue) | |
| changes.commit() | |
| # match all records with the start of the fully qualified domain name | |
| # with the name param and perform and update. | |
| def update_batch_record(self, name, newValue): | |
| changes = ResourceRecordSets(self.route53, self.zone_id) | |
| sets = self.route53.get_all_rrsets(self.zone_id, None) | |
| for rset in sets: | |
| if rset.name.startswith(name) and (rset.type != "NS" or rset.type != "SOA"): | |
| # Add a change request to the set | |
| changeUpsert = changes.add_change("UPSERT", rset.name, rset.type, rset.ttl) | |
| if rset.alias_dns_name: | |
| changeUpsert.set_alias(rset.alias_hosted_zone_id, newValue, alias_evaluate_target_health=False) | |
| else: | |
| changeUpsert.add_value(newValue) | |
| changes.commit() | |
| def delete_record(self, name): | |
| changes = ResourceRecordSets(self.route53, self.zone_id) | |
| value = None | |
| sets = self.route53.get_all_rrsets(self.zone_id, None) | |
| for rset in sets: | |
| if rset.name == name + "." + self.domain + ".": | |
| value = rset.resource_records[0] | |
| type = rset.type | |
| ttl = rset.ttl | |
| if value != None: | |
| change = changes.add_change("DELETE", name + "." + self.domain, type, ttl) | |
| change.add_value(value) | |
| changes.commit() | |
| if __name__ == '__main__': | |
| r53_zone = ManageDNS(sys.argv[2], sys.argv[3]) | |
| print getattr(r53_zone, sys.argv[1])(*sys.argv[4:]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment