J. Patwary
J. Patwary

Reputation: 477

Using Route 53 CLI to delete records

I'm having trouble using the AWS CLI to delete Route 53 records. I have a list of hundreds of domains and each one needs both 'A' records deleted. I wanted to do this using the CLI to save time, but I can't get the functionality working.

For example, let's say I have the following domain and I want to delete both 'A' records:

enter image description here

I'm using boto3 here, but it is the same AWS CLI API that I can't get working (https://docs.aws.amazon.com/cli/latest/reference/route53/change-resource-record-sets.html). My issue is somewhere in the json filter for this api call:

        HostedZoneId='ABC123DEF456',
        ChangeBatch={
            'Comment': 'deleteing A records for domains',
            'Changes': [
                {
                    'Action': 'DELETE',
                    'ResourceRecordSet': {
                        'Name': 'example.com',
                        'Type': 'A',
                        'Region': 'us-east-1',
                        'ResourceRecords': [
                            {
                                "Value": "1.2.3.4"
                            }
                        ],
                        'AliasTarget': {
                            'HostedZoneId': 'ABC123DEF456',
                            'DNSName': 'example.com',
                            'EvaluateTargetHealth': False
                        }
                    }
                }
            ]
        }

The error I am getting is:

InvalidInput: An error occurred (InvalidInput) when calling the ChangeResourceRecordSets operation: Invalid request: Expected exactly one of [AliasTarget, all of [TTL, and ResourceRecords], or TrafficPolicyInstanceId], but found more than one in Change with [Action=DELETE, Name=example.com, Type=A, SetIdentifier=null]

Upvotes: 2

Views: 4573

Answers (2)

Regis Wilson
Regis Wilson

Reputation: 11

I was able to write some simple shell scripts and JQ to delete massive quantities of records. The correct format for each type (NS|A|ALIAS|CNAME, etc.) has to be followed of course. See https://release.com/blog/how-to-delete-hundreds-or-thousands-of-route53-dns-entries

---From the article---

Now we are ready to actually apply the records and see how much damage we can do! Take the entire output of your records with this kind of query:

aws route53 list-resource-record-sets \
  --hosted-zone-id ${hostedzoneid} \
  --max-items 10000 \
  --output json

And pipe it into the handy command line options provided at the bottom of your screen in the jq player application:

jq --compact-output '[.ResourceRecordSets[] |
  select(.AliasTarget.DNSName == "something.us-west-2.elb.amazonaws.com.") |
  {Action: "DELETE", ResourceRecordSet: {Name: .Name, Type: .Type, AliasTarget: .AliasTarget}}] |
  _nwise(1) |
  {Changes: .}'

And use split to create a bunch of individual files:

split -l 1

Then loop over all your files to apply them in Route53:

for file in x*; do
  aws route53 change-resource-record-sets \
  --hosted-zone-id=${hostedzoneid} \
  --cange-batch=file://${file}
done

Upvotes: 1

Marcin
Marcin

Reputation: 239005

I think there is some confusion between simple record of A type, and simple record of alias A type. Namely, simple alias record should not don't have ResourceRecords.

To check how they are described in your case, you can use the following command:

aws route53 list-resource-record-sets --hosted-zone-id <your-zone-id>

The output of the above command should be helpful in constructing your DELETE.

Below are examples of outputs from my route53:

simple record

        {
            "Name": "<simple-a.example.com.>",
            "Type": "A",
            "TTL": 300,
            "ResourceRecords": [
                {
                    "Value": "1.2.3.4"
                }
            ]
        }

simple record with alias

        {
            "Name": "<simple-alias.example.com.>",
            "Type": "A",
            "AliasTarget": {
                "HostedZoneId": "Z06990762X86XLR2ZGTK4",
                "DNSName": "<example>.",
                "EvaluateTargetHealth": true
            }
        },

Upvotes: 3

Related Questions