Skip to content

Instantly share code, notes, and snippets.

@rjurney
Last active November 19, 2020 22:55
Show Gist options
  • Save rjurney/c4e90a1b62842fbf939a0767a2626b08 to your computer and use it in GitHub Desktop.
Save rjurney/c4e90a1b62842fbf939a0767a2626b08 to your computer and use it in GitHub Desktop.
Bash script to disable termination protection and then terminate all instances in all regions :)
for region in `aws ec2 describe-regions | jq -r .Regions[].RegionName`
do
echo "Terminating region $region..."
aws ec2 describe-instances --region $region | \
jq -r .Reservations[].Instances[].InstanceId | \
xargs -L 1 -I {} aws ec2 modify-instance-attribute \
--region $region \
--no-disable-api-termination \
--instance-id {}
aws ec2 describe-instances --region $region | \
jq -r .Reservations[].Instances[].InstanceId | \
xargs -L 1 -I {} aws ec2 terminate-instances \
--region $region \
--instance-id {}
done
@cappellaiomatto
Copy link

aws ec2 describe-regions --output text
gives me 4 columns
REGIONS ec2.eu-north-1.amazonaws.com opt-in-not-required eu-north-1
so it should be cut -f4 or better:
aws ec2 describe-regions | jq -r .Regions[].RegionName

@rjurney
Copy link
Author

rjurney commented Sep 13, 2019

@cappellaiomatto Fixed, thanks!

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