-
Notifications
You must be signed in to change notification settings - Fork 9
/
regions.go
55 lines (46 loc) · 1.35 KB
/
regions.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
package helpers
import (
"fmt"
"log"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ec2"
)
var allRegions []string
// GetAllAwsRegions will retrieve all aws regions
//
func GetAllAwsRegions() ([]string, error) {
// log.Println("Getting all regions...")
request := &ec2.DescribeRegionsInput{}
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
awsRegion = "us-east-1"
}
config := aws.NewConfig().WithRegion(awsRegion)
sess, err := session.NewSession(config)
if err != nil {
return allRegions, fmt.Errorf("error starting a new AWS session: %v", err)
}
client := ec2.New(sess, config)
response, err := client.DescribeRegions(request)
if err != nil {
return allRegions, fmt.Errorf("got an error while querying for valid regions (verify your AWS credentials?): %v", err)
}
for _, region := range response.Regions {
allRegions = append(allRegions, *region.RegionName)
}
return allRegions, nil
}
// GetDefaultAwsRegion resolve default region
//
func GetDefaultAwsRegion() (region string) {
region = os.Getenv("AWS_REGION")
if region == "" {
log.Println("No region is set in environment, please set AWS_REGION environment variable or pass --region")
os.Exit(1)
} else {
log.Printf("Using default region %s from environment variable AWS_REGION", region)
}
return region
}