【问题标题】:How to use AWS CLI for multiple accounts如何为多个账户使用 AWS CLI
【发布时间】:2019-11-02 10:44:45
【问题描述】:

我有一个根帐户,它是 1234(帐户 ID),我启用了 MFA 身份验证,我的用户名是 myuser12。我可以使用帐户 ID 和用户名以及密码登录。它会询问 MFA 令牌代码。一旦我输入了令牌代码。我可以在我的根帐户下查看特定位置的实例。

我可以使用我的角色(在我的情况下,所有帐户的角色相同)以及任何别名来将角色切换到新帐户 (5678)。切换角色完成后,我可以在特定位置看到新帐户下的所有实例。

通过 Web 控制台一切正常。当我尝试通过 CLI 访问它们时,我无法获得新帐户的结果 (5678)。但它适用于 1234 帐户位置。

Account ID - Location   - No. of instances

1234       - us-east-1  - 5 instances

1234       - eu-west-3   - 2 instances

5678       - eu-north-1  - 5 instances

5678       - eu-west-1   - 5 instances

等等。

我的初始.aws/config 内容

[profile default]
region = us-east-1
output = json

.aws/credentials 内容,

[default]
aws_access_key_id = accesskeyIdFromDownloadedCSV
aws_secrect_access_key = secrectAccessKeyFromDownloadedCSV

现在,如果我尝试查询任何实例,我会收到错误消息,因为我没有使用 MFA 生成访问密钥令牌。

aws rds describe-db-instances --query 'DBInstances[?DBInstanceClass==`db.m3.large`]' --profile default --no-verify-ssl

所以,我必须获取我的 MFA arn url 并获取即时访问密钥 ID、秘密访问密钥和会话令牌。

aws iam list-mfa-devices --user-name myuser12

我从上面的命令中获得了“序列号”(MFA arn url),稍后我需要它来生成会话令牌。

现在,

aws sts get-session-token --serial-number arn:aws:iam::1234:mfa/myuser12 --token-code 123456

注意:123456(令牌代码)从我的虚拟 MFA 设备生成

现在我有来自上述命令的 SessionToken (aws_session_token)、SecrectAccessKey (aws_secret_access_key)、AccessKeyId (aws_access_key_id) 以及到期时间戳。

因此,我已根据新凭据更新了 .aws/config.aws/credential 文件。

.aws/config文件内容

[profile default]
region = us-east-1
output = json

[profile newacc]
region = eu-west-1
role_arn = arn:aws:iam::5678:role/myRole   
source_profile default                     
output = json

.aws/credentials 内容,

[default]
aws_access_key_id = NewIdFromMFACommand
aws_secrect_access_key = NewKeyFromMFACommand
aws_aws_session_token = TokenFromMFACommand      

[newacc]
region = eu-west-1
role_arn = arn:aws:iam::5678:role/myRole   
source_profile default                     
output = json

现在,我可以执行我的初始命令了

aws rds describe-db-instances --query 'DBInstances[?DBInstanceClass==`db.m3.large`]' --profile default --no-verify-ssl

并且,获取db.m3.large 类型实例的列表。

但是,当我尝试使用--profile newacc 时,我什么也没得到。我真的很困惑要改变什么以及在哪里改变?

【问题讨论】:

  • 当你说你“没有得到任何东西”时,你的意思是使用配置文件 newacc 调用 describe-db-instances 成功,但它返回了零个数据库实例?
  • 不,它没有通过。花了几秒钟,什么也没返回。甚至没有空的 json 数组。
  • 如果是凭证错误,那么您会看到。如果没有匹配的数据库实例,那么您应该看到 [] 作为响应。您能否确保您的 awscli 是最新的。一个问题:你为什么要指明 --no-verify-ssl?
  • 您想通过临时凭证担任角色吗?这可能是导致问题的原因。我建议从get-session-token 获取结果并通过aws configure --profile sts 将它们存储在不同的 配置文件中,从而保留您的默认凭据。然后,当您使用--profile newacc 时,它将使用您的“正常”凭据承担角色。 (或者即使担任职务也需要 MFA?)
  • @jarmod ,它显示了一些 SSL 验证失败错误。所以我用了这个开关。

标签: amazon-web-services aws-cli


【解决方案1】:

终于成功了!看来,我需要为每个帐户运行以下命令。

aws sts assume-role --role-arn "arn:aws:iam::5678:role/MyRoleForThisAccount" --role-session-name AWSCLI-Session

注意:在 IAM 中为您的用户分配相应的角色 (MyRoleForThisAccount),并确保您的用户有权调用 AssumeRole

并更新~/.aws/credentials 文件中每个相应块的结果(凭据)。所以,凭证文件看起来像

[default]
aws_access_key_id = NewIdFromMFACommand
aws_secrect_access_key = NewKeyFromMFACommand
aws_aws_session_token = TokenFromMFACommand      

[newacc]
aws_access_key_id = NewIdFromAssumeRoleCommandForThisAccount
aws_secrect_access_key = NewKeyFromAssumeRoleCommandForThisAccount
aws_aws_session_token = NewTokenFromAssumeRoleCommandForThisAccount

~/.aws/config 文件将是

[profile default]
region = us-east-1
output = json

[profile newacc]
region = eu-west-1

【讨论】:

    【解决方案2】:

    这个配置块:

    [profile newacc]
    region = eu-west-1
    role_arn = arn:aws:iam::5678:role/myRole   
    source_profile default                     
    output = json
    

    表示您希望假设为myRole。但是,代入角色只能由有权代入该角色的 IAM 用户执行。因此,需要一些普通的 IAM 凭证来证明您的身份。这是通过source_profile 完成的。

    source_profile default 行表示default 配置文件中的凭据将用于调用AssumeRole

    我建议您将默认凭证保留为正常的 IAM 凭证。当您调用aws sts get-session-token 时,将结果存储在不同的配置文件中(不是default,不是newacc)。您可以使用aws configure --profile sts(例如)来执行此操作。

    当你想假设myRole,你可以使用--profile newacc。这将使用您的default 凭据调用AssumeRole,并将使用角色的凭据进行调用。

    结果是三个配置文件:

    • default 用于致电 get-session-tokenAssumeRole
    • sts 证明您拥有 MFA
    • newacc 使用角色

    【讨论】:

    • 如果我在 default 块中使用默认凭据,我会收到显式拒绝错误。
    猜你喜欢
    • 2015-05-25
    • 2019-01-23
    • 2021-02-16
    • 2010-10-10
    • 2020-10-19
    • 1970-01-01
    • 2020-09-07
    • 1970-01-01
    • 2014-04-13
    相关资源
    最近更新 更多