向同一账户中的 iam 用户授予 aws iam 角色权限

Grant aws iam role permissions to an iam user in same account

我有一个具有 s3 只读权限的 AWS 角色。我已经为 AWS 用户配置了 aws cli。所以我想使用同一个用户在 aws cli 中浏览 s3 文件。 我所做的是 为 root 用户添加了角色 arn:aws:iam::<1234...>:role/test-role 的信任关系,这样我就可以将其提供给我的所有 iam 用户

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Principal": {
        "AWS": "arn:aws:iam::<1234..>:root",
        "Service": "s3.amazonaws.com"
      },
      "Action": "sts:AssumeRole"
    }
  ]
}

然后,我为用户添加了一个策略来承担上述角色。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "Stmt12345",
            "Effect": "Allow",
            "Action": [
                "sts:AssumeRole"
            ],
            "Resource": "arn:aws:iam::<1234...>:role/test-role"
        }
    ]
}

当我尝试列出时,出现权限被拒绝错误。

aws s3 ls

An error occurred (AccessDenied) when calling the ListObjects operation: Access Denied

我确保该角色具有完整的 s3 读取权限,如下所示。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Action": [
                "s3:Get*",
                "s3:List*"
            ],
            "Resource": "*"
        }
    ]
}

谁能指导一下问题出在哪里?

如果您使用的是 CLI,则需要具有正确凭据的配置文件。

您应该将您的凭据保存在 .aws/credentials 文件中,例如:

[myprofile]
aws_access_key_id =  ... access key ...
aws_secret_access_key = ... secret access key …

然后您可以将假定角色的配置文件添加到 .aws/config 文件,例如:

[profile test-role]
source_profile=myprofile
role_arn = arn:aws:iam::<1234...>:role/test-role

最后,您在 运行 CLI 命令

之前将 AWS_PROFILE 设置为 test-role
SET AWS_PROFILE=test-role
aws s3 ls

我本来会发布一个 link to the AWS documentation,但该站点不赞成仅 link 的答案。