Koralkloud
Koralkloud

Reputation: 444

Delete hosted zones in Route53 using boto3

I need to delete all those hosted zones in Route53 using boto3

I created the zones in AWS console and trying to delete all those zones using boto3 using lamda function. I used the below codes however it throws error

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'])

Getting the following error message:

'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'

Upvotes: -1

Views: 582

Answers (1)

danimal
danimal

Reputation: 1697

Your python KeyError is giving you the information: there is no key hosted-zones on the client response object; the key you are looking for is HostedZones. See the boto3 Route53 documentation which contains the response syntax. The key HostedZone also does not exist at that level of the response object, you need to use Id. The documentation for boto3 is pretty good for this kind of thing!

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

Upvotes: 1

Related Questions