Credential management for AWS SDK Go v2, providing retrieval from multiple sources including static credentials, EC2 instance roles, SSO, STS, external processes, and HTTP endpoints
npx @tessl/cli install tessl/golang-github-com-aws-aws-sdk-go-v2--credentials@1.19.0The credentials package provides a comprehensive credential management system for the AWS SDK for Go v2. It enables applications to retrieve AWS credentials from multiple sources with a consistent provider-based architecture.
go get github.com/aws/aws-sdk-go-v2/credentials@v1.19.2import (
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/credentials/endpointcreds"
"github.com/aws/aws-sdk-go-v2/credentials/logincreds"
"github.com/aws/aws-sdk-go-v2/credentials/processcreds"
"github.com/aws/aws-sdk-go-v2/credentials/ssocreds"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
)package main
import (
"context"
"fmt"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials"
)
func main() {
// Create static credentials
provider := credentials.NewStaticCredentialsProvider(
"AKIAIOSFODNN7EXAMPLE",
"wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY",
"",
)
// Retrieve credentials
creds, err := provider.Retrieve(context.TODO())
if err != nil {
panic(err)
}
fmt.Printf("Access Key: %s\n", creds.AccessKeyID)
}The credentials package follows a provider-based architecture where all credential providers implement the aws.CredentialsProvider interface. Key patterns include:
Retrieve(context.Context) (aws.Credentials, error)ProviderSources() for debugging credential resolutionaws.CredentialsCache for concurrency safety and performanceCreate credentials from explicit access key, secret key, and optional session token.
func NewStaticCredentialsProvider(key, secret, session string) StaticCredentialsProvider
type StaticCredentialsProvider struct {
Value aws.Credentials
Source []aws.CredentialSource
}
func (s StaticCredentialsProvider) Retrieve(_ context.Context) (aws.Credentials, error)
func (s StaticCredentialsProvider) ProviderSources() []aws.CredentialSourceRetrieve credentials from Amazon EC2 Instance Metadata Service (IMDS) for EC2 instances with attached IAM roles.
func New(optFns ...func(*Options)) *Provider
type Provider struct {
// Has unexported fields
}
func (p *Provider) Retrieve(ctx context.Context) (aws.Credentials, error)Retrieve credentials from arbitrary HTTP endpoints, supporting both static and refreshable credentials.
func New(endpoint string, optFns ...func(*Options)) *Provider
type Provider struct {
// Has unexported fields
}
func (p *Provider) Retrieve(ctx context.Context) (aws.Credentials, error)Retrieve credentials for sessions created via aws login command using cached OAuth2 tokens.
func New(client TokenAPIClient, path string, opts ...func(*Options)) *Provider
type Provider struct {
// Has unexported fields
}
func (p *Provider) Retrieve(ctx context.Context) (aws.Credentials, error)Execute external commands/processes to retrieve credentials, useful for custom credential sources.
func NewProvider(command string, options ...func(*Options)) *Provider
type Provider struct {
// Has unexported fields
}
func (p *Provider) Retrieve(ctx context.Context) (aws.Credentials, error)Retrieve temporary credentials using AWS Single Sign-On (SSO) access tokens cached from AWS CLI.
func New(client GetRoleCredentialsAPIClient, accountID, roleName, startURL string, optFns ...func(options *Options)) *Provider
type Provider struct {
// Has unexported fields
}
func (p *Provider) Retrieve(ctx context.Context) (aws.Credentials, error)Retrieve temporary credentials via AWS Security Token Service (STS) by assuming IAM roles or using web identity tokens.
func NewAssumeRoleProvider(client AssumeRoleAPIClient, roleARN string, optFns ...func(*AssumeRoleOptions)) *AssumeRoleProvider
type AssumeRoleProvider struct {
// Has unexported fields
}
func (p *AssumeRoleProvider) Retrieve(ctx context.Context) (aws.Credentials, error)
func NewWebIdentityRoleProvider(client AssumeRoleWithWebIdentityAPIClient, roleARN string, tokenRetriever IdentityTokenRetriever, optFns ...func(*WebIdentityRoleOptions)) *WebIdentityRoleProvider
type WebIdentityRoleProvider struct {
// Has unexported fields
}
func (p *WebIdentityRoleProvider) Retrieve(ctx context.Context) (aws.Credentials, error)The credentials structure returned by all providers:
type Credentials struct {
AccessKeyID string
SecretAccessKey string
SessionToken string
Source string
CanExpire bool
Expires time.Time
}The interface implemented by all credential providers:
type CredentialsProvider interface {
Retrieve(ctx context.Context) (Credentials, error)
}For tracking credential chain information:
type CredentialSource struct {
Name string
}All credential providers return errors when credential retrieval fails. Common error scenarios include:
Each provider returns descriptive errors that can be checked and handled appropriately.