From f4c72c4fe3290725229d59b19555aebd9f7b9e5b Mon Sep 17 00:00:00 2001 From: "Carlos Manzanedo Rueda (ruecarlo@)" Date: Sat, 20 Jun 2020 15:29:37 +0100 Subject: [PATCH 1/4] Added AWS_PROFLE and shared cred Following convention, if the command line arguments does not specify a profile, we check the AWS_PROFILE. The sdk session options is also initialised with SharedConfigEnable to support shared configurations from the (~/.aws/config). https://github.com/aws/aws-sdk-go#configuring-credentials --- cmd/main.go | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index e17bb44..9abb6d6 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -32,6 +32,7 @@ import ( const ( binName = "ec2-instance-selector" awsRegionEnvVar = "AWS_REGION" + awsProfileEnvVar = "AWS_PROFILE" defaultRegionEnvVar = "AWS_DEFAULT_REGION" defaultProfile = "default" awsConfigFile = "~/.aws/config" @@ -270,11 +271,15 @@ func getOutputFn(outputFlag *string, currentFn selector.InstanceTypesOutputFn) s } func getRegionAndProfileAWSSession(regionName *string, profileName *string) (*session.Session, error) { - sessOpts := session.Options{} + sessOpts := session.Options{SharedConfigState: session.SharedConfigEnable} if regionName != nil { sessOpts.Config.Region = regionName } + if awsProfileName, ok := os.LookupEnv(awsProfileEnvVar); profileName != nil && ok && awsProfileName != "" { + profileName = &awsProfileName + } + if profileName != nil { sessOpts.Profile = *profileName if sessOpts.Config.Region == nil { From 6a8355e67a99ad60dfe05beafb23c32011c5bc2e Mon Sep 17 00:00:00 2001 From: "Carlos Manzanedo Rueda (ruecarlo@)" Date: Sat, 20 Jun 2020 15:40:02 +0100 Subject: [PATCH 2/4] AWS_PROFILE and shared cred Following convention, if the command line arguments does not specify a profile, we check the AWS_PROFILE. The sdk session options is also initialised with SharedConfigEnable to support shared configurations from the (~/.aws/config). https://github.com/aws/aws-sdk-go#configuring-credentials --- cmd/main.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index 9abb6d6..184b69f 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -276,7 +276,7 @@ func getRegionAndProfileAWSSession(regionName *string, profileName *string) (*se sessOpts.Config.Region = regionName } - if awsProfileName, ok := os.LookupEnv(awsProfileEnvVar); profileName != nil && ok && awsProfileName != "" { + if awsProfileName, ok := os.LookupEnv(awsProfileEnvVar); profileName == nil && ok && awsProfileName != "" { profileName = &awsProfileName } From 491104972757a44c2bee0bcf221f3ac7e2613020 Mon Sep 17 00:00:00 2001 From: "Carlos Manzanedo Rueda (ruecarlo@)" Date: Mon, 22 Jun 2020 11:22:49 +0100 Subject: [PATCH 3/4] Removing AWS_PROFILE lookup in fav sharedConfigEnabled By setting up the sharedConfigState to enabled the SDK does take care of the AWS_PROFILE population: https://docs.aws.amazon.com/sdk-for-go/api/aws/session/#SharedConfigState --- cmd/main.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 184b69f..f3e87dd 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -276,10 +276,6 @@ func getRegionAndProfileAWSSession(regionName *string, profileName *string) (*se sessOpts.Config.Region = regionName } - if awsProfileName, ok := os.LookupEnv(awsProfileEnvVar); profileName == nil && ok && awsProfileName != "" { - profileName = &awsProfileName - } - if profileName != nil { sessOpts.Profile = *profileName if sessOpts.Config.Region == nil { From 0c347c18e225b5f66056ae309d8a53b50918fd90 Mon Sep 17 00:00:00 2001 From: "Carlos Manzanedo Rueda (ruecarlo@)" Date: Mon, 22 Jun 2020 11:25:51 +0100 Subject: [PATCH 4/4] removing unused const AWS_PROFILE --- cmd/main.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cmd/main.go b/cmd/main.go index f3e87dd..d9250cb 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -32,7 +32,6 @@ import ( const ( binName = "ec2-instance-selector" awsRegionEnvVar = "AWS_REGION" - awsProfileEnvVar = "AWS_PROFILE" defaultRegionEnvVar = "AWS_DEFAULT_REGION" defaultProfile = "default" awsConfigFile = "~/.aws/config"