使用boto3删除Route53中的托管区域

时间:2019-08-28 11:14:05

标签: boto3 amazon-route53 destroy zone

我需要使用boto3删除Route53中所有这些托管区域

我在AWS控制台中创建了区域,并尝试使用lamda函数使用boto3删除所有这些区域。我使用了以下代码,但是会引发错误

import boto3

def lambda_handler(event, context):

    # Iterate over regions
    for region in regions:

        # Running following for a particular region
        print ("*************** Checking region  --   %s " % region['RegionName'])
        reg=region['RegionName']


        ########### cleaning Route53 zones ################

        client = boto3.client('route53',region_name=reg)
        response = client.list_hosted_zones_by_name()
        for zone in response ['hosted-zones']:
            print ("About to delete %s | in %s" % (zone['HostedZone'], region['RegionName']))
            result = client.delete_hosted_zone(HostedZone=zone['HostedZone'])

获取以下错误消息:

'hosted-zones': KeyError
Traceback (most recent call last):
  File "/var/task/lambda_function.py", line 22, in lambda_handler
    for zone in response ['hosted-zones']:
KeyError: 'hosted-zones'

1 个答案:

答案 0 :(得分:1)

您的python KeyError正在为您提供信息:客户端响应对象上没有键hosted-zones;您要查找的密钥是HostedZones。请参阅boto3 Route53 documentation,其中包含响应语法。密钥HostedZone在响应对象的该级别上也不存在,您需要使用Idboto3的{​​{3}}非常适合这种事情!

for zone in response['HostedZones']:
    client.delete_hosted_zone(Id=zone['Id'])