LoginSignup
8
2

More than 1 year has passed since last update.

AWS ECRでAccessDeniedExceptionが発生したときの解決法

Last updated at Posted at 2021-03-03

概要

自作RailsアプリをECSにデプロイするために、ECRにログイン時に発生しました。

$ aws ecr get-login-password --region ap-northeast-1

An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation: User: arn:aws:iam::125724222608:user/ecr_replication is not authorized to perform: ecr:GetAuthorizationToken on resource: *

環境

$ aws --version
aws-cli/2.1.26 Python/3.9.1 Darwin/20.2.0 source/x86_64 prompt/off

解決策(IAMを作成して、ECR用のポリシーをアタッチする)

Amazon ECR エラーメッセージのトラブルシューティングに詳細は記載されていますが、AccessDeniedException が発生した場合は下記のどちらかか原因です。

  • ECRを使用するアクセス権限がユーザーに付与されていない
  • アクセス権限が正しく設定されていない

そこで、「アクセス権限が正しく設定されていない」に対しては、適切なポリシー(下記)を作成し、ECRへのデプロイ用IAMロールにアタッチしました。

{
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": [
            "ecr:GetAuthorizationToken",
            "ecr:BatchCheckLayerAvailability",
            "ecr:GetDownloadUrlForLayer",
            "ecr:GetRepositoryPolicy",
            "ecr:DescribeRepositories",
            "ecr:ListImages",
            "ecr:DescribeImages",
            "ecr:BatchGetImage",
            "ecr:InitiateLayerUpload",
            "ecr:UploadLayerPart",
            "ecr:CompleteLayerUpload",
            "ecr:PutImage"
],
          "Resource": "*"
        }
] }

次に、「アクセス権限が正しく設定されていない」に対しては、ターミナル上で aws configure と入力し、作成したIAMの設定を入力しました。
作成後、aws configure list と入力し、IAMの情報が記載されていることを確認しました。

結果、無事ログインに成功し、ECRへのイメージのPushもすることができました🎉

参考

Amazon ECR エラーメッセージのトラブルシューティング

8
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
2