Is there an Amazon API to get Nice Region names rather than us-west1?

Issue

I’m using the following API to retrieve a list of amazon regions.

However, it basically returns the regions as "us-west1, us-west2" etc. Is there a way to get the region name from the API with output such as "US West (N. California)", "US West (Oregon)" ?

    // Get a list of regions from our default region
    svc := ec2.NewFromConfig(cfg)
    result, err := svc.DescribeRegions(context.TODO(), &ec2.DescribeRegionsInput{})
    if err != nil {
        return nil, err
    }

    var regions []portaineree.Pair
    for _, region := range result.Regions {
        fmt.Println("region.Name=", *region.RegionName)
       // do something with region...
    }

Solution

You can use the SSM Agent to both get the list of regions, and pull out the long name for each region:

package main

import (
    "log"
    "strings"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/ssm"
)

func main() {
    // Build a AWS SSM Agent
    sess := session.Must(session.NewSessionWithOptions(session.Options{
        SharedConfigState: session.SharedConfigEnable,
    }))

    // We're requesting global data, the region doesn't matter
    svc := ssm.New(sess, &aws.Config{Region: aws.String("us-east-1")})
    var nextToken *string
    for {
        // Request all regions, paginating the results if needed
        var input = &ssm.GetParametersByPathInput{
            Path:      aws.String("/aws/service/global-infrastructure/regions"),
            NextToken: nextToken,
        }
        var output, err = svc.GetParametersByPath(input)
        if err != nil {
            log.Fatal(err)
        }

        // For each region, get the "longName" for the region
        for _, element := range output.Parameters {
            region := (*element.Name)[strings.LastIndex(*element.Name, "/")+1:]

            var regionInfo, err = svc.GetParameter(&ssm.GetParameterInput{
                Name: aws.String("/aws/service/global-infrastructure/regions/" + region + "/longName"),
            })
            if err != nil {
                log.Fatal(err)
            }

            regionDesc := *regionInfo.Parameter.Value

            // Just output the region and region description
            log.Println(region, " = ", regionDesc)
        }

        // Pull in the next page of regions if needed
        nextToken = output.NextToken
        if nextToken == nil {
            break
        }
    }
}

Answered By – Anon Coward

Answer Checked By – Gilberto Lyons (GoLangFix Admin)

Leave a Reply

Your email address will not be published.