Skip to content
This repository was archived by the owner on Apr 24, 2019. It is now read-only.

Commit 5ca4816

Browse files
committed
Script now reads default region from AWS CLI unless overridden by argument --aws-region. Closes issue #6
1 parent c9152fd commit 5ca4816

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

lib/core.py

+21-1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ def _get_aws_credentials(self):
147147

148148
return credentials
149149

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+
150166
def _connect_to_deep_security(self):
151167
dsm = None
152168
if self.args.ignore_ssl_validation:
@@ -185,8 +201,12 @@ def _connect_to_aws_service(self, service_name):
185201
(shared by the AWS CLI) or an instance role
186202
"""
187203
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+
188208
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)
190210
service = aws.client(service_name)
191211
self._log("Connected to AWS {}".format(service_name))
192212
except Exception, err:

0 commit comments

Comments
 (0)