Python3 boto3 Route53 delete hosted zones
This script reviews your domains that you’ve purchased through amazon and gives you the option to set their NS and then delete hosted zone (I wanted to manage DNS elswhere free instead of pay the $7/year BS charge from AWS Route53).
To setup, you need to enable the api, and also configure “aws configure” correctly.
import boto3
def get_domain_nameservers(client_domains, domain_name):
try:
response = client_domains.get_domain_detail(DomainName=domain_name)
nameservers = [ns['Name'] for ns in response.get('Nameservers', [])]
print(f"Current nameservers for {domain_name} are: {nameservers}")
return nameservers
except client_domains.exceptions.ClientError as e:
print(f"Error fetching nameservers for {domain_name}: {e}")
return []
def update_domain_nameservers(client_domains, domain_name):
nameservers = [
{'Name': 'ns1.digitalocean.com'},
{'Name': 'ns2.digitalocean.com'},
{'Name': 'ns3.digitalocean.com'}
]
try:
response = client_domains.update_domain_nameservers(
DomainName=domain_name,
Nameservers=nameservers
)
if 'OperationId' in response:
print(f"Nameservers for {domain_name} updated. Operation ID: {response['OperationId']}")
else:
print(f"Failed to update nameservers for {domain_name}. Unexpected response: {response}")
except client_domains.exceptions.ClientError as e:
print(f"Error updating nameservers for {domain_name}: {e}")
def delete_all_records(client, hosted_zone_id, domain_name):
try:
record_sets = client.list_resource_record_sets(HostedZoneId=hosted_zone_id)['ResourceRecordSets']
for record_set in record_sets:
if record_set['Type'] not in ['NS', 'SOA']:
client.change_resource_record_sets(
HostedZoneId=hosted_zone_id,
ChangeBatch={
'Changes': [{
'Action': 'DELETE',
'ResourceRecordSet': record_set
}]
}
)
except client.exceptions.ClientError as e:
print(f"Error deleting records for {domain_name}: {e}")
def process_hosted_zone(client, domain_name):
domain_name_with_dot = domain_name if domain_name.endswith('.') else domain_name + '.'
try:
zones = client.list_hosted_zones()['HostedZones']
zone = next((z for z in zones if z['Name'] == domain_name_with_dot), None)
if zone:
print(f"Found hosted zone for {domain_name}")
delete = input(f"Do you want to delete the hosted zone for {domain_name}? (yes/no) ").strip().lower()
if delete == 'yes':
delete_all_records(client, zone['Id'], domain_name)
client.delete_hosted_zone(Id=zone['Id'])
print(f"Deleted hosted zone for {domain_name}")
else:
print(f"Cannot find zone: {domain_name}")
except client.exceptions.ClientError as e:
print(f"Error processing hosted zone for {domain_name}: {e}")
if __name__ == "__main__":
client_route53 = boto3.client('route53')
client_route53domains = boto3.client('route53domains', region_name='us-east-1')
domains = ['malchias.com', 'nicebanker.com', 'sfxmakeupartist.com']
for domain in domains:
print(f"DOMAIN: {domain}")
get_domain_nameservers(client_route53domains, domain)
process_hosted_zone(client_route53, domain)
update_domain_nameservers(client_route53domains, domain)
get_domain_nameservers(client_route53domains, domain)
print("\n\n")