@@ -4,11 +4,11 @@ import (
44 "context"
55 "errors"
66 "fmt"
7- "net/http"
87 "reflect"
98 "time"
109
1110 "github.com/aws/aws-sdk-go-v2/aws"
11+ "github.com/aws/aws-sdk-go-v2/aws/transport/http"
1212 "github.com/aws/aws-sdk-go-v2/config"
1313 "github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
1414 "github.com/aws/aws-sdk-go-v2/service/autoscaling"
@@ -31,26 +31,6 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
3131 if err != nil {
3232 return nil , fmt .Errorf ("error validating config: %w" , err )
3333 }
34-
35- if cfg .Region == "self" {
36- httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
37-
38- conf , loadErr := config .LoadDefaultConfig (context .TODO ())
39- if loadErr != nil {
40- return nil , fmt .Errorf ("unable to load default AWS config: %w" , loadErr )
41- }
42-
43- client := imds .NewFromConfig (conf , func (o * imds.Options ) {
44- o .HTTPClient = httpClient
45- })
46-
47- response , regionErr := client .GetRegion (context .TODO (), & imds.GetRegionInput {})
48- if regionErr != nil {
49- return nil , fmt .Errorf ("unable to retrieve region from ec2metadata: %w" , regionErr )
50- }
51- cfg .Region = response .Region
52- }
53-
5434 awsClient .config = cfg
5535
5636 err = awsClient .configure ()
@@ -83,22 +63,40 @@ func (client *AWSClient) GetUpstreams() []Upstream {
8363
8464// configure configures the AWSClient with necessary parameters.
8565func (client * AWSClient ) configure () error {
86- httpClient := & http.Client {Timeout : connTimeoutInSecs * time .Second }
66+ httpClient := http .NewBuildableClient ().WithTimeout (connTimeoutInSecs * time .Second )
67+
68+ if client .config .Region == "self" {
69+ conf , loadErr := config .LoadDefaultConfig (
70+ context .TODO (),
71+ config .WithSharedConfigProfile (client .config .Profile ),
72+ config .WithHTTPClient (httpClient ),
73+ )
74+ if loadErr != nil {
75+ return fmt .Errorf ("unable to load default AWS config: %w" , loadErr )
76+ }
8777
88- cfg , err := config .LoadDefaultConfig (context .TODO ())
78+ imdClient := imds .NewFromConfig (conf )
79+
80+ response , regionErr := imdClient .GetRegion (context .TODO (), & imds.GetRegionInput {})
81+ if regionErr != nil {
82+ return fmt .Errorf ("unable to retrieve region from ec2metadata: %w" , regionErr )
83+ }
84+ client .config .Region = response .Region
85+ }
86+
87+ cfg , err := config .LoadDefaultConfig (
88+ context .TODO (),
89+ config .WithSharedConfigProfile (client .config .Profile ),
90+ config .WithRegion (client .config .Region ),
91+ config .WithHTTPClient (httpClient ),
92+ )
8993 if err != nil {
9094 return fmt .Errorf ("unable to load default AWS config: %w" , err )
9195 }
9296
93- client .svcEC2 = ec2 .NewFromConfig (cfg , func (o * ec2.Options ) {
94- o .Region = client .config .Region
95- o .HTTPClient = httpClient
96- })
97+ client .svcEC2 = ec2 .NewFromConfig (cfg )
9798
98- client .svcAutoscaling = autoscaling .NewFromConfig (cfg , func (o * autoscaling.Options ) {
99- o .Region = client .config .Region
100- o .HTTPClient = httpClient
101- })
99+ client .svcAutoscaling = autoscaling .NewFromConfig (cfg )
102100
103101 return nil
104102}
@@ -239,10 +237,10 @@ func prepareBatches(maxItems int, items []string) [][]string {
239237 return batches
240238}
241239
242- // Configuration for AWS Cloud Provider
243-
240+ // Configuration for AWS Cloud Provider.
244241type awsConfig struct {
245242 Region string `yaml:"region"`
243+ Profile string `yaml:"profile"`
246244 Upstreams []awsUpstream `yaml:"upstreams"`
247245}
248246
0 commit comments