6

I can't find a way to retrieve the English names of regions with AWS CLI. Any idea?

I need to generate the following table of English names of all AWS regions in command line output in Linux.

'ap-northeast-1' => 'Asia Pacific (Tokyo)',
'ap-southeast-1' => 'Asia Pacific (Singapore)',
'ap-southeast-2' => 'Asia Pacific (Sydney)',
'eu-central-1'   => 'EU (Frankfurt)',
'eu-west-1'      => 'EU (Ireland)',
'sa-east-1'      => 'South America (Sao Paulo)',
'us-east-1'      => 'US East (N. Virginia)',
'us-east-2'      => 'US East (Ohio)',
'us-west-1'      => 'US West (N. California)',
'us-west-2'      => 'US West (Oregon)',
'eu-west-2'      => 'EU (London)',
'ca-central-1'   => 'Canada (Central)',
'sa-east-1'      => 'South America (Sao Paulo)',

I am not admin for the linux VMs and can't install Java.

chriscatfr
  • 2,592
  • 3
  • 24
  • 32
  • 1
    You can check out the answer at https://stackoverflow.com/questions/38020106/how-should-i-get-aws-region-names-using-regions. – junkangli Apr 08 '18 at 09:54
  • thanks @junkangli . `aws lightsail get-regions` is incomplete, many regions are missing and JDK are too much. I am not admin for the linux VMs and can't install Java. – chriscatfr Apr 08 '18 at 10:06
  • 1
    I don't believe there is a command to achieve this. Windows Powershell has the following command to get this: `Get-AWSRegion`. This command does not exist in bash. However, if you are looking to use some of the most frequently regions, you can use the following lightsail command to get the region names: `aws lightsail get-regions --output table --query 'regions[*].{Name:name,EnglishName:displayName}'` – krishna_mee2004 Apr 08 '18 at 14:07
  • 3
    They don't change all that frequently so you could just use a hard-coded list. Or you could scrape them periodically from the table at https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html#concepts-available-regions – jarmod Apr 08 '18 at 17:44

4 Answers4

3

UPDATE: AWS recently revamp their documentation site, which caused this solution to stop working. I've updated the code below to make it work again. Please keep in mind that any changes to structure of the AWS documentation site in the future might result in it not working again. This is simply a workaround.

Based on the @jarmod's comment how about something like this:

Function:

  1. Pull the html page from AWS documentation; pass output to next command.
  2. Remove blank lines from the output.
  3. Match the RegionName code to a line in the html, and pass the next 5 lines to next command.
  4. Find the line with Human readable name of the region.
  5. Print the code and name in one line.

Example Code (BASH):

function getAWSRegionName {
   tmp=$(wget -qO- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.partial.html | awk NF | awk -v pat="<p><code class=\"code\">$1" '$0 ~ pat {for(i=0;i<5;i++) {getline; print}}' | awk -F'[<|>]' '/<p>|<\/p>/ {print $3}'); 
   echo "$1 = $tmp"; 
}

for code in `aws ec2 describe-regions | awk -F'"' '/RegionName/{print $4}'`; do
        getAWSRegionName $code;
done

Result:

code-run-result

Dmitri Sandler
  • 1,123
  • 6
  • 17
3

Also encountered this issue, found this AWS article saying you can query all kind of data using AWS Parameter Store. after digging a little found this solution using AWS CLI

region=us-east-1
aws ssm get-parameter --name /aws/service/global-infrastructure/regions/$region/longName --query "Parameter.Value" --output text
Itay Sued
  • 134
  • 3
  • 1
    can't believe I missed such a useful way of using SSM from APR 2019! Thanks for sharing the right answer at last – chriscatfr Aug 28 '20 at 14:47
2

Even though this is kind of old I think the correct answer for the question which stated the need for generating a table with the regions and their English names without relaying in the documentation format hasn't been addressed correctly. I go with two AWS CLI commands on a loop like this:

#!/bin/bash
fmt="%-16s%-4s%-25s\n"
for i in $(aws ec2 describe-regions --query 'Regions[].RegionName' --output text)
do 
  printf "$fmt" "'$i" "=>" "$(aws ssm get-parameter --name /aws/service/global-infrastructure/regions/$i/longName --query "Parameter.Value" --output text)',"
done
Colin Moreno Burgess
  • 1,432
  • 1
  • 12
  • 17
  • thanks for compiling a working answer. After 25 years of work in IT, I tend to dislike any solution with text and regex or such because they stop working after a while. I prefer JSON output with jq. For proper sanitation. But I'll still mark your answer as the right one because it works (until text format change) – chriscatfr May 01 '22 at 21:26
0

I have a similar issue, solved the issue with the code below:

REGION_TARGET=us-east-1
region_name=$(wget -qO- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-regions-availability-zones.html\#concepts-available-regions | awk NF | awk -v pat="<p><code class=\"code\">$TARGET_REGION" '$0 ~ pat {for(i=0;i<5;i++) {getline; print}}' | awk -F'[<|>]' '/<p>|<\/p>/ {print $3}');
Yaronk
  • 9
  • 3
  • thanks. That's a good answer. But like Dmitri said above "Please keep in mind that any changes to structure of the AWS documentation site in the future might result in it not working again." I think for me the right answer will be the comment from jarmod "They don't change all that frequently so you could just use a hard-coded list.". I used your answers to automatically hardcode the list :) – chriscatfr Apr 13 '20 at 10:59