214

I am using the Boto 3 python library, and want to connect to AWS CloudFront. I need to specify the correct AWS Profile (AWS Credentials), but looking at the official documentation, I see no way to specify it.

I am initializing the client using the code: client = boto3.client('cloudfront')

However, this results in it using the default profile to connect. I couldn't find a method where I can specify which profile to use.

Abdullah Khawer
  • 3,566
  • 4
  • 21
  • 52
Nader A. Jabbar
  • 2,326
  • 2
  • 11
  • 6
  • See also: [read](https://stackoverflow.com/q/36205481/562769) and [download](https://stackoverflow.com/a/51993119/562769) a file from AWS S3 with profiles – Martin Thoma Aug 23 '18 at 19:44
  • have you tried using the keys into the code? (also you can use env var to hide it from the code) `client = boto3.client('s3', aws_access_key_id = '', aws_secret_access_key = '')` – Ivan Carrasco Quiroz Sep 25 '19 at 14:32

4 Answers4

366

I think the docs aren't wonderful at exposing how to do this. It has been a supported feature for some time, however, and there are some details in this pull request.

So there are three different ways to do this:

Option A) Create a new session with the profile

    dev = boto3.session.Session(profile_name='dev')

Option B) Change the profile of the default session in code

    boto3.setup_default_session(profile_name='dev')

Option C) Change the profile of the default session with an environment variable

    $ AWS_PROFILE=dev ipython
    >>> import boto3
    >>> s3dev = boto3.resource('s3')
Alex Ramos
  • 73
  • 9
Jordon Phillips
  • 13,094
  • 4
  • 33
  • 42
  • 2
    Shouldn't the env variable be AWS_PROFILE? – Stefano M Oct 28 '15 at 22:11
  • Thanks for that! didn't seem to find that information anywhere so far. It seems I only needed step 2 to make this work though. What did step 1 do? (since the dev variable isn't used or passed into anything else?) – Mark Sep 21 '16 at 15:55
  • 60
    Those are options, not steps. In the first option you create a new session to use rather than the default session. So to create a client with that session you would do something like `dev.client('s3')` instead of `boto3.client('s3')` – Jordon Phillips Sep 21 '16 at 19:48
  • 1
    off topic, `ipython` was also useful for me. – Mike D May 15 '18 at 13:17
  • 13
    Get the profile list using `boto3.session.Session().available_profiles` - it is a list. Then use the one you want @jordan-phillips. – Daisuke Aramaki Aug 20 '18 at 13:22
  • This works when you have two separate AWS accounts you need to connect to as well. In `~/.aws/credentials` underneath the `[default]` entry, create a new entry named `['dev']` with the separate access keys. End results will be two sets of access keys, one file. Then specify the session in python like above: `dev = boto3.session.Session(profile_name='dev')` – Cale Sweeney Dec 19 '18 at 20:15
  • What is the difference between `boto3.Session` and `boto3.session.Session`? Both methods work for me. – Wei Chen Jul 10 '21 at 11:01
63

Do this to use a profile with name 'dev':

session = boto3.session.Session(profile_name='dev')
s3 = session.resource('s3')
for bucket in s3.buckets.all():
    print(bucket.name)
asmaier
  • 10,347
  • 10
  • 72
  • 97
60

This section of the boto3 documentation is helpful.

Here's what worked for me:

session = boto3.Session(profile_name='dev')
client = session.client('cloudfront')
He3lixxx
  • 2,533
  • 1
  • 8
  • 22
mgig
  • 1,965
  • 4
  • 18
  • 35
  • I really thought this was going to work for me in my work with Secrets Manager. But Secrets Manager + KMS = nope. – Br.Bill Jul 24 '21 at 00:15
8

Just add profile to session configuration before client call. boto3.session.Session(profile_name='YOUR_PROFILE_NAME').client('cloudwatch')

MrKulli
  • 719
  • 10
  • 18