The region configuration capability provides functionality for resolving AWS region from the EC2 Instance Metadata Service (IMDS).
The UseEC2IMDSRegion type enables automatic region detection from EC2 Instance Metadata Service when running on EC2 instances. This is useful for applications that need to automatically determine their region based on the EC2 instance location.
type UseEC2IMDSRegion struct {
Client *imds.Client
}Fields:
Client - Optional EC2 Instance Metadata Service client. If unset, defaults to a generic EC2 IMDS client.package main
import (
"context"
"fmt"
"log"
"github.com/aws/aws-sdk-go-v2/config"
)
func main() {
// Enable EC2 IMDS region resolution
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSRegion(),
)
if err != nil {
log.Fatalf("unable to load SDK config, %v", err)
}
fmt.Printf("Detected region from EC2 IMDS: %s\n", cfg.Region)
}import (
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"time"
)
// Create custom IMDS client with specific options
customIMDSClient := imds.New(imds.Options{
Endpoint: "http://169.254.169.254",
})
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSRegion(func(o *config.UseEC2IMDSRegion) {
o.Client = customIMDSClient
}),
)
if err != nil {
log.Fatal(err)
}// Try EC2 IMDS first, fall back to default region if not on EC2
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSRegion(),
config.WithDefaultRegion("us-east-1"),
)
if err != nil {
log.Fatal(err)
}When loading configuration, regions are resolved in the following order (first match wins):
AWS_REGION or AWS_DEFAULT_REGION~/.aws/configWithEC2IMDSRegion is configuredWithDefaultRegion is configuredThe EC2 Instance Metadata Service (IMDS) is a service available to EC2 instances that provides information about the instance, including:
By default, IMDS is available at:
http://169.254.169.254http://[fd00:ec2::254]EC2 supports two versions of IMDS:
The SDK defaults to using IMDSv2 with automatic fallback to IMDSv1 unless disabled.
import (
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
)
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSClientEnableState(imds.ClientDisabled),
)// Use IPv6 IMDS endpoint
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSEndpointMode(imds.EndpointModeStateIPv6),
)cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSEndpoint("http://169.254.169.254"),
)Disable fallback to IMDSv1 for improved security:
import (
"os"
)
// Via environment variable (recommended)
os.Setenv("AWS_EC2_METADATA_V1_DISABLED", "true")
cfg, err := config.LoadDefaultConfig(context.TODO())When EC2 IMDS region resolution fails (e.g., not running on EC2), the SDK continues with other region sources:
// This will not fail if IMDS is unavailable
// It will fall back to environment variables or default region
cfg, err := config.LoadDefaultConfig(
context.TODO(),
config.WithEC2IMDSRegion(),
config.WithDefaultRegion("us-east-1"), // Fallback
)
if err != nil {
log.Fatal(err)
}
// The region will be from IMDS if available, otherwise from default
fmt.Printf("Region: %s\n", cfg.Region)