Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Python SDK | Route 53 - delete resource record

How to delete a DNS record in Route 53? I followed the documentation but I still can't make it work. I don't know if I'm missing something here.

Based on the documentation:

DELETE : Deletes a existing resource record set that has the specified values for Name , Type , SetIdentifier (for latency, weighted, geolocation, and failover resource record sets), and TTL (except alias resource record sets, for which the TTL is determined by the AWS resource that you're routing DNS queries to).

But I'm always getting this error:

Traceback (most recent call last):                                                                                                                                      
  File "./test.py", line 37, in <module>                                                                                                                                
    main()                                                                                                                                                              
  File "./test.py", line 34, in main                                                                                                                                    
    print(del_record())                                                                                                                                                 
  File "./test.py", line 23, in del_record                                                                                                                              
    'TTL': 300                                                                                                                                                          
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/botocore/client.py", line 251, in _api_call                                       
    return self._make_api_call(operation_name, kwargs)                                                                                                                  
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/botocore/client.py", line 537, in _make_api_call                                  
    raise ClientError(parsed_response, operation_name)                                                                                                                  
botocore.exceptions.ClientError: An error occurred (InvalidInput) when calling the ChangeResourceRecordSets operation: Invalid request 

Here's my code:

#!/usr/bin/env python3


import boto3

r53 = boto3.client('route53')
zone_id = 'ABCDEFGHIJKLMNO'
record = 'me.domain.com'
r_type = 'CNAME'
r_val = 'google.com'


def del_record():
    response = r53.change_resource_record_sets(
        HostedZoneId=zone_id,
        ChangeBatch={
            'Changes': [
                {
                    'Action': 'DELETE',
                    'ResourceRecordSet': {
                        'Name': record,
                        'Type': r_type,
                        'TTL': 300
                    }
                }
            ]
        }
    )

    return response


def main():
    print(del_record())

if __name__ == '__main__':
    main()
like image 967
devxvda1 Avatar asked Feb 05 '23 21:02

devxvda1


1 Answers

You need a nested 'ResourceRecords' array in the ResourceRecordSet, which has the current 'target' value of the record.

    HostedZoneId=zone_id,
    ChangeBatch={
        'Changes': [
            {
                'Action': 'DELETE',
                'ResourceRecordSet': {
                    'Name': record,
                    'Type': r_type,
                    'TTL': 300,
                    'ResourceRecords': [
                        {
                            'Value': target
                        }
                    ]
                }
            }
        ]
    }
like image 136
Daniel Scott Avatar answered Feb 08 '23 11:02

Daniel Scott