Boto3: Using non-default profile

Created on 15 Nov 2014  路  8Comments  路  Source: boto/boto3

Is there a simple way to use a non-default profile by specifying it's name?
Like on boto there is

boto.connect_ec2(profile_name="dev-profile")

I see that I can construct a session with credentials and region, but I don't see an ability to just refer to the profile's name

feature-request pending-release question

Most helpful comment

@revmischa This is what I was able to come up with. Should print out all available boto profiles.

#!/usr/bin/env python

import boto3

session = boto3.Session()
print session.available_profiles

All 8 comments

@zen4ever unfortunately this isn't exposed in this way yet, but I do think it's something we should add. Here are a couple of workarounds that work right now:

  1. Set the BOTO_DEFAULT_PROFILE environment variable to the name of the profile. Docs for env vars
  2. Use your own Botocore session:
import botocore.session
import boto3

# Set your profile name on a low-level Botocore session
session = botocore.session.get_session()
session.profile = 'my-profile-name'

# Tell Boto 3 to use that session by default
boto3.setup_default_session(botocore_session=session)

# Now create a resource
ec2 = boto3.resource('ec2')

Piping a profile name in through the client and resource calls will require some changes on the Botocore side, so I'll investigate how best to approach this. Thanks for the feedback, and sorry I overlooked this use case! :+1:

Nice. Botocore session seems like a good workaround for now. :-)

@zen4ever it's now possible to set a profile name without dropping down to Botocore sessions. Please take a look at the docs in #69 and let me know if you have any other questions!

Great, thanks :+1:

How do you get a list of available profiles?

@revmischa You can look at your config file at ~/.aws/config or its corresponding ~/.aws/credentials

That's a terrible answer

@revmischa This is what I was able to come up with. Should print out all available boto profiles.

#!/usr/bin/env python

import boto3

session = boto3.Session()
print session.available_profiles
Was this page helpful?
0 / 5 - 0 ratings