diff --git a/discovery/aws/ec2.go b/discovery/aws/ec2.go index cbe3f91773..431bbcd811 100644 --- a/discovery/aws/ec2.go +++ b/discovery/aws/ec2.go @@ -27,7 +27,6 @@ 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" @@ -125,9 +124,7 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { return err } if c.Region == "" { - // TODO(@sysadmind): Should we get a context from somewhere? - ctx := context.TODO() - cfg, err := awsConfig.LoadDefaultConfig(ctx) + cfg, err := awsConfig.LoadDefaultConfig(context.Background()) if err != nil { return err } @@ -136,17 +133,13 @@ func (c *EC2SDConfig) UnmarshalYAML(unmarshal func(interface{}) error) error { // If the region is already set in the config, use it. // This can happen if the user has set the region in the AWS config file or environment variables. c.Region = cfg.Region - } else { - // Attempt to get the region from the instance metadata service. - metaClient := imds.NewFromConfig(cfg) - result, err := metaClient.GetRegion(ctx, &imds.GetRegionInput{}) - if err != nil { - return errors.New("EC2 SD configuration requires a region") - } - c.Region = result.Region } } + if c.Region == "" { + return errors.New("EC2 SD configuration requires a region") + } + for _, f := range c.Filters { if len(f.Values) == 0 { return errors.New("EC2 SD configuration filter values cannot be empty") diff --git a/discovery/aws/ec2_test.go b/discovery/aws/ec2_test.go index 0dc389fe74..8a8fd6e244 100644 --- a/discovery/aws/ec2_test.go +++ b/discovery/aws/ec2_test.go @@ -38,10 +38,6 @@ func boolptr(b bool) *bool { return &b } -func int32ptr(i int32) *int32 { - return &i -} - // Struct for test data. type ec2DataStore struct { region string @@ -289,7 +285,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) { // interface without primary IPv6, index 2 { Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{ - DeviceIndex: int32ptr(3), + DeviceIndex: aws.Int32(3), }, Ipv6Addresses: []ec2Types.InstanceIpv6Address{ { @@ -302,7 +298,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) { // interface with primary IPv6, index 1 { Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{ - DeviceIndex: int32ptr(1), + DeviceIndex: aws.Int32(1), }, Ipv6Addresses: []ec2Types.InstanceIpv6Address{ { @@ -319,7 +315,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) { // interface with primary IPv6, index 3 { Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{ - DeviceIndex: int32ptr(3), + DeviceIndex: aws.Int32(3), }, Ipv6Addresses: []ec2Types.InstanceIpv6Address{ { @@ -332,7 +328,7 @@ func TestEC2DiscoveryRefresh(t *testing.T) { // interface without primary IPv6, index 0 { Attachment: &ec2Types.InstanceNetworkInterfaceAttachment{ - DeviceIndex: int32ptr(0), + DeviceIndex: aws.Int32(0), }, Ipv6Addresses: []ec2Types.InstanceIpv6Address{}, SubnetId: strptr("azid-3"), diff --git a/discovery/aws/lightsail.go b/discovery/aws/lightsail.go index 86ed73837a..62c64afcfd 100644 --- a/discovery/aws/lightsail.go +++ b/discovery/aws/lightsail.go @@ -27,7 +27,6 @@ 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" @@ -107,27 +106,19 @@ func (c *LightsailSDConfig) UnmarshalYAML(unmarshal func(interface{}) error) err return err } if c.Region == "" { - // TODO(@sysadmind): Should we get a context from somewhere? - ctx := context.TODO() - cfg, err := awsConfig.LoadDefaultConfig(ctx) + cfg, err := awsConfig.LoadDefaultConfig(context.Background()) if err != nil { return err } - if cfg.Region != "" { - // If the region is already set in the config, use it. - // This can happen if the user has set the region in the AWS config file or environment variables. - c.Region = cfg.Region - } else { - // Attempt to get the region from the instance metadata service. - metaClient := imds.NewFromConfig(cfg) - result, err := metaClient.GetRegion(ctx, &imds.GetRegionInput{}) - if err != nil { - return errors.New("EC2 SD configuration requires a region") - } - c.Region = result.Region - } + // Use the region from the AWS config. It will load environment variables and shared config files. + c.Region = cfg.Region } + + if c.Region == "" { + return errors.New("lightsail SD configuration requires a region") + } + return c.HTTPClientConfig.Validate() }