Load options provide fine-grained control over AWS SDK configuration loading behavior through functional options pattern.
The LoadOptions struct contains all configurable options for LoadDefaultConfig. Functional options (functions prefixed with With) provide a convenient way to set these options.
type LoadOptions struct {
Region string
Credentials aws.CredentialsProvider
BearerAuthTokenProvider smithybearer.TokenProvider
HTTPClient HTTPClient
EndpointResolver aws.EndpointResolver
EndpointResolverWithOptions aws.EndpointResolverWithOptions
RetryMaxAttempts int
RetryMode aws.RetryMode
Retryer func() aws.Retryer
APIOptions []func(*middleware.Stack) error
Logger logging.Logger
ClientLogMode *aws.ClientLogMode
SharedConfigProfile string
SharedConfigFiles []string
SharedCredentialsFiles []string
CustomCABundle io.Reader
DefaultRegion string
UseEC2IMDSRegion *UseEC2IMDSRegion
CredentialsCacheOptions func(*aws.CredentialsCacheOptions)
BearerAuthTokenCacheOptions func(*smithybearer.TokenCacheOptions)
SSOTokenProviderOptions func(*ssocreds.SSOTokenProviderOptions)
ProcessCredentialOptions func(*processcreds.Options)
EC2RoleCredentialOptions func(*ec2rolecreds.Options)
EndpointCredentialOptions func(*endpointcreds.Options)
WebIdentityRoleCredentialOptions func(*stscreds.WebIdentityRoleOptions)
AssumeRoleCredentialOptions func(*stscreds.AssumeRoleOptions)
SSOProviderOptions func(*ssocreds.Options)
LogConfigurationWarnings *bool
S3UseARNRegion *bool
S3DisableMultiRegionAccessPoints *bool
EnableEndpointDiscovery aws.EndpointDiscoveryEnableState
EC2IMDSClientEnableState imds.ClientEnableState
EC2IMDSEndpointMode imds.EndpointModeState
EC2IMDSEndpoint string
UseDualStackEndpoint aws.DualStackEndpointState
UseFIPSEndpoint aws.FIPSEndpointState
DefaultsModeOptions DefaultsModeOptions
AppID string
DisableRequestCompression *bool
RequestMinCompressSizeBytes *int64
S3DisableExpressAuth *bool
AccountIDEndpointMode aws.AccountIDEndpointMode
}
type LoadOptionsFunc func(*LoadOptions) errorfunc WithRegion(v string) LoadOptionsFunc
func WithDefaultRegion(v string) LoadOptionsFunc
func WithEC2IMDSRegion(fnOpts ...func(o *UseEC2IMDSRegion)) LoadOptionsFuncWithRegion sets the AWS region for requests. This takes precedence over all other region sources.
WithDefaultRegion sets a fallback region used if a region is not resolved from other sources.
WithEC2IMDSRegion enables resolving the region from EC2 Instance Metadata Service.
// Explicit region
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion("us-west-2"),
)
// Fallback region
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithDefaultRegion("us-east-1"),
)
// EC2 IMDS region
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSRegion(),
)func WithCredentialsProvider(v aws.CredentialsProvider) LoadOptionsFunc
func WithCredentialsCacheOptions(v func(*aws.CredentialsCacheOptions)) LoadOptionsFunc
func WithProcessCredentialOptions(v func(*processcreds.Options)) LoadOptionsFunc
func WithEC2RoleCredentialOptions(v func(*ec2rolecreds.Options)) LoadOptionsFunc
func WithEndpointCredentialOptions(v func(*endpointcreds.Options)) LoadOptionsFunc
func WithWebIdentityRoleCredentialOptions(v func(*stscreds.WebIdentityRoleOptions)) LoadOptionsFunc
func WithAssumeRoleCredentialOptions(v func(*stscreds.AssumeRoleOptions)) LoadOptionsFunc
func WithSSOProviderOptions(v func(*ssocreds.Options)) LoadOptionsFunc
func WithSSOTokenProviderOptions(v func(*ssocreds.SSOTokenProviderOptions)) LoadOptionsFunc
func WithBearerAuthTokenProvider(v smithybearer.TokenProvider) LoadOptionsFunc
func WithBearerAuthTokenCacheOptions(v func(*smithybearer.TokenCacheOptions)) LoadOptionsFuncimport (
"github.com/aws/aws-sdk-go-v2/credentials"
)
// Static credentials
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider("AKID", "SECRET", "SESSION"),
),
)
// Configure credentials cache
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithCredentialsCacheOptions(func(options *aws.CredentialsCacheOptions) {
options.ExpiryWindow = 30 * time.Second
}),
)
// Configure assume role options
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithAssumeRoleCredentialOptions(func(options *stscreds.AssumeRoleOptions) {
options.Duration = 3600 * time.Second
}),
)func WithSharedConfigProfile(v string) LoadOptionsFunc
func WithSharedConfigFiles(v []string) LoadOptionsFunc
func WithSharedCredentialsFiles(v []string) LoadOptionsFunc// Use specific profile
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigProfile("production"),
)
// Custom config file
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigFiles([]string{"/path/to/custom/config"}),
)
// Custom credentials file
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedCredentialsFiles([]string{"/path/to/custom/credentials"}),
)func WithHTTPClient(v HTTPClient) LoadOptionsFunc
func WithCustomCABundle(v io.Reader) LoadOptionsFuncimport (
"net/http"
"time"
"os"
)
// Custom HTTP client
customClient := &http.Client{
Timeout: 30 * time.Second,
}
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithHTTPClient(customClient),
)
// Custom CA bundle
caBundle, _ := os.Open("/path/to/ca-bundle.pem")
defer caBundle.Close()
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithCustomCABundle(caBundle),
)func WithRetryMaxAttempts(v int) LoadOptionsFunc
func WithRetryMode(v aws.RetryMode) LoadOptionsFunc
func WithRetryer(v func() aws.Retryer) LoadOptionsFunc// Set max retry attempts
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRetryMaxAttempts(5),
)
// Set retry mode
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRetryMode(aws.RetryModeAdaptive),
)func WithLogger(v logging.Logger) LoadOptionsFunc
func WithClientLogMode(v aws.ClientLogMode) LoadOptionsFunc
func WithLogConfigurationWarnings(v bool) LoadOptionsFuncimport (
"os"
"github.com/aws/smithy-go/logging"
)
// Configure logger
logger := logging.NewStandardLogger(os.Stdout)
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithLogger(logger),
config.WithClientLogMode(aws.LogRetries | aws.LogRequest | aws.LogResponse),
)
// Enable configuration warnings
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithLogConfigurationWarnings(true),
)func WithEndpointResolver(v aws.EndpointResolver) LoadOptionsFunc
func WithEndpointResolverWithOptions(v aws.EndpointResolverWithOptions) LoadOptionsFunc
func WithUseDualStackEndpoint(v aws.DualStackEndpointState) LoadOptionsFunc
func WithUseFIPSEndpoint(v aws.FIPSEndpointState) LoadOptionsFunc
func WithEndpointDiscovery(v aws.EndpointDiscoveryEnableState) LoadOptionsFuncNote: WithEndpointResolver and WithEndpointResolverWithOptions are deprecated. Use service-specific endpoint resolution instead.
// Enable dual-stack endpoints
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithUseDualStackEndpoint(aws.DualStackEndpointStateEnabled),
)
// Enable FIPS endpoints
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithUseFIPSEndpoint(aws.FIPSEndpointStateEnabled),
)
// Enable endpoint discovery
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEndpointDiscovery(aws.EndpointDiscoveryEnabled),
)func WithEC2IMDSClientEnableState(v imds.ClientEnableState) LoadOptionsFunc
func WithEC2IMDSEndpointMode(v imds.EndpointModeState) LoadOptionsFunc
func WithEC2IMDSEndpoint(v string) LoadOptionsFuncimport (
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
)
// Enable EC2 IMDS client
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSClientEnableState(imds.ClientEnabled),
)
// Set IMDS endpoint mode to IPv6
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSEndpointMode(imds.EndpointModeStateIPv6),
)
// Custom IMDS endpoint
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSEndpoint("http://169.254.169.254"),
)func WithS3UseARNRegion(v bool) LoadOptionsFunc
func WithS3DisableMultiRegionAccessPoints(v bool) LoadOptionsFunc
func WithS3DisableExpressAuth(v bool) LoadOptionsFunc// Allow S3 ARNs to direct region
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithS3UseARNRegion(true),
)
// Disable S3 multi-region access points
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithS3DisableMultiRegionAccessPoints(true),
)
// Disable S3 Express auth
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithS3DisableExpressAuth(true),
)func WithDefaultsMode(mode aws.DefaultsMode, optFns ...func(options *DefaultsModeOptions)) LoadOptionsFunc// Set defaults mode to standard
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithDefaultsMode(aws.DefaultsModeStandard),
)
// Set defaults mode to auto with custom IMDS client
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithDefaultsMode(aws.DefaultsModeAuto, func(options *config.DefaultsModeOptions) {
options.IMDSClient = imds.New(imds.Options{})
}),
)func WithDisableRequestCompression(DisableRequestCompression *bool) LoadOptionsFunc
func WithRequestMinCompressSizeBytes(RequestMinCompressSizeBytes *int64) LoadOptionsFunc// Disable request compression
disableCompression := true
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithDisableRequestCompression(&disableCompression),
)
// Set minimum compression size
minSize := int64(10240) // 10KB
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRequestMinCompressSizeBytes(&minSize),
)func WithAPIOptions(v []func(*middleware.Stack) error) LoadOptionsFunc
func WithAppID(ID string) LoadOptionsFunc
func WithAccountIDEndpointMode(m aws.AccountIDEndpointMode) LoadOptionsFuncimport (
"github.com/aws/smithy-go/middleware"
)
// Add API middleware
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithAPIOptions([]func(*middleware.Stack) error{
func(stack *middleware.Stack) error {
// Add custom middleware
return nil
},
}),
)
// Set SDK app ID
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithAppID("my-application-id"),
)
// Set account ID endpoint mode
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithAccountIDEndpointMode(aws.AccountIDEndpointModeRequired),
)func (o LoadOptions) GetRetryMaxAttempts(ctx context.Context) (int, bool, error)
func (o LoadOptions) GetRetryMode(ctx context.Context) (aws.RetryMode, bool, error)
func (o LoadOptions) GetS3DisableExpressAuth() (value, ok bool)
func (o LoadOptions) GetS3DisableMultiRegionAccessPoints(ctx context.Context) (v bool, found bool, err error)
func (o LoadOptions) GetS3UseARNRegion(ctx context.Context) (v bool, found bool, err error)
func (o LoadOptions) GetUseDualStackEndpoint(ctx context.Context) (value aws.DualStackEndpointState, found bool, err error)
func (o LoadOptions) GetUseFIPSEndpoint(ctx context.Context) (value aws.FIPSEndpointState, found bool, err error)
func (o LoadOptions) GetEnableEndpointDiscovery(ctx context.Context) (value aws.EndpointDiscoveryEnableState, ok bool, err error)
func (o LoadOptions) GetEC2IMDSClientEnableState() (imds.ClientEnableState, bool, error)
func (o LoadOptions) GetEC2IMDSEndpointMode() (imds.EndpointModeState, bool, error)
func (o LoadOptions) GetEC2IMDSEndpoint() (string, bool, error)