forked from awsdocs/aws-doc-sdk-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RegionsAndZones.go
82 lines (70 loc) · 2.75 KB
/
RegionsAndZones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
// snippet-start:[ec2.go.regions_and_zones]
package main
// snippet-start:[ec2.go.regions_and_zones.imports]
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
// snippet-end:[ec2.go.regions_and_zones.imports]
// 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) {
// snippet-start:[ec2.go.regions_and_zones.regions]
svc := ec2.New(sess)
resultRegions, err := svc.DescribeRegions(nil)
// snippet-end:[ec2.go.regions_and_zones.regions]
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)
// snippet-start:[ec2.go.regions_and_zones.zones]
resultAvalZones, err := svc.DescribeAvailabilityZones(nil)
// snippet-end:[ec2.go.regions_and_zones.zones]
if err != nil {
return nil, err
}
return resultAvalZones, nil
}
func main() {
// snippet-start:[ec2.go.regions_and_zones.session]
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
// snippet-end:[ec2.go.regions_and_zones.session]
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
}
// snippet-start:[ec2.go.regions_and_zones.display]
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)
// snippet-end:[ec2.go.regions_and_zones.display]
}
// snippet-end:[ec2.go.regions_and_zones]