您可以从boto获取AWS账户名称吗?

问题描述 投票:0回答:4

我有 AWS 密钥和秘密密钥,想致电

boto
获取账户名称。

我可以获得账户 ID,但 AWS 账户名称是个谜。

python python-2.7 amazon-web-services boto3 boto
4个回答
26
投票

要在

boto3
中获取 AWS 账户别名:

alias = boto3.client('iam').list_account_aliases()['AccountAliases'][0]
获取账户ID(账号):

id = boto3.client('sts').get_caller_identity().get('Account')
    

7
投票
来自

从 Boto 获取 AWS 账户 ID

id = boto3.client('sts').get_caller_identity().get('Account')

然后

name = boto3.client('organizations').describe_account(AccountId=id).get('Account').get('Name')
    

3
投票
虽然已经晚了,但可能对未来有帮助。 如果您正在使用组织服务,则使用以下代码您可以获取

帐户名称

org = boto3.client('organizations') account_name = org.describe_account(AccountId='account-id').get('Account') print(account_name ['Name'])

了解更多信息


2
投票
仅当您使用 IAM 并且想要检索该别名时才有可能。如果您有 root 凭据,则无法检索帐户名。

相关电话是:

get_account_alias()



http://boto.readthedocs.org/en/latest/ref/iam.html#boto.iam.connection.IAMConnection.get_account_alias

© www.soinside.com 2019 - 2024. All rights reserved.