Skip to content

Commit

Permalink
Merge pull request #4 from segmentio/try-to-determine-region-automati…
Browse files Browse the repository at this point in the history
…cally

try to determine region automatically if not set:
  • Loading branch information
dfuentes committed Jun 20, 2017
2 parents 389a404 + 8ab8caf commit 0a4f848
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
1 change: 0 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func Execute() {
}

func init() {
RootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func validateService(service string) error {
Expand Down
16 changes: 14 additions & 2 deletions store/ssmstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package store

import (
"fmt"
"os"
"regexp"
"strconv"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ssm"
"github.com/aws/aws-sdk-go/service/ssm/ssmiface"
Expand All @@ -27,8 +29,18 @@ type SSMStore struct {

// NewSSMStore creates a new SSMStore
func NewSSMStore() *SSMStore {
session := session.New()
svc := ssm.New(session)
region, ok := os.LookupEnv("AWS_REGION")
if !ok {
// If region is not set, attempt to determine it via ec2 metadata API
session := session.New()
ec2metadataSvc := ec2metadata.New(session)
region, _ = ec2metadataSvc.Region()
}

ssmSession := session.Must(session.NewSession(&aws.Config{
Region: aws.String(region),
}))
svc := ssm.New(ssmSession)
return &SSMStore{
svc: svc,
}
Expand Down

0 comments on commit 0a4f848

Please sign in to comment.