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.

4
  • 1
    You can check out the answer at stackoverflow.com/questions/38020106/….
    – junkangli
    Commented Apr 8, 2018 at 9: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
    Commented Apr 8, 2018 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}' Commented Apr 8, 2018 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 docs.aws.amazon.com/AWSEC2/latest/UserGuide/…
    – jarmod
    Commented Apr 8, 2018 at 17:44

4 Answers 4

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

3
  • 1
    Awesome answer.
    – copper.hat
    Commented Mar 5, 2019 at 5:02
  • This worked for me fine till recently. Looks like AWS changes something on their interface. I get empty result
    – hagits
    Commented Jan 12, 2020 at 17:16
  • The fix stopped working again. It was OK on March 31 2020. Today (April 7th 2020 it does not work). :(
    – hagits
    Commented Apr 7, 2020 at 16:46
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
1
  • 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
    Commented Aug 28, 2020 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
1
  • 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
    Commented May 1, 2022 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}');
1
  • 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
    Commented Apr 13, 2020 at 10:59

Not the answer you're looking for? Browse other questions tagged or ask your own question.