【问题标题】:AWS CLI - Get account name from profile name?AWS CLI - 从配置文件名称中获取账户名称?
【发布时间】:2019-10-02 09:38:59
【问题描述】:

我正在尝试为我的 aws 事物创建部署脚本。

我们为生产和开发创建了单独的帐户,我想传入一个参数,用户可以在其中指定要部署到哪个环境。由于配置文件将他们与他们正在使用的帐户联系起来,有没有办法在命令行上获取给定配置文件名称的帐户名称?

【问题讨论】:

  • 您是否考虑过使用 IaC 和 Terraform 或 AWS 原生 CloudFormation 之类的工具。这确实是实现自动化部署的方法。
  • @Dude0001 对我来说是新的

标签: amazon-web-services aws-cli


【解决方案1】:

您可以使用sts get-caller-identity 获取帐户ID。这通常可以在没有扩展权限的情况下完成,但需要将帐户 ID 映射到您的帐户名称的非直观映射,但在某些情况下它很有用,因为它具有确定性并且在帐户之间是唯一的。

$ aws --profile profilename sts get-caller-identity
{
    "Account": "#####", 
    "UserId": "ABC", 
    "Arn": "arn:aws:iam::#####:user.name"
}

你也可以使用iam list-account-aliases,这样比较好用:

$ aws --profile profilename iam list-account-aliases
{
    "AccountAliases": [
        "my-company-account-alias"
    ]
}

【讨论】:

  • 后面的命令在这里似乎更相关,因为get-caller-identity 只输出帐户 ID,而不是其名称。
【解决方案2】:

describe-account 命令将为您提供帐户名称

https://docs.aws.amazon.com/cli/latest/reference/organizations/describe-account.html

不清楚这如何帮助您实现自动化部署的目标,但我认为它回答了您的问题。

【讨论】:

    猜你喜欢
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-02
    • 2021-02-19
    相关资源
    最近更新 更多