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.
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.
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.
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 .
Boto3 comes with 'waiters', which automatically poll for pre-defined status changes in AWS resources.
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.
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?
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.
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')
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')
This section of the boto3 documentation is helpful.
Here's what worked for me:
session = boto3.Session(profile_name='dev')
client = session.client('cloudfront')
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)
Just add profile to session configuration before client call.
boto3.session.Session(profile_name='YOUR_PROFILE_NAME').client('cloudwatch')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With