How to get a list of AWS regions programmatically in Go SDK V2

Viewed 944

AWS removed endpoints package from Go SDK V2. Is there a way to get a list of AWS regions using Go SDK V2?

In V1 you could write something like this:

    import "github.com/aws/aws-sdk-go/aws/endpoints"
    ...
    ...
    
        partitions := endpoints.DefaultPartitions()
        for _, p := range partitions {
            for region := range p.Regions() {
                validRegions[region] = struct{}{}
            }
        }
    
    ...
    ...

However, this seems no longer possible. I did notice an autogenerated json which seems to contain all the partitions, however I can't seem to figure out how to get the list of available regions in the code.

Is there a way to do this in Go SDK V2?

3 Answers

One option is to fetch the JSON that you mentioned, something like:

// endpoints holds the aws generated endpoints.json
type endpoints struct {
    Partitions []Partition `json:"partitions"`
}

type Partition struct {
    PartitionName string                 `json:"partitionName"`
    Regions       map[string]interface{} `json:"regions"`
    Services      map[string]Service     `json:"services"`
}

type Service struct {
    Endpoints map[string]interface{} `json:"endpoints"`
}

func main() {

    fmt.Println("Generating AWS regions")

    resp, err := http.Get("https://raw.githubusercontent.com/aws/aws-sdk-go-v2/master/codegen/smithy-aws-go-codegen/src/main/resources/software/amazon/smithy/aws/go/codegen/endpoints.json")
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
    }

    defer resp.Body.Close()

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
    }

    e := endpoints{}
    err = json.Unmarshal(body, &e)
    if err != nil {
        fmt.Fprintln(os.Stderr, err.Error())
    }

    // do something with e
}

There is no way. The new SDK is broken by design and shouldn't be used.

Related