Last active
October 17, 2020 19:43
Star
You must be signed in to star a gist
cli for route53 to create/update A (ipv4) and AAAA (ipv6) records. use in conjunction with https://gist.github.com/gretel/d42879295a0750c102edc7babbebcc0e
This file contains 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 python3 | |
# https://gist.github.com/gretel/8bb0c2936ae5cda12ae5f0bf381dd3f0 | |
# based on https://github.com/edgan/route53-set-hostname/blob/master/route53-set-hostname.py | |
import boto3 | |
import sys | |
import argparse | |
class Course53(object): | |
def __init__(self, region, domain, subdomain, hosted_zone_id, record_type, ipaddress, ttl): | |
self.client = boto3.client('route53') | |
self.domain = domain | |
self.hosted_zone_id = hosted_zone_id | |
self.ipaddress = ipaddress | |
self.subdomain = subdomain | |
self.record_type = record_type | |
self.ttl = ttl | |
self.fqdn = "{0}.{1}".format(self.subdomain, self.domain) | |
def check_existing_record(self): | |
response = self.client.list_resource_record_sets( | |
HostedZoneId=self.hosted_zone_id, | |
StartRecordName=self.fqdn, | |
StartRecordType=self.record_type | |
) | |
found_flag = False | |
if len(response['ResourceRecordSets']) == 0: | |
raise Exception("could not find any '{0}' records matching domain: {0}".format(self.record_type, self.fqdn)) | |
sys.exit(2) | |
if self.fqdn in response['ResourceRecordSets'][0]['Name']: | |
for ip in response['ResourceRecordSets'][0]['ResourceRecords']: | |
if self.ipaddress == ip['Value']: | |
found_flag = True | |
else: | |
raise Exception("cannot find record set for domain: {0}".format(self.fqdn)) | |
sys.exit(3) | |
return found_flag | |
def update_record(self): | |
if self.check_existing_record(): | |
print('address has not changed') | |
sys.exit(0) | |
else: | |
response = self.client.change_resource_record_sets( | |
HostedZoneId=self.hosted_zone_id, | |
ChangeBatch={ | |
'Comment': 'string', | |
'Changes': [ | |
{ | |
'Action': 'UPSERT', | |
'ResourceRecordSet': { | |
'Name': self.fqdn, | |
'Type': self.record_type, | |
'TTL': self.ttl, | |
'ResourceRecords': [ | |
{ | |
'Value': self.ipaddress | |
}, | |
], | |
} | |
}, | |
] | |
} | |
) | |
print('record updated to reflect change') | |
sys.exit(1) | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="update a route53 record") | |
parser.add_argument( | |
"--region", "-r", | |
default="eu-central-1", | |
help="AWS region to connect to", | |
required=False | |
) | |
parser.add_argument( | |
"--domain", "-d", | |
help="domain to modify", | |
required=True | |
) | |
parser.add_argument( | |
"--subdomain", "-s", | |
help="subdomain to modify", | |
required=True | |
) | |
parser.add_argument( | |
"--zone", "-z", | |
help="aws hosted zone id", | |
required=True | |
) | |
parser.add_argument( | |
"--record_type", "--type", "-t", | |
default="A", | |
help="type of record", | |
required=False | |
) | |
parser.add_argument( | |
"--ttl", | |
default=60, | |
help="time to live of record", | |
required=False | |
) | |
parser.add_argument( | |
"--ipaddress", "--ip", "-i", | |
help="ip address", | |
required=True | |
) | |
args = parser.parse_args() | |
run = Course53(args.region, args.domain, args.subdomain, args.zone, args.record_type, args.ipaddress, args.ttl) | |
run.update_record() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment