or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

advanced-configuration.mdconfiguration-loading.mdenvironment-configuration.mdindex.mdload-options.mdregion-configuration.mdshared-config-advanced.mdshared-config.md
tile.json

configuration-loading.mddocs/

Configuration Loading

The configuration loading capability provides the primary LoadDefaultConfig function for loading AWS SDK configuration from all supported sources.

Overview

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.

API

func LoadDefaultConfig(ctx context.Context, optFns ...func(*LoadOptions) error) (cfg aws.Config, err error)

Parameters:

  • ctx - Context for configuration loading operations
  • optFns - Variable number of functional options to customize configuration loading

Returns:

  • cfg - Populated AWS configuration
  • err - Error if configuration loading fails

Configuration Sources

The function loads configuration from these sources in order:

  1. Environment Variables
  2. Shared Configuration files (~/.aws/config)
  3. Shared Credentials files (~/.aws/credentials)

Custom configurations passed via optFns take precedence over default sources.

Usage Examples

Basic Configuration Loading

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)
}

Configuration with Custom Profile

cfg, err := config.LoadDefaultConfig(
    context.TODO(),
    config.WithSharedConfigProfile("production"),
)
if err != nil {
    log.Fatalf("unable to load SDK config, %v", err)
}

Configuration with Explicit Region

cfg, err := config.LoadDefaultConfig(
    context.TODO(),
    config.WithRegion("us-west-2"),
)
if err != nil {
    log.Fatalf("unable to load SDK config, %v", err)
}

Configuration with Custom Credentials

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)
}

Configuration with Multiple Options

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)
}

Configuration with Custom HTTP Client

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)
}

Configuration with Logger

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)
}

Configuration with EC2 IMDS Region

cfg, err := config.LoadDefaultConfig(
    context.TODO(),
    config.WithEC2IMDSRegion(),
)
if err != nil {
    log.Fatalf("unable to load SDK config, %v", err)
}

Configuration with Custom CA Bundle

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)
}

Related

See Load Options and Functional Options for a complete list of available configuration options.