Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you list local profiles with boto3 from ~/.aws/.credentials and ~/.aws/.config files?

I would like to list all of my local profiles using boto3, as I think boto3 is not picking up my credentials correctly.

I have tried the following:

import boto3

boto3.Session.available_profiles

Which doesn't give me a list, but a property object.

like image 740
apollowebdesigns Avatar asked Nov 24 '20 11:11

apollowebdesigns


People also ask

Where is the ~/ AWS config file?

The config file is located at ~/. aws/config on Linux or macOS, or at C:\Users\ USERNAME \. aws\config on Windows. This file contains the configuration settings for the default profile and any named profiles.

How do you use credentials on boto3?

You can interact with any AWS service using Boto3 when you're programming with python if you have the access and the appropriate credentials. You can specify credentials in boto3 using session = boto3. Session(aws_access_key_id= '<your_access_key_id>' , aws_secret_access_key= '<your_secret_access_key>' ) .

How do I set up my boto3 credentials for AWS?

Boto3 credentials can be configured in multiple ways. Regardless of the source or sources that you choose, you must have both AWS credentials and an AWS Region set in order to make requests. If you have the AWS CLI, then you can use its interactive configure command to set up your credentials and default region:

How does boto3 search for credentials?

Boto3 will look in several locations when searching for credentials. The mechanism in which Boto3 looks for credentials is to search through a list of possible locations and stop as soon as it finds credentials. The order in which Boto3 searches for credentials is: Passing credentials as parameters in the boto.client()method

What is boto3 AWS SDK?

Boto3 is an AWS SDK for python. You can interact with any AWS service using Boto3 when you’re programming with python if you have the access and the appropriate credentials. You can specify credentials when connecting to AWS in four different ways. Passing credentials as parameters

How do I read credentials from the AWS CLI?

The AWS CLI can also read credentials from the config file. You can keep all of your profile settings in a single file. If there are ever credentials in both locations for a profile (say you used aws configure to update the profile's keys), the keys in the credentials file take precedence.


Video Answer


1 Answers

You might want to use awscli instead of boto3 to list your profiles.

aws configure list

This should output something like this:

   Name                    Value             Type    Location
      ----                    -----             ----    --------
   profile                <not set>             None    None
access_key     ****************ABCD      config_file    ~/.aws/config
secret_key     ****************ABCD      config_file    ~/.aws/config
    region                us-west-2              env    AWS_DEFAULT_REGION

As for boto3, try this:

for profile in boto3.session.Session().available_profiles:
    print(profile)
like image 98
baduker Avatar answered Nov 14 '22 23:11

baduker