简体   繁体   中英

Switching AWS Accounts in Python

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM