Skip to content

Instantly share code, notes, and snippets.

@joshm1
Created April 21, 2017 15:30
Show Gist options
  • Save joshm1/3b6f23b0527b1de44ea30d98dbe8bf71 to your computer and use it in GitHub Desktop.
Save joshm1/3b6f23b0527b1de44ea30d98dbe8bf71 to your computer and use it in GitHub Desktop.
bash function to delete ec2 instances based on private DNS names
ec2_terminate() {
local private_dns_name=$1
local instance_ids=$(aws ec2 describe-instances \
--filters "Name=private-dns-name,Values=$private_dns_name" | \
jq '.Reservations | .[] | .Instances | .[] | .InstanceId' -r | tr '\n' ' ')
if [ -z $instance_ids ]; then
echo "instances not found for $private_dns_name"
exit 1
fi
echo "Terminating EC2 Instances:$instance_ids\n"
local cmd="aws ec2 terminate-instances --instance-ids $instance_ids"
echo $cmd
bash -c $cmd
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment