Skip to content

Commit ce0478f

Browse files
committed
Add profile option for AWS
1 parent 8aca02f commit ce0478f

File tree

3 files changed

+34
-33
lines changed

3 files changed

+34
-33
lines changed

cmd/sync/aws.go

+31-33
Original file line numberDiff line numberDiff line change
@@ -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.
8565
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+
}
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.
244241
type awsConfig struct {
245242
Region string `yaml:"region"`
243+
Profile string `yaml:"profile"`
246244
Upstreams []awsUpstream `yaml:"upstreams"`
247245
}
248246

cmd/sync/aws_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ func getValidAWSConfig() *awsConfig {
2222
cfg := awsConfig{
2323
Region: "us-west-2",
2424
Upstreams: upstreams,
25+
Profile: "default",
2526
}
2627

2728
return &cfg

examples/aws.md

+2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ region: us-west-2
2727
api_endpoint: http://127.0.0.1:8080/api
2828
sync_interval: 5s
2929
cloud_provider: AWS
30+
profile: default
3031
upstreams:
3132
- name: backend-one
3233
autoscaling_group: backend-one-group
@@ -54,6 +55,7 @@ upstreams:
5455
empty if using AWS. Possible values are: `AWS`, `Azure`.
5556
- The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups. Setting `region` to
5657
`self` will use the EC2 Metadata service to retrieve the region of the current instance.
58+
- The optional `profile` key specifies the AWS profile to use.
5759
- The `upstreams` key defines the list of upstream groups. For each upstream group we specify:
5860
- `name` – The name we specified for the upstream block in the NGINX Plus configuration.
5961
- `autoscaling_group` – The name of the corresponding Auto Scaling group. Use of wildcards is supported. For example,

0 commit comments

Comments
 (0)