amazon-web-services - 如何将 MFA 与 AWS CLI 结合使用?

标签 amazon-web-services

使用 AWS CLI 时如何输入 MFA 代码?我查看了IAM的文档页面http://docs.aws.amazon.com/cli/latest/reference/iam/index.html .

我已在我的用户名下启用了 MFA 设备。

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

返回

{
"MFADevices": [
    {
        "UserName": "X", 
        "SerialNumber": "arn:aws:iam::+++:mfa/X", 
        "EnableDate": "2016-01-13T23:15:43Z"
    }
]
}

最佳答案

如果您使用角色,CLI 可以为您管理很多工作。此处描述:http://docs.aws.amazon.com/cli/latest/userguide/cli-roles.html

在我的凭据文件中,我有:

[my_iam_user]
aws_access_key_id = AKIABLAHBLAHBLAHBLAH
aws_secret_access_key = <blah>
region = us-east-1

[my_admin_role]
role_arn = arn:aws:iam::123456789123:role/my_admin_role
source_profile = my_iam_user
mfa_serial = arn:aws:iam::123456789123:mfa/my_iam_user
region = us-east-1

请注意 mfa_serial 条目。您可以从 AWS IAM 控制台中的用户详细信息获取此值。此条目告诉 CLI 该角色需要 MFA。

当我调用 aws s3 ls --profile my_admin_role 时,它显示输入 MFA 代码:,在我粘贴代码后,它会返回列表。

注意:我还没有找到让 CLI 在调用用户配置文件 (--profile my_iam_user) 时请求 MFA 的方法,只有调用角色配置文件才会触发 MFA 请求.

然后,MFA token 将被结转,并且用户配置文件也可以使用:

aws sts get-caller-identity --profile my_iam_user
 # {
 # "Account": "123456789123",
 # "UserId": "AIDABLAHBLAHBLAHBLAH",
 # "Arn": "arn:aws:iam::123456789123:user/my_iam_user"
 # }

aws sts get-caller-identity --profile my_admin_role
 # {
 # "Account": "123456789123",
 # "UserId": "AROABLAHBLAHBLAHBLAH:AWS-CLI-session-1234567890",
 # "Arn": "arn:aws:sts::123456789123:assumed-role/my_admin_role/AWS-CLI-session-1234567890"
 # }

关于amazon-web-services - 如何将 MFA 与 AWS CLI 结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34795780/

相关文章:

linux - 如何在 AWS 上配置 d2.xlarge linux 实例

amazon-web-services - 如何在AWS Cloudformation中检索带有随机后缀的 secret arn

rest - AWS IoT - 通过 .Net、REST 和证书访问影子

amazon-web-services - 尽管权限 = 允许所有,但拒绝从 Athena 访问 S3 存储桶

java - 是否存在使用 AWS SDK InitiateAuth 的 Camel 组件?

amazon-web-services - 通过 ID 查找已弃用 AMI 的类型

amazon-web-services - 从S3上传AWS Lambda和zip

amazon-web-services - ECS 服务在创建时超时

amazon-web-services - 从 CloudFormation 参数获取输入并将其传递给从 S3 下载的脚本

amazon-web-services - AWS 中长期运行的订阅后台服务的最佳位置