From 7a29bd2cb4e4dfc6407fbfaa255f465088b5c4db Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Mon, 20 Oct 2025 22:47:07 -0400 Subject: [PATCH 1/3] 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 --- discovery/aws/ec2.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/discovery/aws/ec2.go b/discovery/aws/ec2.go index 539cd84c4f..d6e0fd012d 100644 --- a/discovery/aws/ec2.go +++ b/discovery/aws/ec2.go @@ -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 == "" { From a425442c1c864c74c9657929b9ac4624a2200344 Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Mon, 20 Oct 2025 22:51:14 -0400 Subject: [PATCH 2/3] Add IMDS go dep Signed-off-by: Joe Adams --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index b3793ba033..02ec483ff7 100644 --- a/go.mod +++ b/go.mod @@ -114,7 +114,7 @@ require ( github.com/Microsoft/go-winio v0.6.1 // indirect github.com/armon/go-metrics v0.4.1 // indirect github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect - github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.9 // indirect + github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.18.9 github.com/aws/aws-sdk-go-v2/internal/configsources v1.4.9 // indirect github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.7.9 // indirect github.com/aws/aws-sdk-go-v2/internal/ini v1.8.3 // indirect From 6f9af6651ee24f4053a85ac9af92a6c7cc864adc Mon Sep 17 00:00:00 2001 From: Joe Adams Date: Tue, 21 Oct 2025 20:31:55 -0400 Subject: [PATCH 3/3] Update lightsail to use IMDS for region Signed-off-by: Joe Adams --- discovery/aws/lightsail.go | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/discovery/aws/lightsail.go b/discovery/aws/lightsail.go index 5c356c8c45..2bfc9c2eae 100644 --- a/discovery/aws/lightsail.go +++ b/discovery/aws/lightsail.go @@ -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/lightsail" "github.com/aws/aws-sdk-go-v2/service/sts" "github.com/aws/smithy-go" @@ -105,14 +106,27 @@ func (c *LightsailSDConfig) UnmarshalYAML(unmarshal func(any) error) error { if err != nil { return err } + if c.Region == "" { cfg, err := awsConfig.LoadDefaultConfig(context.Background()) if err != nil { return err } - // Use the region from the AWS config. It will load environment variables and shared config files. - c.Region = cfg.Region + if cfg.Region != "" { + // Use the region from the AWS config. It will load environment variables and shared config files. + 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 == "" {