Skip to content

Instantly share code, notes, and snippets.

@earljon
Created August 15, 2017 08:58
Show Gist options
  • Save earljon/8579429f90c3480c06eb2bc952255987 to your computer and use it in GitHub Desktop.
Save earljon/8579429f90c3480c06eb2bc952255987 to your computer and use it in GitHub Desktop.
Delete a Route 53 Record Set in AWS CLI
#!/bin/sh
# NOTE:
# Make sure that the value of Name, Type, TTL are the same with your DNS Record Set
HOSTED_ZONE_ID=<YOUR_HOSTED_ZONE_ID>
RESOURCE_VALUE=<YOUR_DNS_RESOURCE_VALUE-ex:IP or dns>
DNS_NAME=<YOUR_DNS_NAME-ex: subdomain.domain.com>
RECORD_TYPE=<DNS_RECORD_TYPE-ex: A, CNAME>
TTL=<TTL_VALUE>
JSON_FILE=`mktemp`
(
cat <<EOF
{
"Comment": "Delete single record set",
"Changes": [
{
"Action": "DELETE",
"ResourceRecordSet": {
"Name": "$DNS_NAME.",
"Type": "$RECORD_TYPE",
"TTL": $TTL,
"ResourceRecords": [
{
"Value": "${RESOURCE_VALUE}"
}
]
}
}
]
}
EOF
) > $JSON_FILE
echo "Deleting DNS Record set"
aws route53 change-resource-record-sets --hosted-zone-id ${HOSTED_ZONE_ID} --change-batch file://$JSON_FILE
echo "Deleting record set ..."
echo
echo "Operation Completed."
@hinorashi
Copy link

You can try this, which query for the necessary info before performing the deletion:

#!/bin/bash

# =============================================================================================================
# Usage:
#   ./route53-delete-record.sh [Hostname] [Type]
# Example:
#   ./route53-delete-record.sh dummy.example.org
#   ./route53-delete-record.sh dummy.example.org TXT
#   ./route53-delete-record.sh dummy.example.org txt
#   ./route53-delete-record.sh dummy.example.org CNAME
# =============================================================================================================

# output coloring
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CLEAR=$(tput sgr0)

# put your value here
# note that jq can work with env var
HOSTED_ZONE=
DNS_NAME=${1:-test.example.org}
DNS_TYPE=${2:-A}

[[ -z "$HOSTED_ZONE" ]] && HOSTED_ZONE=example.org

# add . to the end
DNS_NAME="$DNS_NAME."

# capitalize
DNS_TYPE=${DNS_TYPE^^}

# find Zone ID
ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name $HOSTED_ZONE --output json \
  | jq .HostedZones[].Id --raw-output \
  | awk -F / '{print $3}')

if [[ -z "$ZONE_ID" ]]; then
  echo ${RED}Hosted zone not found!$CLEAR
  exit 1
fi

echo Zone ID: $YELLOW$ZONE_ID$CLEAR
echo

# find resource record set
RECORD_SETS=$(aws route53 list-resource-record-sets --hosted-zone-id=$ZONE_ID --output json \
  | jq '.ResourceRecordSets[] | select ((.Name == '\"$DNS_NAME\"') and (.Type=='\"$DNS_TYPE\"'))')

if [[ -z "$RECORD_SETS" ]]; then
  echo ${RED}No record found!$CLEAR
  exit 1
fi

echo Resource Record Sets:
jq <<< "$RECORD_SETS"
echo

# prepare the change batch value
CHANGE_BATCH=$(cat << EOF
{
    "Comment": "delete this record",
    "Changes": [
        {
            "Action": "DELETE",
            "ResourceRecordSet":
              $RECORD_SETS

        }
    ]
}
EOF
)

echo Change batch:
jq <<< "$CHANGE_BATCH"
echo

# perform the deletion
aws route53 change-resource-record-sets --hosted-zone-id=$ZONE_ID --change-batch "$CHANGE_BATCH"

@bitroniq
Copy link

@hinorashi I'd suggest to improve this line

 aws route53 list-hosted-zones-by-name --dns-name dev-ip.acreto.net. --output json | jq .HostedZones[].Id --raw-output | awk -F / '{print $3}'

shows 3 IDs:

Z07056181OWBARCHC5VZS
Z0223670ZG946EY9LDV
Z04775222YW062TH2JMK2
Z033767315KG9JEQ9M9VT

This works better:

aws route53 list-hosted-zones | jq '.HostedZones[] | select(.Name == "$DNS_NAME.") | .Id' | awk -F"\"" '{ print $2 }' | awk -F "/" '{ print $3 }'

@armenr
Copy link

armenr commented Dec 17, 2022

#!/bin/bash

# =============================================================================================================
# Usage:
#   ./route53-delete-record.sh [Hostname] [Type]
# Example:
#   ./route53-delete-record.sh dummy.example.org
#   ./route53-delete-record.sh dummy.example.org TXT
#   ./route53-delete-record.sh dummy.example.org txt
#   ./route53-delete-record.sh dummy.example.org CNAME
# =============================================================================================================

# output coloring
RED=$(tput setaf 1)
GREEN=$(tput setaf 2)
YELLOW=$(tput setaf 3)
CLEAR=$(tput sgr0)

# put your value here
# note that jq can work with env var
HOSTED_ZONE=
DNS_NAME=${1:-test.example.org}
DNS_TYPE=${2:-A}

[[ -z "$HOSTED_ZONE" ]] && HOSTED_ZONE=example.org

# add . to the end
DNS_NAME="$DNS_NAME."

# capitalize
DNS_TYPE=${DNS_TYPE^^}

# find Zone ID
ZONE_ID=$(aws route53 list-hosted-zones-by-name --dns-name $HOSTED_ZONE --output json \
  | jq .HostedZones[].Id --raw-output \
  | awk -F / '{print $3}')

if [[ -z "$ZONE_ID" ]]; then
  echo ${RED}Hosted zone not found!$CLEAR
  exit 1
fi

echo Zone ID: $YELLOW$ZONE_ID$CLEAR
echo

# find resource record set
RECORD_SETS=$(aws route53 list-resource-record-sets --hosted-zone-id=$ZONE_ID --output json \
  | jq '.ResourceRecordSets[] | select ((.Name == '\"$DNS_NAME\"') and (.Type=='\"$DNS_TYPE\"'))')

if [[ -z "$RECORD_SETS" ]]; then
  echo ${RED}No record found!$CLEAR
  exit 1
fi

echo Resource Record Sets:
jq <<< "$RECORD_SETS"
echo

# prepare the change batch value
CHANGE_BATCH=$(cat << EOF
{
    "Comment": "delete this record",
    "Changes": [
        {
            "Action": "DELETE",
            "ResourceRecordSet":
              $RECORD_SETS

        }
    ]
}
EOF
)

echo Change batch:
jq <<< "$CHANGE_BATCH"
echo

# perform the deletion
aws route53 change-resource-record-sets --hosted-zone-id=$ZONE_ID --change-batch "$CHANGE_BATCH"

@hinorashi - This doesn't appear to work:

Error parsing parameter '--change-batch': Invalid JSON: Expecting value: line 7 column 9 (char 140)
JSON received: {
    "Comment": "delete this record",
    "Changes": [
        {
            "Action": "DELETE",
            "ResourceRecordSet":
        }
    ]
}

@lgb861213
Copy link

lgb861213 commented Jun 2, 2024

I �use it delete the route hosted zone ,but it failed. so I use the aws_route53_delete.py and delele success

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment