1

Boto and aws-cli users write ini-style config in one or more files (e.g. ~/.aws/config) under a series of different profiles. I am writing a library which wraps boto API calls, but I would like to change its behavior based on the user's active profile settings (i.e. AWS_PROFILE and friends).

How does one programmatically retrieve the effective user's profile settings (i.e. boto defaults, overridden with the user's profile settings, overridden by whatever environment variable have precedence). I am particularly interested in the "region" key of the profile, but I suspect the procedure would be similar for other keys.

Another way of phrasing this, would be: is there a boto API call (or series of calls) that will retrieve the "currently active / effective" settings for a given boto Session?

As a counterexample: A very crude, brittle, and incorrect way to extract the user's region settings would to inspect the environment AWS_PROFILE, look for ~.aws/config, and parse the key "region" out of it. I'd rather let boto apply its own rules, and extract the result.

init_js
  • 4,143
  • 2
  • 23
  • 53
  • What do you mean by "retrieve settings for the current session"? Do you wish to view the content of the file itself, or do you wish to use one of the profiles to call AWS? Feel free to Edit your question to clarify what you are seeking. – John Rotenstein Jun 08 '19 at 02:56
  • updated question. I meant _effective_, I didn't mean to imply a temporal constraint. As an example, s3.create_bucket() [uses "us-east-1"](https://github.com/boto/boto3/issues/125) as the home region. I'd like my library to specify a LocationConstraint parameter that is closer to the user's active region setting (effective at the time of the create_bucket call), without forcing the user to explicitly re-state what is already in their ini files. – init_js Jun 10 '19 at 09:04

3 Answers3

0

You can do this by reading the AWS_PROFILE environment variable and passing it into the boto3 Session object.

enter image description here

Chris McKinnel
  • 14,694
  • 6
  • 64
  • 67
  • 1
    Thanks. Can you comment on robustness? Is AWS_PROFILE the only environment setting that determines which profile is used (when the profile_name key is omitted from the Session, Resource, or Client constructors)? e.g. If `AWS_PROFILE` was undefined, which profile would apply then? ("default" ?) – init_js Jun 10 '19 at 09:12
0

The configuration can be loaded from many different places, and simply looking at "AWS_PROFILE" isn't correct.

See the list of configuration sources that get applied in the calculation of the configuration: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html

One easy way is to simply create a default session (which applies many of the configuration sources), and extract the settings from the created object:

import boto3
session = boto3.Session()
print(session.region_name)
aimme
  • 6,385
  • 7
  • 48
  • 65
init_js
  • 4,143
  • 2
  • 23
  • 53
0

I haven't tested, but I think this is a good starting point:

https://stackoverflow.com/a/41270744/220949

In particular for the region, there is a region_name property in the Session

arod
  • 13,481
  • 6
  • 31
  • 39