AWS Route 53 Listing CNAME Records using boto3

Amit740 picture Amit740 · Jan 18, 2017 · Viewed 7.3k times · Source

I want to list all the CNAME records in a certain hosted zone. Let's say I have over 400 records in my hosted zone. I'm using boto3:

response_per_zone = client.list_resource_record_sets(HostedZoneId=Id, MaxItems='100')

This command list 100 records of all types. There is a lot of CNAME records missing.
How do I iterate through all the records so that I can list all the CNAME records?

Answer

Serban Cezar picture Serban Cezar · May 26, 2017

You should just use the official paginator method provided by AWS: https://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Paginator.ListResourceRecordSets

Example code for listing CNAME records regardless of the number of records:

#!/usr/bin/env python3

paginator = client.get_paginator('list_resource_record_sets')

try:
    source_zone_records = paginator.paginate(HostedZoneId='HostedZoneId')
    for record_set in source_zone_records:
        for record in record_set['ResourceRecordSets']:
            if record['Type'] == 'CNAME':
                print(record['Name'])

except Exception as error:
    print('An error occurred getting source zone records:')
    print(str(error))
    raise