forked from segmentio/chamber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shared.go
41 lines (34 loc) · 957 Bytes
/
shared.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
package store
import (
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
)
const (
RegionEnvVar = "CHAMBER_AWS_REGION"
)
func getSession(numRetries int) (*session.Session, *string) {
var region *string
if regionOverride, ok := os.LookupEnv(RegionEnvVar); ok {
region = aws.String(regionOverride)
}
retSession := session.Must(session.NewSessionWithOptions(
session.Options{
Config: aws.Config{
Region: region,
MaxRetries: aws.Int(numRetries),
},
SharedConfigState: session.SharedConfigEnable,
},
))
// If region is still not set, attempt to determine it via ec2 metadata API
if aws.StringValue(retSession.Config.Region) == "" {
session := session.New()
ec2metadataSvc := ec2metadata.New(session)
if regionOverride, err := ec2metadataSvc.Region(); err == nil {
region = aws.String(regionOverride)
}
}
return retSession, region
}