/
session.go
68 lines (55 loc) · 1.43 KB
/
session.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
package session
import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
log "github.com/sirupsen/logrus"
)
// Session is a wrapper around the aws session service
type Session struct {
Session *session.Session
RoleName string
ExternalID string
credentials *credentials.Credentials
region string
}
type SessionOption func(*Session)
// New creates a new AWS session with options
func New(opts ...SessionOption) Session {
log.Info("creating new aws session...")
s := Session{}
for _, opt := range opts {
opt(&s)
}
config := aws.Config{
Credentials: s.credentials,
Region: aws.String(s.region),
}
sess := session.Must(session.NewSession(&config))
s.Session = sess
return s
}
func WithCredentials(key, secret, token string) SessionOption {
return func(s *Session) {
log.Debugf("setting credentials with key id %s", key)
s.credentials = credentials.NewStaticCredentials(key, secret, token)
}
}
func WithRegion(region string) SessionOption {
return func(s *Session) {
log.Debugf("setting region to %s", region)
s.region = region
}
}
func WithExternalID(extId string) SessionOption {
return func(s *Session) {
log.Debugf("setting external ID to %s", extId)
s.ExternalID = extId
}
}
func WithExternalRoleName(role string) SessionOption {
return func(s *Session) {
log.Debugf("setting region to %s", role)
s.RoleName = role
}
}