Skip to content
Merged
20 changes: 5 additions & 15 deletions aws/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,13 @@ import (
//
// This means we can change our sessions to be `session.New(<whatver>, session.NewDefaultConfig())
// If we need to override it we can swap order (last config's value wins)
func NewDefaultConfig() *aws.Config {
func NewDefaultConfig(region string) *aws.Config {
return &aws.Config{
HTTPClient: httpx.NewDefaultClient(),
CredentialsChainVerboseErrors: aws.Bool(true),
Region: aws.String(region),
HTTPClient: httpx.NewDefaultClient(),
Retryer: &client.DefaultRetryer{
NumMaxRetries: 10,
MaxThrottleDelay: 1500 * time.Millisecond,
},
}
}

// NewDefaultConfigWithRegion returns the same config as NewDefaultConfig, but allows a user to specify a region as well
func NewDefaultConfigWithRegion(region string) *aws.Config {
return &aws.Config{
Region: aws.String(region),
HTTPClient: httpx.NewDefaultClient(),
Retryer: &client.DefaultRetryer{
NumMaxRetries: 10,
NumMaxRetries: 3,
MaxThrottleDelay: 1500 * time.Millisecond,
},
}
Expand Down
77 changes: 40 additions & 37 deletions aws/session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,89 +5,92 @@ import (
"sync"

"github.com/aws/aws-sdk-go/aws/session"
log "github.com/sirupsen/logrus"

"github.com/disneystreaming/ssm-helpers/aws/config"
)

// NewPoolSafe is used to create a pool of AWS sessions with different profile/region permutations
func NewPoolSafe(profiles []string, regions []string) (allSessions *PoolSafe) {
func NewPoolSafe(profiles []string, regions []string, logger *log.Logger) (allSessions *PoolSafe) {

wg := sync.WaitGroup{}
sp := &PoolSafe{
Sessions: make(map[string]*Pool),
}

if regions != nil {
wg.Add(len(profiles) * len(regions))
if len(regions) == 0 {
for _, p := range profiles {
for _, r := range regions {
// Wait until we have the session for each permutation of profiles and regions
go func(p string, r string) {
defer wg.Done()

newSession := newSession(p, r)
sp.Lock()
s, err := newSession(p, r)
if err != nil {
logger.Fatalf("Error when trying to create session:\n%v", err)
}

if err := validateSessionCreds(s); s != nil {
logger.Fatal(err)
}

session := Pool{
Session: newSession,
Logger: logger,
ProfileName: p,
Session: s,
}
sp.Sessions[fmt.Sprintf("%s-%s", p, r)] = &session
//sp.Sessions = append(sp.Sessions, &session)
defer sp.Unlock()
}(p, r)
}
}
} else {
wg.Add(len(profiles))
for _, p := range profiles {
// Wait until we have the session for each profile
go func(p string) {
defer wg.Done()

newSession := newSession(p, "")
sp.Lock()
s, err := newSession(p, "")
if err != nil {
logger.Fatalf("Error when trying to create session:\n%v", err)
}

if err := validateSessionCreds(s); s != nil {
logger.Fatal(err)
}

session := Pool{
Session: newSession,
Logger: logger,
ProfileName: p,
Session: s,
}
sp.Sessions[fmt.Sprintf("%s", p)] = &session

//sp.Sessions = append(sp.Sessions, &session)
defer sp.Unlock()
}(p)
}
}

// Wait until all sessions have been initialized
wg.Wait()

return sp
}

// createSession uses a given profile and region to call NewSessionWithOptions() to initialize an instance of the AWS client with the given settings.
func validateSessionCreds(session *session.Session) (err error) {
creds := session.Config.Credentials
if _, err := creds.Get(); err != nil {
return fmt.Errorf("Error when validating credentials:\n%v", err)
}

return nil
}

// newSession uses a given profile and region to call NewSessionWithOptions() to initialize an instance of the AWS client with the given settings.
// If the region is nil, it defaults to the default region in the ~/.aws/config file or the AWS_REGION environment variable.
func newSession(profile string, region string) (newSession *session.Session) {
func newSession(profile string, region string) (newSession *session.Session, err error) {
// Create AWS session from shared config
// This will import the AWS_PROFILE envvar from your console, if set
if region != "" {
newSession = session.Must(
session.NewSessionWithOptions(
session.Options{
Config: *config.NewDefaultConfigWithRegion(region),
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
}))
} else {
newSession = session.Must(
session.NewSessionWithOptions(
session.Options{
Config: *config.NewDefaultConfig(),
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
}))
}

return newSession
return session.NewSessionWithOptions(
session.Options{
Config: *config.NewDefaultConfig(region),
Profile: profile,
SharedConfigState: session.SharedConfigEnable,
})
}
2 changes: 2 additions & 0 deletions aws/session/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ import (
"sync"

"github.com/aws/aws-sdk-go/aws/session"
"github.com/sirupsen/logrus"
)

// Pool is a type that holds an instance of an AWS session as well as the profile name used to initialize it
type Pool struct {
Logger *logrus.Logger
Session *session.Session
ProfileName string
}
Expand Down
Loading