Created
April 21, 2017 15:30
-
-
Save joshm1/3b6f23b0527b1de44ea30d98dbe8bf71 to your computer and use it in GitHub Desktop.
bash function to delete ec2 instances based on private DNS names
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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