Documentation
¶
Overview ¶
Package aws provides idiomatic Go helpers for AWS SDK v2.
Design Philosophy ¶
This package follows several key principles:
- No global state: All clients are explicitly created and passed
- Context-aware: All operations accept context.Context for cancellation
- Testable: Interfaces enable mocking without real AWS calls
- Explicit configuration: No magic; all settings are visible
- Safe defaults: Sensible retry policies and timeouts out of the box
- Minimal abstraction: Helpers augment the SDK, not replace it
Package Structure ¶
The aws package is organized into focused sub-packages:
- aws: Core configuration and common utilities
- aws/s3: S3 object and bucket operations
- aws/dynamodb: DynamoDB item and table operations
- aws/sqs: SQS message operations
- aws/secretsmanager: Secrets Manager operations
- aws/ssm: SSM Parameter Store operations
- aws/lambda: Lambda function operations
Configuration ¶
All clients are created with explicit configuration:
cfg, err := aws.LoadConfig(ctx, aws.WithRegion("us-west-2"))
if err != nil {
return err
}
s3Client := s3.NewClient(cfg)
Error Handling ¶
All operations return wrapped errors with context:
obj, err := client.GetObject(ctx, bucket, key)
if errors.Is(err, s3.ErrObjectNotFound) {
// Handle not found
}
Testing ¶
Each client accepts interfaces that can be mocked:
mock := &MockS3API{}
client := s3.NewClientWithAPI(mock)
// Use client in tests
Index ¶
- Constants
- Variables
- func WrapError(service, operation string, err error) error
- type Config
- type ConfigError
- type ConfigOption
- func WithCredentials(provider aws.CredentialsProvider) ConfigOption
- func WithEndpoint(endpoint string) ConfigOption
- func WithLoadOption(opt func(*config.LoadOptions) error) ConfigOption
- func WithProfile(profile string) ConfigOption
- func WithRegion(region string) ConfigOption
- func WithRetryMaxAttempts(attempts int) ConfigOption
- func WithRetryMode(mode aws.RetryMode) ConfigOption
- type NotFoundError
- type OperationError
- type ValidationError
Constants ¶
const DefaultRetryMaxAttempts = 3
DefaultRetryMaxAttempts is the default number of retry attempts for AWS operations.
const DefaultTimeout = 30 * time.Second
DefaultTimeout is the default timeout for AWS operations.
Variables ¶
var ( // ErrNilConfig is returned when a nil config is provided. ErrNilConfig = errors.New("aws: config is nil") // ErrNilClient is returned when a nil client is provided. ErrNilClient = errors.New("aws: client is nil") // ErrEmptyBucket is returned when a bucket name is empty. ErrEmptyBucket = errors.New("aws: bucket name is empty") // ErrEmptyKey is returned when a key/path is empty. ErrEmptyKey = errors.New("aws: key is empty") // ErrEmptyTable is returned when a table name is empty. ErrEmptyTable = errors.New("aws: table name is empty") // ErrEmptyQueue is returned when a queue name/URL is empty. ErrEmptyQueue = errors.New("aws: queue name is empty") // ErrEmptySecret is returned when a secret name is empty. ErrEmptySecret = errors.New("aws: secret name is empty") // ErrEmptyParameter is returned when a parameter name is empty. ErrEmptyParameter = errors.New("aws: parameter name is empty") // ErrEmptyFunction is returned when a function name is empty. ErrEmptyFunction = errors.New("aws: function name is empty") // ErrInvalidInput is returned when input validation fails. ErrInvalidInput = errors.New("aws: invalid input") )
Common sentinel errors.
Functions ¶
Types ¶
type Config ¶ added in v1.0.0
Config wraps aws.Config with additional helper methods.
func LoadConfig ¶ added in v1.0.0
func LoadConfig(ctx context.Context, opts ...ConfigOption) (*Config, error)
LoadConfig loads AWS configuration with the specified options. If no options are provided, it uses default configuration from environment variables, shared credentials, and IAM roles.
Example:
// Load with defaults
cfg, err := aws.LoadConfig(ctx)
// Load with specific region and profile
cfg, err := aws.LoadConfig(ctx,
aws.WithRegion("us-west-2"),
aws.WithProfile("production"),
)
type ConfigError ¶ added in v1.0.0
type ConfigError struct {
Err error
}
ConfigError represents an error loading AWS configuration.
func (*ConfigError) Error ¶ added in v1.0.0
func (e *ConfigError) Error() string
func (*ConfigError) Unwrap ¶ added in v1.0.0
func (e *ConfigError) Unwrap() error
type ConfigOption ¶ added in v1.0.0
type ConfigOption func(*configOptions)
ConfigOption configures AWS config loading.
func WithCredentials ¶ added in v1.0.0
func WithCredentials(provider aws.CredentialsProvider) ConfigOption
WithCredentials sets explicit credentials provider.
Example:
creds := credentials.NewStaticCredentialsProvider("key", "secret", "")
cfg, err := aws.LoadConfig(ctx, aws.WithCredentials(creds))
func WithEndpoint ¶ added in v1.0.0
func WithEndpoint(endpoint string) ConfigOption
WithEndpoint sets a custom endpoint URL for all services. Useful for testing with LocalStack or other AWS-compatible services.
Example:
cfg, err := aws.LoadConfig(ctx, aws.WithEndpoint("http://localhost:4566"))
func WithLoadOption ¶ added in v1.0.0
func WithLoadOption(opt func(*config.LoadOptions) error) ConfigOption
WithLoadOption adds a raw AWS SDK config load option. Use this for advanced configuration not covered by other options.
func WithProfile ¶ added in v1.0.0
func WithProfile(profile string) ConfigOption
WithProfile sets the AWS profile to use from shared credentials.
Example:
cfg, err := aws.LoadConfig(ctx, aws.WithProfile("production"))
func WithRegion ¶ added in v1.0.0
func WithRegion(region string) ConfigOption
WithRegion sets the AWS region.
Example:
cfg, err := aws.LoadConfig(ctx, aws.WithRegion("us-west-2"))
func WithRetryMaxAttempts ¶ added in v1.0.0
func WithRetryMaxAttempts(attempts int) ConfigOption
WithRetryMaxAttempts sets the maximum number of retry attempts. Default is 3 attempts.
Example:
cfg, err := aws.LoadConfig(ctx, aws.WithRetryMaxAttempts(5))
func WithRetryMode ¶ added in v1.0.0
func WithRetryMode(mode aws.RetryMode) ConfigOption
WithRetryMode sets the retry mode (standard or adaptive). Default is adaptive retry mode.
Example:
cfg, err := aws.LoadConfig(ctx, aws.WithRetryMode(aws.RetryModeAdaptive))
type NotFoundError ¶ added in v1.0.0
NotFoundError represents a resource not found error.
func NewNotFoundError ¶ added in v1.0.0
func NewNotFoundError(resource, id string) *NotFoundError
NewNotFoundError creates a new NotFoundError.
func (*NotFoundError) Error ¶ added in v1.0.0
func (e *NotFoundError) Error() string
type OperationError ¶ added in v1.0.0
OperationError represents an error during an AWS operation.
func (*OperationError) Error ¶ added in v1.0.0
func (e *OperationError) Error() string
func (*OperationError) Unwrap ¶ added in v1.0.0
func (e *OperationError) Unwrap() error
type ValidationError ¶ added in v1.0.0
ValidationError represents an input validation error.
func NewValidationError ¶ added in v1.0.0
func NewValidationError(field, message string) *ValidationError
NewValidationError creates a new ValidationError.
func (*ValidationError) Error ¶ added in v1.0.0
func (e *ValidationError) Error() string
Directories
¶
| Path | Synopsis |
|---|---|
|
Package dynamodb provides helpers for Amazon DynamoDB operations.
|
Package dynamodb provides helpers for Amazon DynamoDB operations. |
|
internal
|
|
|
pagination
Package pagination provides internal helpers for AWS paginated operations.
|
Package pagination provides internal helpers for AWS paginated operations. |
|
testutil
Package testutil provides utilities for testing AWS helpers with LocalStack.
|
Package testutil provides utilities for testing AWS helpers with LocalStack. |
|
Package lambda provides helpers for AWS Lambda operations.
|
Package lambda provides helpers for AWS Lambda operations. |
|
Package s3 provides helpers for Amazon S3 operations.
|
Package s3 provides helpers for Amazon S3 operations. |
|
Package secretsmanager provides helpers for AWS Secrets Manager operations.
|
Package secretsmanager provides helpers for AWS Secrets Manager operations. |
|
Package sqs provides helpers for Amazon SQS operations.
|
Package sqs provides helpers for Amazon SQS operations. |
|
Package ssm provides helpers for AWS Systems Manager Parameter Store operations.
|
Package ssm provides helpers for AWS Systems Manager Parameter Store operations. |