Skip to content

Commit 3b376d5

Browse files
committed
add feature for region self to use ec2metadata service for configuration
1 parent f118bb0 commit 3b376d5

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cmd/sync/aws.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77

88
"github.com/aws/aws-sdk-go/aws"
99
"github.com/aws/aws-sdk-go/aws/session"
10+
"github.com/aws/aws-sdk-go/aws/ec2metadata"
1011
"github.com/aws/aws-sdk-go/service/autoscaling"
1112
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
1213
"github.com/aws/aws-sdk-go/service/ec2"
@@ -29,6 +30,27 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
2930
return nil, fmt.Errorf("error validating config: %v", err)
3031
}
3132

33+
if cfg.Region == "self" {
34+
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
35+
params := &aws.Config{HTTPClient: httpClient}
36+
37+
metaSession, err := session.NewSession(params)
38+
if err != nil {
39+
return nil, err
40+
}
41+
42+
metaClient := ec2metadata.New(metaSession)
43+
if !metaClient.Available() {
44+
return nil, fmt.Errorf("ec2metadata service is unavailable")
45+
}
46+
47+
region, err := metaClient.Region()
48+
if err != nil {
49+
return nil, fmt.Errorf("unable to retreive region from ec2metadata: %v", err)
50+
}
51+
cfg.Region = region
52+
}
53+
3254
awsClient.config = cfg
3355

3456
err = awsClient.configure()

examples/aws.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ upstreams:
3131
* The `api_endpoint` key defines the NGINX Plus API endpoint.
3232
* The `sync_interval_in_seconds` key defines the synchronization interval: nginx-asg-sync checks for scaling updates every 5 seconds.
3333
* The `cloud_provider` key defines a cloud provider that will be used. The default is `AWS`. This means the key can be empty if using AWS. Possible values are: `AWS`, `Azure`.
34-
* The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups.
34+
* The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups. Setting `region` to `self` will use the EC2 Metadata service to retreive the region of the current instance.
3535
* The `upstreams` key defines the list of upstream groups. For each upstream group we specify:
3636
* `name` – The name we specified for the upstream block in the NGINX Plus configuration.
3737
* `autoscaling_group` – The name of the corresponding Auto Scaling group.

0 commit comments

Comments
 (0)