Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save eugeneius/50750fed26f50308a311 to your computer and use it in GitHub Desktop.
Save eugeneius/50750fed26f50308a311 to your computer and use it in GitHub Desktop.
If you automatically create Route 53 DNS entries for EC2 instances on boot, and you had a bug that meant they were never removed, this gist is for you.
#!/bin/bash
hosted_zone_id=$1
hostnames=`./get_hostnames_from_hosted_zone.sh "$hosted_zone_id"`
while read hostname; do
echo "$hostname"
instance_id=`./hostname_to_instance_id.sh "$hostname"`
if [[ -z $instance_id ]]; then
echo " hostname doesn't contain instance id, skipping."
else
echo " instance_id: $instance_id"
if ./instance_exists.sh $instance_id; then
echo " Instance still exists, skipping."
else
echo " Deleting resource set..."
`./delete_resource_record_set.rb "$hosted_zone_id" "$hostname"`
fi
fi
done <<< "$hostnames"
#!/usr/bin/env ruby
require 'aws-sdk-v1'
hosted_zone_id = ARGV[0]
resource_name = ARGV[1]
hosted_zone = AWS::Route53::HostedZone.new(hosted_zone_id)
resource_record_set = hosted_zone.rrsets[resource_name, 'A']
resource_record_set.delete
#!/bin/bash
hosted_zone_id=$1
aws route53 list-resource-record-sets --hosted-zone-id "$hosted_zone_id" --max-items 100 |
jq --raw-output '.ResourceRecordSets | map(select(.Type == "A")) | .[].Name'
#!/bin/bash
hostname=$1
grep "\.i-[0123456789abcdef]\{8\}\." <<< "$hostname" | sed 's/^.*\(i-[0123456789abcdef]\{8\}\).*$/\1/'
#!/bin/bash
instance_id=$1
errors=`aws ec2 describe-instances --instance-ids "$instance_id" 2>&1 > /dev/null`
if [[ $errors =~ .*InvalidInstanceID.NotFound.* ]]; then
exit 1
else
exit 0
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment