Route53 IP Update Automation

We have to modify like 50 IP addresses and TTL’s for a client and the obvious solution is LET’S AUTOMATE IT, so since they are using AWS Route 53, I looked up the docs and threw together the following little piece of heaven.

#!/usr/bin/env python
Automation for City of XXX
to accomplish the following:
    1. Update TTL
    2. Modify A records from 1.2.3.x to 2.3.4.x
from boto3.session import Session
import boto3, json, re

def init_boto3():
    instantiate session object for use of
    boto api - this determines which user
    session = Session(
    if session:
        return session

def get_current_zones(client):
    Returns list of current hosted zones for
    configured amazon account
    return client.list_hosted_zones()

def get_current_records(client, record_id):
    Returns list of Records for given HostedZoneId
    return client.list_resource_record_sets(

def get_new_ip(records):
    Filters only A records from Record Set
    for record in records:
        if record['Type'] == 'A':
            match ='(1\.2\.3)\.(\d+)', record['ResourceRecords'][0]['Value'])
            if match:
                if == '1.2.3':
                    new_ip = '2.3.4.{}'.format(
                    return new_ip

def modify_record(client, zone_name, zone_id, new_ip, ttl=600):
    Modifies a given resource record set for
    print client.change_resource_record_sets(
            'Comment': 'Modification by Matthew Harris',
            'Changes': [
                    'Action': 'UPSERT',
                    'ResourceRecordSet': {
                        'TTL': ttl,
                        'Name': zone_name,
                        'Type': 'A',
                        'ResourceRecords': [
                                'Value': new_ip

if __name__ == '__main__':
    # Setup Route 53 Session
    session = init_boto3()
    client = session.client('route53')

    # Get all Hosted Zones
    zones = get_current_zones(client)

    # Get records for each zone
    for zone in zones['HostedZones']:
        records = get_current_records(client, zone['Id'])
        new_ip = get_new_ip(records)
        if new_ip:
            print " * Updating Zone for {}".format(zone['Name'])
            modify_record(client, zone['Name'], zone['Id'], new_ip)
            print " * Hosted Zone did not match IP"

So with the above you get a great overview of the things you can do with boto3 and route53, from retrieving records to updating records.

Write a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.