Nader A. Jabbar Nader A. Jabbar - 1 year ago 343
Python Question

How to chose an AWS profile when using boto3 to connect to CloudFront

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.

Answer Source

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 ways to do this:

  1. Create a new session with the profile

    dev = boto3.session.Session(profile_name='dev')
  2. Change the profile of the default session in code

  3. Change the profile of the default session with an environment variable

    $ AWS_PROFILE=dev ipython
    >>> import boto3
    >>> s3dev = boto3.resource('s3')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download