Skip to content

Commit

Permalink
Merge pull request #3 from jepperson2/non_env_creds
Browse files Browse the repository at this point in the history
Don't ignore errors
  • Loading branch information
jepperson2 authored Aug 29, 2018
2 parents e4341f7 + 6d24a3e commit 0f47a8d
Showing 1 changed file with 14 additions and 4 deletions.
18 changes: 14 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,21 @@ func RetrieveSecret(variableName string) {
// If the region could not be found in an environment variable or a shared config file,
// create metaSession to fetch the ec2 instance region and pass to the regular Session.
if *sess.Config.Region == "" {
metaSession, _ := session.NewSession()
metaClient := ec2metadata.New(metaSession)
region, _ := metaClient.Region()
metaSession, err := session.NewSession()
if err != nil {
printAndExit(err)
}

sess.Config.Region = aws.String(region)
metaClient := ec2metadata.New(metaSession)
// If running on an EC2 instance, the metaClient will be available and we can set the region to match the instance
// If not on an EC2 instance, the region will remain blank and AWS returns a "MissingRegion: ..." error
if metaClient.Available() {
if region, err := metaClient.Region(); err == nil {
sess.Config.Region = aws.String(region)
} else {
printAndExit(err)
}
}
}

// Create a new instance of the service's client with a Session.
Expand Down

0 comments on commit 0f47a8d

Please sign in to comment.