diff --git a/main.go b/main.go index ac7d716..178b360 100644 --- a/main.go +++ b/main.go @@ -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.