20
loading...
This website collects cookies to deliver better user experience
#!/usr/bin/env python
"""
Update DNS for a RecordSet on AWS Route53
"""
import argparse
import sys
import boto3
parser = argparse.ArgumentParser()
parser.add_argument("--zoneId", type=str, default="", help="Route53 Zone Id, recommend setting this as an environment variable")
parser.add_argument("--hostname", type=str, default="", help="The hostname to update example.domain.com")
parser.add_argument("--dns", type=str, default="", help="The new dns value")
args = parser.parse_args()
route53 = boto3.client('route53')
zoneid = args.zoneId
hostname = args.hostname
CNAME = args.dns
if not zoneid or not hostname or not CNAME:
print("Please provide a zone id, hostname and new dns")
return
def updatedns(hostname, newdns):
sets = route53.list_resource_record_sets(HostedZoneId=zoneid)
for rset in sets['ResourceRecordSets']:
if rset['Name'] == hostname and rset['Type'] == 'CNAME':
curdnsrecord = rset['ResourceRecords']
print(curdnsrecord)
if type(curdnsrecord) in [list, tuple, set]:
for record in curdnsrecord:
curdns = record
# print('Current DNS CNAME: %s' % curdns)
curttl = rset['TTL']
# print('Current DNS TTL: %s' % curttl)
if curdns != newdns:
# UPSERT the record
print('Updating %s' % hostname)
route53.change_resource_record_sets(
HostedZoneId=zoneid,
ChangeBatch={
'Changes': [
{
'Action': 'UPSERT',
'ResourceRecordSet': {
'Name': hostname,
'Type': 'CNAME',
'TTL': curttl,
'ResourceRecords': [
{
'Value': newdns
}
]
}
}
]
}
)
#!bin/bash
# Shell script to update AWS Route53 DNS when switching Blue/Green Deployments
if [ $# -lt 1 ]
then
echo "Please supply $1 destination DNS address (likely an ELB address)"
return 1
fi
# array of domains to update
domains=("example.mydomain.com" "example2.mydomain.com")
for i in "${domains[@]}"
do
echo "updating $i";
python updatedns.py --zoneId="$MY_ROUTE53_HOSTED_ZONE" --hostname="$i" --dns="$1"
done
echo "Finished updating Route53 records"