Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Testing latency between regions.
#!/bin/bash
#
# Usage:
#
# 1. Set SECURITY_GROUP and KEY_NAME to your value
# 2. source ec2-ping-test.sh
# 3. launch_all_instances
# 4. terminate_all_instances
#
AWS_REGIONS=(
'us-east-1'
'us-west-1'
'us-west-2'
'eu-west-1'
'eu-central-1'
'ap-northeast-1'
'ap-northeast-2'
'ap-southeast-1'
'ap-southeast-2'
'sa-east-1'
)
# 099720109477/ubuntu/images/hvm-ssd/ubuntu-xenial-16.04-amd64-server-20160420.3
AWS_AMIS=(
'ami-840910ee'
'ami-d8e996b8'
'ami-fa82739a'
'ami-3079f543'
'ami-f0eb089f'
'ami-3c5f4152'
'ami-ea488084'
'ami-fbe83c98'
'ami-a5416cc6'
'ami-567cf23a'
)
# Set these to YOUR values before running the test.
INSTANCE_TYPE="YOUR CHOICE OF INSTANCE TYPE"
SECURITY_GROUP="YOUR SECURITY GROUP NAME"
KEY_NAME="YOUR KEYPAIR NAME"
# This tag is for finding and terminating all instances.
TAG_KEY=Name
TAG_VALUE=aws-inter-region-latency-test
function get_zone_name() {
local region=$1
aws ec2 describe-availability-zones \
--region $region \
--query AvailabilityZones[0].ZoneName \
--output text
}
function run_instance() {
local region=$1
local ami=$2
local params="
--region $region
--security-groups $SECURITY_GROUP
--image-id $ami
--instance-type $INSTANCE_TYPE
--key-name $KEY_NAME
--output json
"
local output=$(aws ec2 run-instances $params)
local instance_id=$(echo "$output" | jq .Instances[0].InstanceId -r)
echo $instance_id
}
function terminate_instances() {
local region=$1
local key=$2
local value=$3
local params="
--region $region
--filters
"Name=instance-state-name,Values=running"
"Name=tag:$key,Values=$value"
--output json
"
local output=$(aws ec2 describe-instances $params)
local instance_ids=$(echo "$output" | jq .Reservations[].Instances[].InstanceId -r)
# use sed to trim spaces
local count=$(echo $instance_ids | wc -w | sed -e 's/^[ \t]*//')
if [ "$count" -gt "0" ]
then
echo "The following instances will be terminated:"
for i in $instance_ids;
do
echo -e "\033[92m$i\033[0m"
done
echo "Terminating $count instances."
output=$(aws ec2 terminate-instances \
--region $region \
--instance-ids $instance_ids \
--output json)
else
echo "No instance to be terminated."
fi
}
function tag_instance() {
local region=$1
local instance_id=$2
local key=$3
local value=$4
local params="
--region $region
--resources $instance_id
--tags
Key=$key,Value=$value
"
aws ec2 create-tags $params
}
function launch_all_instances() {
for i in "${!AWS_REGIONS[@]}"; do
local region=${AWS_REGIONS[$i]}
local ami=${AWS_AMIS[$i]}
echo -e "Launching EC2 in \033[92m$region\033[0m region with AMI \033[92m$ami\033[0m ..."
local instance_id=$(run_instance $region $ami)
echo -e "Instance \033[92m$instance_id\033[0m launched."
echo "Tagging the instance ..."
tag_instance $region $instance_id $TAG_KEY $TAG_VALUE
echo "Instance tagged successfully."
done
}
function terminate_all_instances() {
for i in "${!AWS_REGIONS[@]}"; do
local region=${AWS_REGIONS[$i]}
local ami=${AWS_AMIS[$i]}
echo -e "Finding EC2 to terminate in \033[92m$region\033[0m region ..."
terminate_instances $region $TAG_KEY $TAG_VALUE
done
}
EC2_INSTANCES=(
'ec2-54-210-101-4.compute-1.amazonaws.com'
'ec2-52-53-201-114.us-west-1.compute.amazonaws.com'
'ec2-52-36-149-26.us-west-2.compute.amazonaws.com'
'ec2-52-51-100-72.eu-west-1.compute.amazonaws.com'
'ec2-52-58-166-120.eu-central-1.compute.amazonaws.com'
'ec2-52-68-187-137.ap-northeast-1.compute.amazonaws.com'
'ec2-52-79-176-113.ap-northeast-2.compute.amazonaws.com'
'ec2-52-77-220-235.ap-southeast-1.compute.amazonaws.com'
'ec2-52-62-74-45.ap-southeast-2.compute.amazonaws.com'
'ec2-52-67-9-167.sa-east-1.compute.amazonaws.com'
)
function ping_all() {
for instance in "${EC2_INSTANCES[@]}"; do
ping -q -c 60 $instance
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment