Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to choose 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.

like image 435
Nader A. Jabbar Avatar asked Oct 27 '15 21:10

Nader A. Jabbar


People also ask

How do I link my AWS account to Boto3?

Step 1: Configure a local AWS CLI profile. Step 2: Establish a Boto3 session object. Step 3: Incorporate the Boto3 session into the Braket AwsSession.

What is my AWS profile?

A named profile is a collection of settings and credentials that you can apply to a AWS CLI command. When you specify a profile to run a command, the settings and credentials are used to run that command. Multiple named profiles can be stored in the config and credentials files.

Which credentials does Boto3 use?

Configuring credentials There are two types of configuration data in Boto3: credentials and non-credentials. Credentials include items such as aws_access_key_id , aws_secret_access_key , and aws_session_token .

Which method in Boto3 is used to specify the AWS services?

Boto3 comes with 'waiters', which automatically poll for pre-defined status changes in AWS resources.

How do I use boto3 with AWS CLI?

In this case, Boto3 uses credentials that you have used when setting up a default profile while configuring AWS CLI. You can learn more about how to configure AWS CLI here. Once you have configured AWS CLI, you can directly use boto3 to create a service client or resource.

Can we use boto3 to create a service client or resource?

Once you have configured AWS CLI, you can directly use boto3 to create a service client or resource. But this approach has the same drawback, what if when we have multiple user profiles? Can we tell boto3 which profile to use when connecting to AWS?

What is the use of boto3?

Boto3 is python’s library to interact with AWS services. When we want to use AWS services we need to provide security credentials of our user to boto3.

How to run boto3 in Python?

Running Boto3 in python, it uses the default AWS profile. However, there is an option to use a different one that we will show you how in the following lines of code. Let’s suppose that we want to use the comprehend service and the profile name is ‘billy’. session = boto3.session.Session (profile_name='billy')


4 Answers

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')
like image 132
Jordon Phillips Avatar answered Oct 05 '22 07:10

Jordon Phillips


This section of the boto3 documentation is helpful.

Here's what worked for me:

session = boto3.Session(profile_name='dev')
client = session.client('cloudfront')
like image 25
mgig Avatar answered Oct 05 '22 07:10

mgig


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)
like image 42
asmaier Avatar answered Oct 05 '22 08:10

asmaier


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

like image 33
MrKulli Avatar answered Oct 05 '22 08:10

MrKulli