Skip to content

Instantly share code, notes, and snippets.

@brunoaduarte
Created May 3, 2025 15:24
Show Gist options
  • Save brunoaduarte/cfbbd0880357201480d4af0e7fec17f3 to your computer and use it in GitHub Desktop.
Save brunoaduarte/cfbbd0880357201480d4af0e7fec17f3 to your computer and use it in GitHub Desktop.
PowerShell script to measure latency (by opening a TCP connection since ICMP is blocked) to all america nearest AWS regions.
# List of AWS regions
$regions = @(
"us-east-1","us-east-2","us-west-1","us-west-2",
"ca-central-1",
"eu-west-1","eu-west-2","eu-west-3","eu-central-1","eu-north-1","eu-south-1",
"ap-east-1","ap-south-1","ap-northeast-1","ap-northeast-2","ap-northeast-3",
"ap-southeast-1","ap-southeast-2",
"sa-east-1",
"me-south-1","af-south-1"
)
# Measure and collect latencies
$results = foreach ($r in $regions) {
$endpoint = "ec2.$r.amazonaws.com"
$sw = [Diagnostics.Stopwatch]::StartNew()
try {
$tcp = [Net.Sockets.TcpClient]::new()
$tcp.Connect($endpoint, 443)
$tcp.Close()
} catch {
# ignore errors
}
$sw.Stop()
[PSCustomObject]@{
Region = $r
Endpoint = $endpoint
LatencyMs = [math]::Round($sw.Elapsed.TotalMilliseconds,2)
}
}
# Show the top 5 fastest regions
$results |
Sort-Object LatencyMs |
Select-Object -First 5 |
Format-Table Region, @{n='Latency (ms)';e={$_.LatencyMs}} -AutoSize
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment