The configuration loading capability provides the primary LoadDefaultConfig function for loading AWS SDK configuration from all supported sources.
LoadDefaultConfig reads the SDK's default external configurations and populates an AWS Config with values from environment variables, shared configuration files, and shared credentials files. It accepts an optional variadic set of functional options to customize the configuration loading behavior.
func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) (cfg aws.Config, err error)Parameters:
ctx - Context for configuration loading operationsoptFns - Variable number of functional options to customize configuration loadingReturns:
cfg - Populated AWS configurationerr - Error if configuration loading failsThe function loads configuration from these sources in order:
Custom configurations passed via optFns take precedence over default sources.
package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/config"
)
func main() {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
fmt.Printf("Loaded config for region: %s\n", cfg.Region)
}cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithSharedConfigProfile("production"),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion("us-west-2"),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}import (
"github.com/aws/aws-sdk-go-v2/credentials"
)
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithCredentialsProvider(
credentials.NewStaticCredentialsProvider(
"AKID",
"SECRET",
"SESSION",
),
),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithRegion("us-east-1"),
config.WithSharedConfigProfile("development"),
config.WithRetryMaxAttempts(5),
config.WithRetryMode(aws.RetryModeAdaptive),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}import (
"net/http"
"time"
)
customHTTPClient := &http.Client{
Timeout: 30 * time.Second,
}
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithHTTPClient(customHTTPClient),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}import (
"log"
"os"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/smithy-go/logging"
)
logger := logging.NewStandardLogger(os.Stdout)
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithLogger(logger),
config.WithClientLogMode(aws.LogRetries | aws.LogRequest),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSRegion(),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}import (
"os"
)
caBundle, err := os.Open("/path/to/custom-ca-bundle.pem")
if err != nil {
log.Fatalf("unable to open CA bundle, %v", err)
}
defer caBundle.Close()
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithCustomCABundle(caBundle),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}See Load Options and Functional Options for a complete list of available configuration options.