Search code examples
amazon-web-servicesgoaws-sdkretry-logicaws-config

Can we configure retry attempts on AWS Go SDK service calls


AWS by default provides retries support on its service calls, which is usually set to a max of 3 attempts.

Can we configure the retry object to set retry attempts to 5?


Solution

  • Yes, AWS provides support to configure their retry and timeouts features. Here are two ways to increase the max number of retries to 5 in AWS Golang SDK v2:

    1. Configure the retry logic on the AWS Config object cfg and it can be used with various AWS service clients using NewFromConfig function
    cfg, err := config.LoadDefaultConfig(context.TODO(), config.WithRetryer(func() aws.Retryer {
        return retry.AddWithMaxAttempts(retry.NewStandard(), 5)
    }))
    
    client := s3.NewFromConfig(cfg)
    
    1. Configure the retry logic only for a specific AWS service client
    customRetry := retry.NewStandard(func(o *retry.StandardOptions) {
            o.MaxAttempts = 5
        })
    
    sqsClient := sqs.NewFromConfig(creds,
        func(o *sqs.Options) {
            o.Retryer = customRetry
        },
    )
    

    More info can be found at https://aws.github.io/aws-sdk-go-v2/docs/configuring-sdk/retries-timeouts/ and https://pkg.go.dev/github.com/aws/aws-sdk-go-v2/aws/retry#hdr-Standard