From 6d24a3e2973ce98a0c5af23984b7cf3dd0d0938f Mon Sep 17 00:00:00 2001 From: Jesse Epperson Date: Wed, 29 Aug 2018 10:16:18 -0700 Subject: [PATCH] Don't ignore errors --- main.go | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) 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.