Skip to content

Instantly share code, notes, and snippets.

@marcellodesales
Last active January 8, 2016 13:42
Show Gist options
  • Save marcellodesales/4ea62933a9f2e4550180 to your computer and use it in GitHub Desktop.
Save marcellodesales/4ea62933a9f2e4550180 to your computer and use it in GitHub Desktop.
Renew all of your Amazon aws EC2 Fleet IP addresses by rebooting all of them by Tag:key=value using
#!/bin/bash
ACCESS_KEY=ADD_YOUR_KEY
SECRET_KEY=ADD_YOUR_SECRET
echo "########################################################";
echo "IP Renewal Scheduler at $(date)";
echo "########################################################";
echo "";
# The tag used in the EC2 instances
TAG="type=consumer"
consumers=$(ec2-describe-instances --filter tag:$TAG --region sa-east-1 | grep 'INSTANCE' | awk '{print $2}')
consumersArray=($consumers)
numberOfConsumers=${#consumersArray[@]}
echo "=====> There are $numberOfConsumers consumers"
echo $consumers
echo "================================================"
echo ""
# Search all the instances with the tag "type=consumer" http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-DescribeInstances.html
for INSTANCE in $consumers;
do
echo "* Stopping instance ${INSTANCE} at $(date)";
overallStart=`date +%s`
start_time=`date +%s`
# Execute the stop of the instance...
ec2-stop-instances -O ${ACCESS_KEY} -W ${SECRET_KEY} --region sa-east-1 ${INSTANCE}
# Monitor the status of the machine till it is stopped.
while [ 1 ]; do
# http://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/ () makes it an array
tokens=($(ec2-describe-instances --filter "instance-id=$INSTANCE" --region sa-east-1 | grep "INSTANCE"))
size=${#tokens[@]}
# When the machine is not running, it will show 23 columns
if [[ $size = 21 ]]; then
status=$(ec2-describe-instances --filter "instance-id=$INSTANCE" --region sa-east-1 | grep "INSTANCE" | awk '{print $5}')
fi
# INSTANCE i-xxxxxx ami-7f1c8d62 ec2-xxx-233-xxx-114.xx-east-1.compute.amazonaws.com ip-xxx-xxx-22-126.sa-east-1.compute.internal
# stopping 44ddddds 0 t2.medium 2015-10-22T08:52:38+0000 sa-east-1c monitoring-disabled xxx.xxx.78.xxx xxx.31.22.xxx
# vpc-0cb7da69 subnet-6b031a2d ebs hvm xen AyziH1445487939809 sg-1f91177a default false
if [[ $size = 23 ]]; then
status=$(ec2-describe-instances --filter "instance-id=$INSTANCE" --region sa-east-1 | grep "INSTANCE" | awk '{print $6}')
fi
echo "-> Status is $status..."
if [[ "$status" = "stopped" ]]; then
end_time=`date +%s`
stop_time="$(($end_time - $start_time))"
echo "[x] Server completely stopped in $stop_time sec"
echo ""
break
fi
sleep 1
done
echo "* Starting instance ${INSTANCE} at $(date)";
start_time=`date +%s`
# Execute the start of the instance again
ec2-start-instances -O ${ACCESS_KEY} -W ${SECRET_KEY} --region sa-east-1 ${INSTANCE}
while [ 1 ]; do
tokens=($(ec2-describe-instances --filter "instance-id=$INSTANCE" --region sa-east-1 | grep "INSTANCE"))
size=${#tokens[@]}
# When the machine is , it will show 23 columns
if [[ $size = 21 ]]; then
status=$(ec2-describe-instances --filter "instance-id=$INSTANCE" --region sa-east-1 | grep "INSTANCE" | awk '{print $5}')
fi
if [[ $size = 23 ]]; then
status=$(ec2-describe-instances --filter "instance-id=$INSTANCE" --region sa-east-1 | grep "INSTANCE" | awk '{print $6}')
fi
echo "-> Status is $status..."
if [[ "$status" = "running" || $size = 23 ]]; then
end_time=`date +%s`
started_time="$(($end_time - $start_time))"
echo "[v] Server completely running again in $started_time sec"
break
fi
sleep 1
done
overallEnd=`date +%s`
overallTime="$(($overallEnd - $overallStart))"
echo ""
echo "====> Complete IP renewal time in $overallTime sec"
echo "";
echo "######################################################";
echo "";
done
root@xxxxxxx:/home/ubuntu# ./renew-amazon-ec2-ips-by-tag.sh
########################################################
IP Renewal Scheduler at Thu Oct 22 09:37:22 UTC 2015
########################################################
=====> There are 2 consumers
i-bb37806a i-b3378062
================================================
* Stopping instance i-bb37806a at Thu Oct 22 09:37:24 UTC 2015
INSTANCE i-bb37806a running stopping
-> Status is stopping...
-> Status is stopping...
-> Status is stopping...
-> Status is stopping...
-> Status is stopping...
-> Status is stopped...
[x] Server completely stopped in 31 sec
* Starting instance i-bb37806a at Thu Oct 22 09:37:55 UTC 2015
INSTANCE i-bb37806a stopped pending
-> Status is pending...
-> Status is pending...
-> Status is pending...
-> Status is pending...
-> Status is running...
[v] Server completely running again in 28 sec
====> Complete IP renewal time in 59 sec
######################################################
* Stopping instance i-b3378062 at Thu Oct 22 09:38:23 UTC 2015
INSTANCE i-b3378062 running stopping
-> Status is stopping...
-> Status is stopping...
-> Status is stopping...
-> Status is stopping...
-> Status is stopped...
[x] Server completely stopped in 26 sec
* Starting instance i-b3378062 at Thu Oct 22 09:38:49 UTC 2015
INSTANCE i-b3378062 stopped pending
-> Status is pending...
-> Status is pending...
-> Status is pending...
-> Status is running...
[v] Server completely running again in 21 sec
====> Complete IP renewal time in 47 sec
######################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment