Does AWS SDK handles session expiry automatically?

Viewed 1023

I am writing a client to get the continuous messages from RabbitMQ and pushing to AWS SQS service. But I'm not sure about the session expiry, If the session expires do we need to recreate the session or AWS SDK handles it automatically?

log.Printf("PPU Message Broker: Pushing messages to SQS")
    sess, err := session.NewSession(&aws.Config{
        Region:      aws.String("us-east-1"),
        //Credentials: credentials.NewSharedCredentials("", "sqs_user"),
    })
    _, err = sess.Config.Credentials.Get()
    if err != nil {
        log.Fatalf("PPU Message Broker: Credentails Failed")
    }
    svc := sqs.New(sess)    
    result, err := svc.SendMessage(&sqs.SendMessageInput{
        MessageBody:    aws.String(string(data)),
        MessageGroupId: aws.String("TestGroup"),
        QueueUrl:       &qURL,
    })
2 Answers

There is a default configuration for session expiration, but you can specify yours:

In addition to NewSession, you can create sessions using NewSessionWithOptions. This function allows you to control and override how the session will be created through code, instead of being driven by environment variables only.

Use NewSessionWithOptions when you want to provide the config profile

Inside of the Options object, there is an attribute for changing the default expiration time, by default is 15 minutes:

// When the SDK's shared config is configured to assume a role this option // may be provided to set the expiry duration of the STS credentials. // Defaults to 15 minutes if not set as documented in the // stscreds.AssumeRoleProvider. AssumeRoleDuration time.Duration

https://docs.aws.amazon.com/sdk-for-go/api/aws/session/

Updated answer:

Just noticed the question is about AWS SDK for Go.

From the AWS SDK for Go documentation: https://docs.aws.amazon.com/sdk-for-go/api/aws/credentials/#Credentials.Get

func (*Credentials) Get

func (c *Credentials) Get() (Value, error)

Get returns the credentials value, or error if the credentials Value failed to be retrieved.

Will return the cached credentials Value if it has not expired. If the credentials Value has expired the Provider's Retrieve() will be called to refresh the credentials.

If Credentials.Expire() was called the credentials Value will be force expired, and the next call to Get() will cause them to be refreshed. 

Original answer:

From AWS Javascript SDK documentation at https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Credentials.html#get-property

get(callback) ⇒ void

Gets the existing credentials, refreshing them if they are not yet loaded or have expired. Users should call this method before using refresh(), as this will not attempt to reload credentials when they are already loaded into the object.
Related