首页 > 解决方案 > 通过 AWS CLI 使用临时安全凭证

问题描述

我想在 AWS CLI 中生成临时凭证,我知道应该使用以下命令。

$ aws sts assume-role --role-arn arn:aws:iam::123456789012:role/role-name --role-session-name "RoleSession1" --profile IAM-user-name > assume-role-output.txt

我还随身带着访问密钥和密钥。

我也跑了aws sts get-caller-identity,得到:

{
    "Account": "12345",
    "UserId": "AIXXXXXXXNUNHY",
    "Arn": "arn:aws:iam::12345:user/dcp/ua.166666654"
}

但我不明白我应该在上述命令中替换哪些值来获取临时凭据。

标签: amazon-web-servicesamazon-s3command-line

解决方案


您的调用assume-role将返回类似于以下内容的信息:

{
    "AssumedRoleUser": {
        "AssumedRoleId": "AROAxx:RoleSession1", 
        "Arn": "arn:aws:sts::123456789012:assumed-role/role/role-name"
    }, 
    "Credentials": {
        "SecretAccessKey": "xxx", 
        "SessionToken": "xxx", 
        "Expiration": "2018-11-13T07:50:29Z", 
        "AccessKeyId": "ASIAxxx"
    }
}

然后,运行:

aws configure --profile RoleSession1

并输入assume-role(Access Key, Secret Key, Token) 提供的详细信息。

这会将新凭据存储在标题.aws/credentials下的文件中。[RoleSession1]

要使用这些凭据,请指定您刚刚定义的配置文件:

aws s3 ls --profile RoleSession1

推荐阅读