RegionsAndZones.go - AWS Code Sample

RegionsAndZones.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "fmt" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) // GetRegions retrieves the AWS Regions where Amazon Elastic Compute Cloud (Amazon EC2) is available. // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // Output: // If success, the list of AWS Regions and nil // Otherwise, nil and an error from the call to DescribeRegions func GetRegions(sess *session.Session) (*ec2.DescribeRegionsOutput, error) { svc := ec2.New(sess) resultRegions, err := svc.DescribeRegions(nil) if err != nil { return nil, err } return resultRegions, nil } // GetZones retrieves the Availability Zones within the current AWS Region. // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // Output: // If success, the list of Availability Zones and nil // Otherwise, nil and an error from the call to func GetZones(sess *session.Session) (*ec2.DescribeAvailabilityZonesOutput, error) { svc := ec2.New(sess) resultAvalZones, err := svc.DescribeAvailabilityZones(nil) if err != nil { return nil, err } return resultAvalZones, nil } func main() { sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) resultRegions, err := GetRegions(sess) if err != nil { fmt.Println("Got an error retrieving the Regions:") fmt.Println(err) return } resultAvalZones, err := GetZones(sess) if err != nil { fmt.Println("Got an error retrieving the Availability Zones:") fmt.Println(err) return } fmt.Println("AWS Regions:", resultRegions.Regions) fmt.Println("") fmt.Println("Zones: ", resultAvalZones.AvailabilityZones) fmt.Println("") fmt.Println("Found", len(resultRegions.Regions), "AWS Regions; found", len(resultAvalZones.AvailabilityZones), "Availability Zones", "in", *sess.Config.Region) }