Finding AWS region closest to your server with a shell script

in Cloud & Serverless


For something more sophisticated and less manual, use ekalinin/awsping.

The below script will ping every region (or a subset of regions) and output the latency.

Here's the full list of AWS regions as of November 2022:

  • Africa (Cape Town): af-south-1
  • Asia Pacific (Hong Kong): ap-east-1
  • Asia Pacific (Tokyo): ap-northeast-1
  • Asia Pacific (Seoul): ap-northeast-2
  • Asia Pacific (Osaka): ap-northeast-3
  • Asia Pacific (Mumbai): ap-south-1
  • Asia Pacific (Singapore): ap-southeast-1
  • Asia Pacific (Sydney): ap-southeast-2
  • Asia Pacific (Jakarta): ap-southeast-3
  • Canada (Central): ca-central-1
  • Europe (Frankfurt): eu-central-1
  • Europe (Zurich): eu-central-2
  • Europe (Stockholm): eu-north-1
  • Europe (Milan): eu-south-1
  • Europe (Spain): eu-south-2
  • Europe (Ireland): eu-west-1
  • Europe (London): eu-west-2
  • Europe (Paris): eu-west-3
  • Middle East (UAE): me-central-1
  • Middle East (Bahrain): me-south-1
  • South America (São Paulo): sa-east-1
  • US East (N. Virginia): us-east-1
  • US East (Ohio): us-east-2
  • US West (N. California): us-west-1
  • US West (Oregon): us-west-2
  • AWS GovCloud (US-East): us-gov-east-1
  • AWS GovCloud (US-West): us-gov-west-1

And here's a quick POSIX/BASH script to ping all (or some) of them:

#!/bin/sh

for region in af-south-1 ap-east-1 ap-northeast-1 ap-northeast-2 ap-northeast-3 ap-south-1 ap-southeast-1 ap-southeast-2 ap-southeast-3 ca-central-1 eu-central-1 eu-central-2 eu-north-1 eu-south-1 eu-south-2 eu-west-1 eu-west-2 eu-west-3 me-central-1 me-south-1 sa-east-1 us-east-1 us-east-2 us-west-1 us-west-2 us-gov-east-1 us-gov-west-1; do
    if [ -n "${1}" ]; then
        case ${region} in
            ${1}*)
                ;;
            *)
                continue
                ;;
        esac
    fi
    printf "PINGING ${region}... "
    MS=$(ping -c 8 -q "lambda.${region}.amazonaws.com" | awk -F '/' 'END {print $5}')
    printf "$MS\n"
done

usage:

  • sh aws.sh to ping all regions
  • sh aws.sh abc-def to ping only regions starting with abc-def, e.g. sh aws.sh eu or even sh aws.sh eu-west
#aws #shell-script