discovery/aws: Fix region load from IMDS

Loading the local region from the Instance MetaData Service broke in v3.7. This adds the IMDS call back in order to load the local region when no other method has set the region.

fixes #17375

Signed-off-by: Joe Adams <github@joeadams.io>
This commit is contained in:
Joe Adams 2025-10-20 22:47:07 -04:00
parent 3107bdc2ea
commit 7a29bd2cb4
No known key found for this signature in database
GPG Key ID: B5D9F80A5CE7B14C

View File

@ -27,6 +27,7 @@ import (
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/credentials/stscreds"
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2Types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/sts"
@ -123,6 +124,7 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(any) error) error {
if err != nil {
return err
}
if c.Region == "" {
cfg, err := awsConfig.LoadDefaultConfig(context.Background())
if err != nil {
@ -134,6 +136,16 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(any) error) error {
// This can happen if the user has set the region in the AWS config file or environment variables.
c.Region = cfg.Region
}
if c.Region == "" {
// Try to get the region from the instance metadata service (IMDS).
imdsClient := imds.NewFromConfig(cfg)
region, err := imdsClient.GetRegion(context.Background(), &imds.GetRegionInput{})
if err != nil {
return err
}
c.Region = region.Region
}
}
if c.Region == "" {