awsopt

awsopt configures the aws-sdk-go-v2 library consistently across multiple AWS service clients.

Part of nurago, a collection of independent Go packages for backend services.

import "github.com/tecnickcom/nurago/pkg/awsopt"

Package awsopt configures the aws-sdk-go-v2 library consistently across multiple AWS service clients. It centralizes config.LoadOptionsFunc calls into a composable Options slice that can be built once and handed to any AWS-based package in this library.

How It Works

Options is a typed slice of config.LoadOptionsFunc values. Options are accumulated with method calls and then materialized into an aws.Config via Options.LoadDefaultConfig, which delegates to the standard config.LoadDefaultConfig from the SDK:

  1. Create an Options value (zero value is ready to use).
  2. Append options with Options.WithAWSOption, Options.WithRegion, or Options.WithRegionFromURL.
  3. Call Options.LoadDefaultConfig to obtain an aws.Config that can be passed directly to any aws-sdk-go-v2 service constructor.

Integrated Packages

awsopt is used as the AWS configuration layer by:

Usage

var opts awsopt.Options
opts.WithRegionFromURL("https://s3.eu-west-1.amazonaws.com", "")

awsCfg, err := opts.LoadDefaultConfig(ctx)
if err != nil {
    return err
}

// awsCfg is ready for any aws-sdk-go-v2 service client:
// s3.NewFromConfig(awsCfg), secretsmanager.NewFromConfig(awsCfg), …

When consuming an awsopt-based package, pass additional options via the package’s own WithAWSOptions helper so all AWS clients in the process share consistent configuration.

When To Use

  • Several AWS clients in one service must use the same region, credentials, and endpoint settings.
  • The region is derived from an endpoint URL supplied by configuration.

Example

// The zero value is ready to use.
var opts awsopt.Options

opts.WithRegion("eu-west-1")

// Any config.LoadOptionsFunc from the SDK can be appended, so nothing in
// the SDK is out of reach. Static credentials keep the example offline;
// real code relies on the default credential chain.
opts.WithAWSOption(config.WithCredentialsProvider(
	credentials.NewStaticCredentialsProvider("AKID", "SECRET", ""),
))

awsCfg, err := opts.LoadDefaultConfig(context.TODO())
if err != nil {
	fmt.Println(err)

	return
}

// awsCfg is passed directly to any aws-sdk-go-v2 service constructor,
// for example s3.NewFromConfig or secretsmanager.NewFromConfig.
fmt.Println(awsCfg.Region)

// Output:
// eu-west-1

Full source is in example_awsopt_test.go. More runnable examples are on pkg.go.dev.

Dependencies

Importing this package pulls 14 external modules:

  • github.com/aws/aws-sdk-go-v2
  • github.com/aws/aws-sdk-go-v2/config
  • github.com/aws/aws-sdk-go-v2/credentials
  • github.com/aws/aws-sdk-go-v2/feature/ec2/imds
  • github.com/aws/aws-sdk-go-v2/internal/configsources
  • github.com/aws/aws-sdk-go-v2/internal/endpoints/v2
  • github.com/aws/aws-sdk-go-v2/internal/v4a
  • github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding
  • github.com/aws/aws-sdk-go-v2/service/internal/presigned-url
  • github.com/aws/aws-sdk-go-v2/service/signin
  • github.com/aws/aws-sdk-go-v2/service/sso
  • github.com/aws/aws-sdk-go-v2/service/ssooidc
  • github.com/aws/aws-sdk-go-v2/service/sts
  • github.com/aws/smithy-go