-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaws_helper.go
More file actions
43 lines (36 loc) · 1.04 KB
/
aws_helper.go
File metadata and controls
43 lines (36 loc) · 1.04 KB
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
package main
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"os"
)
func GetSession() *session.Session {
accessKey := os.Getenv("AWS_ACCESS_KEY_ID")
secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
sessionToken := os.Getenv("AWS_SESSION_TOKEN")
region := os.Getenv("AWS_REGION")
config := GetConfig(region, accessKey, secret, sessionToken)
session, err := session.NewSession(config)
if err != nil {
return nil
}
return session
}
func GetConfig(region, accessKey, secret, sessionToken string) *aws.Config {
if accessKey == "" {
return ConfigWithIAM(region)
}
return ConfigWithAccessKey(region, accessKey, secret, sessionToken)
}
func ConfigWithAccessKey(region, accessKey, secret, sessionToken string) *aws.Config {
return &aws.Config{
Region: aws.String(region),
Credentials: credentials.NewStaticCredentials(accessKey, secret, sessionToken),
}
}
func ConfigWithIAM(region string) *aws.Config {
return &aws.Config{
Region: aws.String(region),
}
}