AWS Route 53列出使用boto3的CNAME记录

时间:2017-01-18 10:12:18

标签: amazon-web-services amazon-route53 cname

我想列出某个托管区域中的所有CNAME记录。我们假设我的托管区域中有超过400条记录。我正在使用boto3

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

此命令列出所有类型的100条记录。缺少很多CNAME记录 如何遍历所有记录以便列出所有CNAME记录?

4 个答案:

答案 0 :(得分:4)

您应该使用AWS提供的官方分页器方法: https://boto3.readthedocs.io/en/latest/reference/services/route53.html#Route53.Paginator.ListResourceRecordSets

无论记录数量如何,列出CNAME记录的示例代码:

#!/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

答案 1 :(得分:1)

好的,我在清楚阅读文档后找到了答案。如果返回的maxitems将超过100,则每100条记录将具有尾随NextRecordType和NextRecordName字段。我们需要使用这些来获取接下来的100个项目,依此类推。 这段代码对我有用,如果我的方法有误,请告诉我。

NextRecordName = 'a'
NextRecordType = 'CNAME'
while(NextRecordName is not None and NextRecordType is not None):
    response_per_zone = client.list_resource_record_sets(HostedZoneId=Id,StartRecordName=NextRecordName, StartRecordType=NextRecordType ,MaxItems='400')

    try:
        NextRecordName = response_per_zone['NextRecordName']
        NextRecordType = response_per_zone['NextRecordType']
    except Exception as e:
        NextRecordName = None
        NextRecordType = None



    print NextRecordType
    print NextRecordName
    #Since I need to find CNAME records, this is a function to check whether the record is CNAME, checking it is done using response_record = client.list_resource_record_sets(HostedZoneId=hostedzone, StartRecordName=cname_record, MaxItems='1')

    private_zone = resp['Config']['PrivateZone']
    if private_zone == False:
        find_record(response_per_zone, Id, record_stack)

答案 2 :(得分:1)

根据AWS documentation:将在响应中设置一个标志,以指示结果是否已被截断(IsTruncated将设置为True) - 然后您可以查看{的结果{1}}在响应中作为NextRecordName

的参数

如果您需要我编写示例代码,请告诉我,我很乐意:)

答案 3 :(得分:0)

如果有更多记录,大多数列表方法将返回NextToken。 list_resource_record_sets改用NextRecordName,您可以像NexToken一样使用它。这是将迭代所有记录的示例代码,

import boto3

aws_profile = '...'
zone_id = 'Z2A...'
max_records = 1000
session = boto3.Session(profile_name=aws_profile)
route53 = session.client('route53')
dns_records = []

dns_in_iteration = route53.list_resource_record_sets(HostedZoneId=zone_id)
dns_records.extend(dns_in_iteration['ResourceRecordSets'])

while len(dns_records) < max_records and 'NextRecordName' in dns_in_iteration.keys():
    next_record_name = dns_in_iteration['NextRecordName']
    print('listing next set: ' + next_record_name)
    dns_in_iteration = route53.list_resource_record_sets(HostedZoneId=zone_id, StartRecordName=next_record_name)
    dns_records.extend(dns_in_iteration['ResourceRecordSets'])

print('records found: ' + str(len(dns_records)))    
for record in dns_records:
    if record['Type'] == 'CNAME':
        print(record['Name'])

我添加了max_records来测试我的20k条以上的记录,因此它可以以较小的记录集或记录停止。