/
session.go
42 lines (39 loc) · 952 Bytes
/
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
//go:generate mockgen -source=${GOFILE} -package=${GOPACKAGE} -destination=${GOPACKAGE}_mock.go
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"
)
type (
Options struct {
Region string
AccessKeyID string
SecretAccessKey string
}
Session interface {
GetSession() *session.Session
}
sessionImpl struct {
awsSession *session.Session
}
)
func New(opts Options) (Session, error) {
credentials := credentials.NewCredentials(
&credentials.StaticProvider{
Value: credentials.Value{
AccessKeyID: opts.AccessKeyID,
SecretAccessKey: opts.SecretAccessKey,
},
},
)
configs := []*aws.Config{
aws.NewConfig().WithCredentials(credentials),
aws.NewConfig().WithRegion(opts.Region),
}
sess, err := session.NewSession(configs...)
if err != nil {
return nil, err
}
return sessionImpl{awsSession: sess}, nil
}