This repository was archived by the owner on Apr 24, 2019. It is now read-only.
File tree 1 file changed +21
-1
lines changed
1 file changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,22 @@ def _get_aws_credentials(self):
147
147
148
148
return credentials
149
149
150
+ def _get_aws_region_from_config (self ):
151
+ """
152
+ Get the default region from a pre-configured AWS CLI installation
153
+ """
154
+ region = None
155
+ aws_config_path = [ '{}/.aws/config' .format (os .environ ['HOME' ]), "{}\.aws\config" .format (os .environ ['HOME' ]) ]
156
+ for path in aws_config_path :
157
+ if os .path .exists (path ):
158
+ self ._log ("Reading AWS config from {}" .format (path ))
159
+ with open (path ) as fh :
160
+ for line in fh :
161
+ if line .startswith ('region' ):
162
+ region = line .split ('=' )[- 1 ].strip ()
163
+
164
+ return region
165
+
150
166
def _connect_to_deep_security (self ):
151
167
dsm = None
152
168
if self .args .ignore_ssl_validation :
@@ -185,8 +201,12 @@ def _connect_to_aws_service(self, service_name):
185
201
(shared by the AWS CLI) or an instance role
186
202
"""
187
203
service = None
204
+
205
+ region = self .args .aws_region # prefer explicit region vs. CLI config
206
+ if not region : region = self ._get_aws_region_from_config ()
207
+
188
208
try :
189
- aws = boto3 .session .Session (aws_access_key_id = self .aws_credentials ['aws_access_key_id' ], aws_secret_access_key = self .aws_credentials ['aws_secret_access_key' ], region_name = self . args . aws_region )
209
+ aws = boto3 .session .Session (aws_access_key_id = self .aws_credentials ['aws_access_key_id' ], aws_secret_access_key = self .aws_credentials ['aws_secret_access_key' ], region_name = region )
190
210
service = aws .client (service_name )
191
211
self ._log ("Connected to AWS {}" .format (service_name ))
192
212
except Exception , err :
You can’t perform that action at this time.
0 commit comments