Automating Route 53 Domain and Hosted Zone Management with AWS CloudShell

Managing your AWS Route 53 domains and hosted zones can be very easy when you’re using AWS CloudShell. The reason I needed to do a quick script was that I have a lot of domains in Route53 that I have registered, and each has a hosted zone.

The problem is that I have been holding onto hosted zones that are now there without any domains because I’ve let domains expire or sold them over time. What I need to be able to quickly do is check all my registered domains, all the hosted zones, and find the mismatches.

Here’s a simple script to list all registered domains and identify hosted zones without associated domains.

What does the Script do?

This script accomplishes two main tasks:

  1. Lists all registered Route 53 domains.
  2. Identifies hosted zones without registered domains.

It’s just that easy, but requires a few steps along the way to accomplish these tasks.

The Script

#!/bin/bash

# List all Route 53 domains
echo "Listing all Route 53 domains:"
aws route53domains list-domains --query 'Domains[].DomainName' --output text | tr '\t' '\n'

# List all Route 53 hosted zones
echo "Listing all Route 53 hosted zones:"
aws route53 list-hosted-zones --query 'HostedZones[].Id' --output text | sed 's/\/hostedzone\///' | tr '\t' '\n' > all_hosted_zones.txt

# List domains for comparison
aws route53domains list-domains --query 'Domains[].DomainName' --output text | tr '\t' '\n' > registered_domains.txt

# Find hosted zones without registered domains
echo "Hosted zones without registered domains:"
while IFS= read -r zone_id; do
    zone_name=$(aws route53 get-hosted-zone --id $zone_id --query 'HostedZone.Name' --output text)
    if ! grep -q "$zone_name" registered_domains.txt; then
        echo "$zone_name"
    fi
done < all_hosted_zones.txt

# Cleanup
rm all_hosted_zones.txt registered_domains.txt

How to Use this Script

  1. Open AWS CloudShell.
  2. Copy the script into a file (e.g., list_route53.sh).
  3. Make the script executable: chmod +x list_route53.sh.
  4. Run the script: ./list_route53.sh.

What is the Output?

  • Registered Domains: Listed line-by-line for easy reading and copying into a spreadsheet or document.
  • Hosted Zones Without Domains: Identified and listed so that I know which ones are mismatched and can be deleted.

This script is a rather specific but was worth sharing because its a practical solution for AWS administrators to manage Route 53 resources. All you have to do is copy the script, run it in CloudShell, and easily transfer the output to your preferred spreadsheet software for further analysis.

Happy scripting!

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.