@@ -4,11 +4,11 @@ import (
4
4
"context"
5
5
"errors"
6
6
"fmt"
7
- "net/http"
8
7
"reflect"
9
8
"time"
10
9
11
10
"github.com/aws/aws-sdk-go-v2/aws"
11
+ "github.com/aws/aws-sdk-go-v2/aws/transport/http"
12
12
"github.com/aws/aws-sdk-go-v2/config"
13
13
"github.com/aws/aws-sdk-go-v2/feature/ec2/imds"
14
14
"github.com/aws/aws-sdk-go-v2/service/autoscaling"
@@ -31,26 +31,6 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
31
31
if err != nil {
32
32
return nil , fmt .Errorf ("error validating config: %w" , err )
33
33
}
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
-
54
34
awsClient .config = cfg
55
35
56
36
err = awsClient .configure ()
@@ -83,22 +63,40 @@ func (client *AWSClient) GetUpstreams() []Upstream {
83
63
84
64
// configure configures the AWSClient with necessary parameters.
85
65
func (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
+ }
87
77
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
+ )
89
93
if err != nil {
90
94
return fmt .Errorf ("unable to load default AWS config: %w" , err )
91
95
}
92
96
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 )
97
98
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 )
102
100
103
101
return nil
104
102
}
@@ -239,10 +237,10 @@ func prepareBatches(maxItems int, items []string) [][]string {
239
237
return batches
240
238
}
241
239
242
- // Configuration for AWS Cloud Provider
243
-
240
+ // Configuration for AWS Cloud Provider.
244
241
type awsConfig struct {
245
242
Region string `yaml:"region"`
243
+ Profile string `yaml:"profile"`
246
244
Upstreams []awsUpstream `yaml:"upstreams"`
247
245
}
248
246
0 commit comments