Skip to content

Commit

Permalink
Short timeout on fetch AWS account ID (#1987)
Browse files Browse the repository at this point in the history
  • Loading branch information
nopcoder authored May 24, 2021
1 parent a927343 commit 13de272
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions pkg/cloud/aws/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package aws
import (
"crypto/md5" //nolint:gosec
"fmt"
"net/http"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -22,16 +24,27 @@ func NewMetadataProvider(logger logging.Logger, awsConfig *aws.Config) *Metadata
}

func (m *MetadataProvider) GetMetadata() map[string]string {
sess, err := session.NewSession(m.awsConfig)
// max number of retries on the client operation
const sessionMaxRetries = 0
// use a shorter timeout than default because the service can be inaccessible from networks which don't have internet connection
const sessionTimeout = 5 * time.Second
metaConfig := &aws.Config{
HTTPClient: &http.Client{
Timeout: sessionTimeout,
},
MaxRetries: aws.Int(sessionMaxRetries),
}
sess, err := session.NewSession(m.awsConfig.Copy(metaConfig))
if err != nil {
m.logger.Warnf("%v: failed to create AWS session for BI", err)
m.logger.WithError(err).Warn("Failed to create AWS session for BI")
return nil
}
sess.ClientConfig(s3.ServiceName)

stsClient := sts.New(sess)
identity, err := stsClient.GetCallerIdentity(&sts.GetCallerIdentityInput{})
if err != nil {
m.logger.Warnf("%v: failed to get AWS account ID for BI", err)
m.logger.WithError(err).Warn("%v: failed to get AWS account ID for BI")
return nil
}
return map[string]string{
Expand Down

0 comments on commit 13de272

Please sign in to comment.