I'm learning how to use AWS infrastructure and CLI tools. I had many EC2 instances with public IP that I have terminated using another CLI script authored by Russell Jurney Source.
I tried to modify this to release all Public IPs, but as I'm very new to scripting and json I can't get my head around this one. How to address all Public IPs in this script and do correct loops so each IP is released?
for region in `aws ec2 describe-regions | jq -r .Regions[].RegionName`
do
echo "Releasing Elastic IPs in region $region..."
for address in 'aws ecs describe-regions | jq -r .Regions[].RegionName[]'
do
aws ec2 disassociate-address --region $region | \
jq -r .Reservations[].Instances[].PrivateIpAddress| \
xargs -L 1 -I {} aws ec2 modify-instance-attribute \
--region $region \
--allocation-id {}\
--public-ip {}
aws ec2 release-address --region $region | \
jq -r .Reservations[].Instances[].PrivateIpAddress | \
xargs -L 1 -I {} aws ec2 terminate-instances \
--region $region \
--allocation-id {}
--instance-id {}
done
done