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

时间:2016-10-22 20:09:46

标签: python amazon-web-services dns aws-sdk amazon-route53

如何删除Route 53中的DNS记录?我跟着documentation,但我仍然无法使其发挥作用。我不知道我是否在这里遗漏了一些东西。

基于文档:

  

DELETE:删除具有指定的现有资源记录集   Name,Type,SetIdentifier的值(延迟,加权,   地理位置和故障转移资源记录集)和TTL(别名除外)   资源记录集,其TTL由AWS确定   您将DNS查询路由到的资源。

但我总是收到这个错误:

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()

2 个答案:

答案 0 :(得分:5)

您需要一个嵌套的资源记录&#39; ResourceRecordSet中的数组,它具有当前的&#39;目标&#39;记录的价值。

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

答案 1 :(得分:0)

添加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)