【问题标题】:Can you get the AWS account name from boto?您可以从 boto 获取 AWS 账户名称吗?
【发布时间】:2021-01-03 18:43:51
【问题描述】:

我有一个 AWS 密钥和秘密密钥,想致电 boto 获取帐户名称。

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

【问题讨论】:

标签: python python-2.7 amazon-web-services boto3 boto


【解决方案1】:

在boto3中获取AWS账户别名:

alias = boto3.client('iam').list_account_aliases()['AccountAliases'][0]

获取账号ID(账号):

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

【讨论】:

    【解决方案2】:

    来自Get AWS Account ID from Boto

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

    然后

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

    【讨论】:

    • 获取名称导致 botocore.exceptions.ParamValidationError
    • 这仅在帐户是任何组织的成员时才有用。
    【解决方案3】:

    只有在您使用 IAM 并且想要检索该别名时才有可能。如果您有 root 凭据,则无法检索帐户名称。

    相关调用为:get_account_alias()

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

    【讨论】:

    • 别名与账户名不同。
    【解决方案4】:

    它已经晚了,但可能对未来有所帮助。 如果您使用的是组织服务,则可以使用以下代码获取 Account Name。

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

    For more info

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-16
      • 1970-01-01
      • 1970-01-01
      • 2013-10-02
      • 1970-01-01
      • 1970-01-01
      • 2010-11-28
      • 1970-01-01
      相关资源
      最近更新 更多