首页 > 解决方案 > AWS IAM 用户在访问 ECR 存储库时收到 401,与 root 用户一起使用

问题描述

我已经开始使用 AWS ECR 来存储我的 docker 镜像。当我尝试通过 Powershell 对 IAM 用户进行身份验证时(通过 AWS 命令​​行时也会发生同样的情况),我收到 401:UnAuthorized。如果我使用 root 用户的身份验证密钥/秘密,它可以工作并进行身份验证。

我使用的 PowerShell 脚本是

(Get-ECRLoginCommand).Password | docker login --username AWS --password-stdin 474389077978.dkr.ecr.eu-west-3.amazonaws.com/myreoi

我已将 AWS 用户替换为 IAM 用户。我还向管理员添加了 IAM 用户,但这似乎还不够。

有什么建议吗?谢谢

标签: amazon-web-servicesamazon-ecr

解决方案


必须为 IAM 用户分配一个角色才能访问 ECR 服务。这可以通过在组的权限部分添加内联策略来完成。

请按照以下步骤执行使用非 root IAM 用户可以执行 docker ecr 操作。

1.) Create IAM user say "ecr-user". 
2.) Create IAM group called "ecr-group".
3.) Add user ecr-user to ecr-group.
4.) Create a role "ecr-role"
5.) Attach the policy name "AmazonEC2ContainerServiceRole" to the role ecr-role.

6.) Go in the group section of the AWS console.
7.) Select the group "ecr-group" and go to the permission tab.
    Add policy - "AmazonEC2ContainerServiceRole" using attach policy button. 
8.) "Click here" in the inline policy section of the permission tab.
9.) Choose custom policy.
10.) Choose a name for custom policy - "ecr-passon"
11.) Add policy json given above - ensure to change your account id.

{
"Version": "2012-10-17",
"Statement": [{
    "Effect": "Allow",
    "Action": [
        "iam:GetRole",
        "iam:PassRole"
    ],
    "Resource": "arn:aws:iam::<account-id>:role/ecr-role"
}]}

所有这些步骤将使用策略 AmazonEC2ContainerServiceRole 将角色 ecr-role 附加到组 ecr-group 的 ecr-user。

AWS 编程 IAM 用户必须代入角色才能执行某些操作。

使用参考来了解角色的传递。 将角色传递给 AWS 服务


推荐阅读