Skip to content

Instantly share code, notes, and snippets.

@jqn
Forked from wjimenez5271/read_aws_credentials.py
Created December 10, 2019 14:19
Show Gist options
  • Save jqn/fa7ec283283cdaaa2f07666c36233561 to your computer and use it in GitHub Desktop.
Save jqn/fa7ec283283cdaaa2f07666c36233561 to your computer and use it in GitHub Desktop.
Simple python 2.7 implementation of a config reader for ~/.aws/credentials file
def get_profile_credentials(profile_name):
from ConfigParser import ConfigParser
from ConfigParser import ParsingError
from ConfigParser import NoOptionError
from ConfigParser import NoSectionError
from os import path
config = ConfigParser()
config.read([path.join(path.expanduser("~"),'.aws/credentials')])
try:
aws_access_key_id = config.get(profile_name, 'aws_access_key_id')
aws_secret_access_key = config.get(profile_name, 'aws_secret_access_key')
except ParsingError:
print('Error parsing config file')
raise
except (NoSectionError, NoOptionError):
try:
aws_access_key_id = config.get('default', 'aws_access_key_id')
aws_secret_access_key = config.get('default', 'aws_secret_access_key')
except (NoSectionError, NoOptionError):
print('Unable to find valid AWS credentials')
raise
return aws_access_key_id, aws_secret_access_key
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment