Quantcast
Channel: Active questions tagged amazon-ec2 - Stack Overflow
Viewing all articles
Browse latest Browse all 29253

Script to disassociate and release all Elastic IP addresses from all regions using bash and AWS CLI

$
0
0

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

Viewing all articles
Browse latest Browse all 29253

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>