AWS Python SDK | Route 53 - 删除资源记录

AWS Python SDK | Route 53 - delete resource record

如何删除 Route 53 中的 DNS 记录?我按照 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).

但我总是收到这个错误:

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 

这是我的代码:

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

您需要在 ResourceRecordSet 中嵌套 'ResourceRecords' 数组,其中包含记录的当前 'target' 值。

    HostedZoneId=zone_id,
    ChangeBatch={
        'Changes': [
            {
                'Action': 'DELETE',
                'ResourceRecordSet': {
                    'Name': record,
                    'Type': r_type,
                    'TTL': 300,
                    'ResourceRecords': [
                        {
                            'Value': target
                        }
                    ]
                }
            }
        ]
    }

添加 ResourceRecords 仍然出现错误,提示找不到记录。与其手动构建可能仍然遗漏所需元素的记录对象,不如先找到记录,然后将记录传递给方法。

import boto3

aws_profile = '...'
zone_id = 'Z2A....'

session = boto3.Session(profile_name=aws_profile)
route53 = session.client('route53')

record_to_delete = None
delete_name = '....net'
response = route53.list_resource_record_sets(HostedZoneId=zone_id, StartRecordName=delete_name, MaxItems='1')

print('deleting: ' + delete_name)
if delete_name in response['ResourceRecordSets'][0]['Name']:
    record_to_delete = response['ResourceRecordSets'][0]
    route53.change_resource_record_sets(
        HostedZoneId=zone_id,
        ChangeBatch={
            'Changes': [{
                'Action': 'DELETE',
                'ResourceRecordSet': record_to_delete
            }]
        }
    )
    print('deleted: ' + record_to_delete['Name'])
    
else:
    print('record not found: ' + delete_name)