Skip to content

Commit

Permalink
add feature for region self to use ec2metadata service for configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
trjstewart committed Sep 23, 2019
1 parent 055ef27 commit 2e4ed2d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
22 changes: 22 additions & 0 deletions cmd/sync/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/ec2/ec2iface"
yaml "gopkg.in/yaml.v2"
Expand All @@ -26,6 +27,27 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
return nil, fmt.Errorf("error validating config: %v", err)
}

if cfg.Region == "self" {
httpClient := &http.Client{Timeout: connTimeoutInSecs * time.Second}
params := &aws.Config{HTTPClient: httpClient}

metaSession, err := session.NewSession(params)
if err != nil {
return nil, err
}

metaClient := ec2metadata.New(metaSession)
if !metaClient.Available() {
return nil, fmt.Errorf("ec2metadata service is unavailable")
}

region, err := metaClient.Region()
if err != nil {
return nil, fmt.Errorf("unable to retreive region from ec2metadata: %v", err)
}
cfg.Region = region
}

awsClient.config = cfg

err = awsClient.configure()
Expand Down
2 changes: 1 addition & 1 deletion examples/aws.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ upstreams:
* The `api_endpoint` key defines the NGINX Plus API endpoint.
* The `sync_interval_in_seconds` key defines the synchronization interval: nginx-asg-sync checks for scaling updates every 5 seconds.
* 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`.
* The `region` key defines the AWS region where we deploy NGINX Plus and the Auto Scaling groups.
* 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.
* The `upstreams` key defines the list of upstream groups. For each upstream group we specify:
* `name` – The name we specified for the upstream block in the NGINX Plus configuration.
* `autoscaling_group` – The name of the corresponding Auto Scaling group. Use of wildcards is supported.
Expand Down

0 comments on commit 2e4ed2d

Please sign in to comment.