3

When using the AWS CLI it references the credentials and config files located in the ~/.aws directory. And you use the --profile flag to indicate which account you want. Such as:

aws ec2 describe-instances --profile=company-lab
aws ec2 describe-instances --profile=company-nonprod 

etc.

But I am new to scripting in python 3 and boto 3 and want to do the same thing there. How can I switch between AWS accounts using python?

2
  • 1
    Consider putting your credentials in enviroment variables, and then switching environments depending on your needs. This will keep sensitive data out of your code. Commented Mar 3, 2019 at 16:13
  • 3
    Possible duplicate of How to choose an AWS profile when using boto3 to connect to CloudFront Commented Mar 3, 2019 at 17:08

1 Answer 1

4

Just use the `profile_nameˋ parameter when creating the session object.

session = boto3.Session(profile_name='dev')
# Any clients created from this session will use credentials
# from the [dev] section of ~/.aws/credentials.
dev_s3_client = session.client('s3')

https://boto3.amazonaws.com/v1/documentation/api/latest/guide/configuration.html

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.