Skip to content

Commit 90e01a4

Browse files
committed
Merge branch 'feature/region-ec2-metadata' into feature/get-instances-via-describe-instance
2 parents 055ef27 + 2e4ed2d commit 90e01a4

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/ec2"
1112
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
1213
yaml "gopkg.in/yaml.v2"
@@ -26,6 +27,27 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
2627
return nil, fmt.Errorf("error validating config: %v", err)
2728
}
2829

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

3153
err = awsClient.configure()

examples/aws.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ upstreams:
3535
* The `api_endpoint` key defines the NGINX Plus API endpoint.
3636
* The `sync_interval_in_seconds` key defines the synchronization interval: nginx-asg-sync checks for scaling updates every 5 seconds.
3737
* 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`.
38-
* The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups.
38+
* 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.
3939
* The `upstreams` key defines the list of upstream groups. For each upstream group we specify:
4040
* `name` – The name we specified for the upstream block in the NGINX Plus configuration.
4141
* `autoscaling_group` – The name of the corresponding Auto Scaling group. Use of wildcards is supported.

0 commit comments

Comments
 (0)