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
implements solution for issue #26
  • Loading branch information
trjstewart committed Sep 19, 2019
1 parent f118bb0 commit ae94a73
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
19 changes: 19 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/autoscaling"
"github.com/aws/aws-sdk-go/service/autoscaling/autoscalingiface"
"github.com/aws/aws-sdk-go/service/ec2"
Expand All @@ -29,6 +30,24 @@ func NewAWSClient(data []byte) (*AWSClient, error) {
return nil, fmt.Errorf("error validating config: %v", err)
}

if cfg.Region == "self" {
metaSession, err := session.NewSession()
if err != nil {
return nil, err
}

metaClient := ec2metadata.New(metaSession)
if metaClient.Available() != true {
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 @@ -31,7 +31,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.
Expand Down

0 comments on commit ae94a73

Please sign in to comment.