Package: github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds
The ec2rolecreds package provides credential retrieval from Amazon EC2 Instance Roles via the EC2 Instance Metadata Service (IMDS).
import (
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
)When EC2 instances are launched with an attached IAM role, temporary credentials are made available through IMDS. This provider retrieves those credentials automatically without requiring explicit configuration.
Important: The Provider is not safe for concurrent use. Wrap it with aws.CredentialsCache to provide concurrency safety and credential caching.
package main
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
// Create EC2 role credentials provider
provider := ec2rolecreds.New()
// Wrap with CredentialsCache for concurrency safety and caching
credentials := aws.NewCredentialsCache(provider)
// Use with AWS service clients
cfg := aws.Config{
Region: "us-east-1",
Credentials: credentials,
}
client := s3.NewFromConfig(cfg)
// Use client
}package main
import (
"context"
"net/http"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
)
func main() {
// Create custom IMDS client
imdsClient := imds.New(imds.Options{
HTTPClient: &http.Client{
Timeout: 5 * time.Second,
},
})
// Create provider with custom IMDS client
provider := ec2rolecreds.New(func(o *ec2rolecreds.Options) {
o.Client = imdsClient
})
credentials := aws.NewCredentialsCache(provider)
// Use credentials
}const ProviderName = "EC2RoleProvider"Provider name identifier for EC2 role credentials.
func New(optFns ...func(*Options)) *ProviderReturns an initialized Provider configured to retrieve credentials from EC2 Instance Metadata service.
Parameters:
optFns (...func(*Options)): Functional options for configuring the providerReturns:
*Provider: Initialized EC2 role credentials providerExample:
// Default configuration
provider := ec2rolecreds.New()
// With custom IMDS client
provider := ec2rolecreds.New(func(o *ec2rolecreds.Options) {
o.Client = customIMDSClient
})type Options struct {
Client GetMetadataAPIClient
CredentialSources []aws.CredentialSource
}Configuration options for the Provider.
Fields:
Client (GetMetadataAPIClient): The API client for EC2 IMDS operations. If nil, defaults to the standard EC2 IMDS clientCredentialSources ([]aws.CredentialSource): Credential chain information for reporting purposes (not meant to be set directly)type Provider struct {
// Has unexported fields
}Retrieves credentials from the EC2 service and tracks credential expiration.
Note: Not safe for concurrent use. Wrap with aws.CredentialsCache for production use.
func (p *Provider) Retrieve(ctx context.Context) (aws.Credentials, error)Retrieves credentials from the EC2 Instance Metadata Service.
Parameters:
ctx (context.Context): Context for the request, supports cancellation and timeoutsReturns:
aws.Credentials: Retrieved temporary credentials with expiration timeerror: Error if request fails or unable to extract credentialsExample:
creds, err := provider.Retrieve(context.TODO())
if err != nil {
log.Fatalf("Failed to retrieve credentials: %v", err)
}
fmt.Printf("Access Key: %s\n", creds.AccessKeyID)
fmt.Printf("Expires: %s\n", creds.Expires)func (p *Provider) ProviderSources() []aws.CredentialSourceReturns the credential chain that was used to construct this provider.
Returns:
[]aws.CredentialSource: Credential source chain for debuggingfunc (p *Provider) AdjustExpiresBy(creds aws.Credentials, dur time.Duration) (aws.Credentials, error)Adds the specified duration to the credential's expiration time, unless the time until expiration is less than 15 minutes. Returns the credentials even if not updated.
Parameters:
creds (aws.Credentials): The credentials to adjustdur (time.Duration): Duration to add to the expiration timeReturns:
aws.Credentials: Credentials with potentially adjusted expirationerror: Error if adjustment failsExample:
// Extend credentials expiration by 5 minutes
adjustedCreds, err := provider.AdjustExpiresBy(creds, 5*time.Minute)
if err != nil {
log.Fatal(err)
}func (p *Provider) HandleFailToRefresh(ctx context.Context, prevCreds aws.Credentials, err error) (aws.Credentials, error)Extends the credential expiration time if the credentials are expired. If the credentials will not expire within the minimum time, they are returned unchanged. If the credentials cannot expire, the original error is returned.
Parameters:
ctx (context.Context): Context for the operationprevCreds (aws.Credentials): Previously retrieved credentialserr (error): The error that occurred during refresh attemptReturns:
aws.Credentials: Credentials with potentially extended expirationerror: Original error if credentials cannot be extendedExample:
newCreds, err := provider.Retrieve(ctx)
if err != nil {
// Try to extend previous credentials on failure
extendedCreds, extErr := provider.HandleFailToRefresh(ctx, prevCreds, err)
if extErr != nil {
log.Fatal(extErr)
}
newCreds = extendedCreds
}type GetMetadataAPIClient interface {
GetMetadata(context.Context, *imds.GetMetadataInput, ...func(*imds.Options)) (*imds.GetMetadataOutput, error)
}Interface for an EC2 IMDS API client. Allows for custom IMDS client implementations or mocking in tests.
Methods:
GetMetadata: Retrieves metadata from EC2 IMDSThe EC2 Instance role credentials provider is automatically used by the SDK's default credential chain when no other credential provider is resolved first.
You can explicitly configure it in the AWS shared config file:
[default]
credential_source = Ec2InstanceMetadataCommon error scenarios:
Example:
creds, err := provider.Retrieve(ctx)
if err != nil {
if strings.Contains(err.Error(), "no EC2 IMDS role found") {
log.Fatal("EC2 instance does not have an IAM role attached")
}
log.Fatalf("Failed to retrieve credentials: %v", err)
}import (
"context"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
func main() {
// Load default config - will use EC2 role credentials if available
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
panic(err)
}
// Create service client
client := s3.NewFromConfig(cfg)
// Use client
}Always wrap with CredentialsCache: The provider is not thread-safe and doesn't cache credentials internally
provider := ec2rolecreds.New()
creds := aws.NewCredentialsCache(provider)Use default credential chain: Prefer using config.LoadDefaultConfig() which includes EC2 role credentials automatically
IMDSv2: The default IMDS client supports both IMDSv1 and IMDSv2. IMDSv2 is recommended for enhanced security
Credential expiration: EC2 role credentials typically expire after 6 hours. CredentialsCache handles automatic refresh
Context timeouts: Use appropriate context timeouts for IMDS requests:
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
creds, err := provider.Retrieve(ctx)