Skip to content

Instantly share code, notes, and snippets.

@Gabriellpweb
Last active May 28, 2019 16:14
Show Gist options
  • Save Gabriellpweb/e0f51daffdacfb204bd89ea6cde9b844 to your computer and use it in GitHub Desktop.
Save Gabriellpweb/e0f51daffdacfb204bd89ea6cde9b844 to your computer and use it in GitHub Desktop.
Aws Elastic IP roller script
#!/bin/bash
region=YOUR REGION HERE
# Get a new Eip
allocated_eip=$(aws --region $region ec2 allocate-address --output table | perl -lne 'print $& if /(\d+\.){3}\d+/')
# Get the current Eip
old_ip=$(curl -s ident.me)
# Get instance id
instance_id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id)
# Associate the new Eip
aws --region $region ec2 associate-address --instance-id $instance_id --public-ip $allocated_eip
# Get old ip association id
allocation_id=$(aws --region $region ec2 describe-addresses --public-ip $old_ip --query Addresses[].AllocationId --output text | tr -d '\n')
# Release the old eip
aws --region $region ec2 release-address --allocation-id $allocation_id
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment