Table of contents
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:
- Create an
Optionsvalue (zero value is ready to use). - Append options with
Options.WithAWSOption,Options.WithRegion, orOptions.WithRegionFromURL. - Call
Options.LoadDefaultConfigto obtain anaws.Configthat 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-v2github.com/aws/aws-sdk-go-v2/configgithub.com/aws/aws-sdk-go-v2/credentialsgithub.com/aws/aws-sdk-go-v2/feature/ec2/imdsgithub.com/aws/aws-sdk-go-v2/internal/configsourcesgithub.com/aws/aws-sdk-go-v2/internal/endpoints/v2github.com/aws/aws-sdk-go-v2/internal/v4agithub.com/aws/aws-sdk-go-v2/service/internal/accept-encodinggithub.com/aws/aws-sdk-go-v2/service/internal/presigned-urlgithub.com/aws/aws-sdk-go-v2/service/signingithub.com/aws/aws-sdk-go-v2/service/ssogithub.com/aws/aws-sdk-go-v2/service/ssooidcgithub.com/aws/aws-sdk-go-v2/service/stsgithub.com/aws/smithy-go