如何基于Value定位Route53超万条记录?CLI查询方案
Dealing with large Route53 hosted zones (10,000+ records) is frustrating when the web UI can't filter by value for zones over 2000 records. The AWS CLI's list-resource-record-sets command combined with JMESPath queries is the perfect workaround here. Let's break down how to do this effectively:
Basic Command Structure
The core command uses --hosted-zone-id to target your zone, and --query with JMESPath syntax to filter records by their value. Here's the base template:
aws route53 list-resource-record-sets --hosted-zone-id YOUR_HOSTED_ZONE_ID --query 'YOUR_JMESPATH_QUERY' --output table
Key JMESPath Filtering Examples
1. Exact Value Match (Any Record Type)
To find all records where the value exactly matches your target (e.g., "192.168.1.1"), use this query. It checks the ResourceRecords array (where values are stored) for a matching entry:
aws route53 list-resource-record-sets --hosted-zone-id Z3RB47PQXVL6N2 --query 'ResourceRecordSets[?ResourceRecords[?Value==`"192.168.1.1"`]]' --output table
2. Exact Match for a Specific Record Type
If you want to narrow it down to a specific type (like A or CNAME), add a type filter:
# Filter A records with exact value aws route53 list-resource-record-sets --hosted-zone-id Z3RB47PQXVL6N2 --query 'ResourceRecordSets[?Type==`A` && ResourceRecords[?Value==`"192.168.1.1"`]]' --output table # Filter CNAME records pointing to example.com aws route53 list-resource-record-sets --hosted-zone-id Z3RB47PQXVL6N2 --query 'ResourceRecordSets[?Type==`CNAME` && ResourceRecords[?Value==`"example.com."`]]' --output table
Note: Route53 stores CNAME values with a trailing dot, so make sure to include that in your query.
3. Partial/Contains Match
If you need to find values that contain a specific string (e.g., all records pointing to a subdomain of example.com), use the contains() function:
aws route53 list-resource-record-sets --hosted-zone-id Z3RB47PQXVL6N2 --query 'ResourceRecordSets[?ResourceRecords[?contains(Value, `"example.com"`)]].{Name: Name, Type: Type, Value: ResourceRecords[0].Value}' --output table
This also formats the output to only show relevant fields (Name, Type, Value) for readability.
4. Handle Pagination Automatically
For zones with 10k+ records, the CLI will automatically paginate results. If you want to export all matching records to a JSON file (instead of viewing them in the terminal), use this command:
aws route53 list-resource-record-sets --hosted-zone-id Z3RB47PQXVL6N2 --query 'ResourceRecordSets[?ResourceRecords[?Value==`"192.168.1.1"`]]' > matching_records.json
Quick Tips
- Always wrap your target value in both backticks and double quotes (
`"VALUE"`) within the JMESPath query to avoid syntax errors. - Use
--output tablefor human-readable results, or--output jsonfor programmatic use. - If you need to filter weighted or latency records, adjust the query to include fields like
SetIdentifierif needed.
内容的提问来源于stack exchange,提问作者Aaron St Clair

