github.com/mweagle/Sparta@v1.15.0/aws/session.go (about) 1 package aws 2 3 import ( 4 "github.com/aws/aws-sdk-go/aws" 5 "github.com/aws/aws-sdk-go/aws/request" 6 "github.com/aws/aws-sdk-go/aws/session" 7 "github.com/sirupsen/logrus" 8 ) 9 10 type logrusProxy struct { 11 logger *logrus.Logger 12 } 13 14 // Log is a utility function to comply with the AWS signature 15 func (proxy *logrusProxy) Log(args ...interface{}) { 16 proxy.logger.Info(args...) 17 } 18 19 // NewSessionWithConfig returns an awsSession that includes the user supplied 20 // configuration information 21 func NewSessionWithConfig(awsConfig *aws.Config, logger *logrus.Logger) *session.Session { 22 return NewSessionWithConfigLevel(awsConfig, aws.LogDebugWithRequestErrors, logger) 23 } 24 25 // NewSession that attaches a debug level handler to all AWS requests from services 26 // sharing the session value. 27 func NewSession(logger *logrus.Logger) *session.Session { 28 return NewSessionWithLevel(aws.LogDebugWithRequestErrors, logger) 29 } 30 31 // NewSessionWithLevel returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration) 32 // object that attaches a debug level handler to all AWS requests from services 33 // sharing the session value. 34 func NewSessionWithLevel(level aws.LogLevelType, logger *logrus.Logger) *session.Session { 35 awsConfig := &aws.Config{ 36 CredentialsChainVerboseErrors: aws.Bool(true), 37 } 38 return NewSessionWithConfigLevel(awsConfig, level, logger) 39 } 40 41 // NewSessionWithConfigLevel returns an AWS Session (https://github.com/aws/aws-sdk-go/wiki/Getting-Started-Configuration) 42 // object that attaches a debug level handler to all AWS requests from services 43 // sharing the session value. 44 func NewSessionWithConfigLevel(awsConfig *aws.Config, 45 level aws.LogLevelType, 46 logger *logrus.Logger) *session.Session { 47 if nil == awsConfig { 48 awsConfig = &aws.Config{ 49 CredentialsChainVerboseErrors: aws.Bool(true), 50 } 51 } 52 53 // Log AWS calls if needed 54 switch logger.Level { 55 case logrus.DebugLevel: 56 awsConfig.LogLevel = aws.LogLevel(level) 57 } 58 awsConfig.Logger = &logrusProxy{logger} 59 sess, sessErr := session.NewSession(awsConfig) 60 if sessErr != nil { 61 logger.WithField("Error", sessErr).Warn("Failed to create AWS Session") 62 } else { 63 sess.Handlers.Send.PushFront(func(r *request.Request) { 64 logger.WithFields(logrus.Fields{ 65 "Service": r.ClientInfo.ServiceName, 66 "Operation": r.Operation.Name, 67 "Method": r.Operation.HTTPMethod, 68 "Path": r.Operation.HTTPPath, 69 "Payload": r.Params, 70 }).Debug("AWS Request") 71 }) 72 } 73 74 logger.WithFields(logrus.Fields{ 75 "Name": aws.SDKName, 76 "Version": aws.SDKVersion, 77 }).Debug("AWS SDK Info") 78 return sess 79 }