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

Commit

Permalink
Script now reads default region from AWS CLI unless overridden by arg…
Browse files Browse the repository at this point in the history
…ument --aws-region. Closes issue #6
  • Loading branch information
marknca committed Mar 20, 2016
1 parent c9152fd commit 5ca4816
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion lib/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ def _get_aws_credentials(self):

return credentials

def _get_aws_region_from_config(self):
"""
Get the default region from a pre-configured AWS CLI installation
"""
region = None
aws_config_path = [ '{}/.aws/config'.format(os.environ['HOME']), "{}\.aws\config".format(os.environ['HOME']) ]
for path in aws_config_path:
if os.path.exists(path):
self._log("Reading AWS config from {}".format(path))
with open(path) as fh:
for line in fh:
if line.startswith('region'):
region = line.split('=')[-1].strip()

return region

def _connect_to_deep_security(self):
dsm = None
if self.args.ignore_ssl_validation:
Expand Down Expand Up @@ -185,8 +201,12 @@ def _connect_to_aws_service(self, service_name):
(shared by the AWS CLI) or an instance role
"""
service = None

region = self.args.aws_region # prefer explicit region vs. CLI config
if not region: region = self._get_aws_region_from_config()

try:
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)
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)
service = aws.client(service_name)
self._log("Connected to AWS {}".format(service_name))
except Exception, err:
Expand Down

0 comments on commit 5ca4816

Please sign in to comment.