275

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.

2
  • See also: read and download a file from AWS S3 with profiles Aug 23, 2018 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 = '<access-key>', aws_secret_access_key = '<secret-key>') Sep 25, 2019 at 14:32

5 Answers 5

447

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')
7
  • 2
    Shouldn't the env variable be AWS_PROFILE?
    – Stefano M
    Oct 28, 2015 at 22:11
  • 71
    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') Sep 21, 2016 at 19:48
  • 1
    off topic, ipython was also useful for me.
    – Mike D
    May 15, 2018 at 13:17
  • 14
    Get the profile list using boto3.session.Session().available_profiles - it is a list. Then use the one you want @jordan-phillips. Aug 20, 2018 at 13:22
  • 1
    What is the difference between boto3.Session and boto3.session.Session? Both methods work for me.
    – Wei Chen
    Jul 10, 2021 at 11:01
91

This section of the boto3 documentation is helpful.

Here's what worked for me:

session = boto3.Session(profile_name='dev')
client = session.client('cloudfront')
1
  • 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, 2021 at 0:15
74

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)
9

1- To use Session boto3.session.Session:

import boto3
aws_session = boto3.session.Session(profile_name='dev')
s3 = aws_session.resource('s3')

2- To use resource boto3.resource:

import boto3
boto3.setup_default_session(profile_name='dev')
s3 = boto3.resource('s3')

3- OR, Pass environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY to boto3.

import boto3
aws_session = boto3.Session(
    aws_access_key_id=settings.AWS_SERVER_PUBLIC_KEY,
    aws_secret_access_key=settings.AWS_SERVER_SECRET_KEY,
)
s3 = aws_session.resource('s3')
8

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

1
  • This worked (with decryption). Splitting this over two commands didn't.
    – M3RS
    Aug 17, 2021 at 12:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.