53 lines
1.7 KiB
Python
Executable File
53 lines
1.7 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
import sys
|
|
import boto3
|
|
import json
|
|
import argparse
|
|
|
|
|
|
def update_dns(record_name, ips=[], ttl=180, action="UPSERT", record_type='A'):
|
|
route53 = boto3.client("route53")
|
|
zone_id = route53.list_hosted_zones_by_name(
|
|
DNSName=".".join(record_name.split(".")[1:])
|
|
)["HostedZones"][0]["Id"]
|
|
|
|
changeset = {
|
|
"Changes": [
|
|
{
|
|
"Action": action,
|
|
"ResourceRecordSet": {
|
|
"Name": record_name,
|
|
"Type": record_type,
|
|
"TTL": ttl,
|
|
"ResourceRecords": [],
|
|
},
|
|
}
|
|
]
|
|
}
|
|
for ip in ips:
|
|
changeset["Changes"][0]["ResourceRecordSet"]["ResourceRecords"].append(
|
|
{"Value": ip}
|
|
)
|
|
|
|
route53.change_resource_record_sets(HostedZoneId=zone_id, ChangeBatch=changeset)
|
|
|
|
|
|
parser = argparse.ArgumentParser(description='Update Route53 entries')
|
|
parser.add_argument('--fqdn', dest='fqdn', action='store', required=True,
|
|
help='FQDN for this record')
|
|
parser.add_argument('--record', action='append', required=True,
|
|
help='Value of a record')
|
|
parser.add_argument('--type', dest='record_type', action='store', default='A',
|
|
help='Record type')
|
|
parser.add_argument('--ttl', dest='ttl', action='store', default=180, type=int,
|
|
help='TTL of the entry')
|
|
parser.add_argument('--delete', dest='delete', action='store_true',
|
|
help='delete entry')
|
|
|
|
args = parser.parse_args()
|
|
action = "UPSERT"
|
|
if args.delete:
|
|
action = "DELETE"
|
|
|
|
update_dns(args.fqdn, args.record, action=action, ttl=args.ttl, record_type=args.record_type)
|