AWS ECSのタスク定義にECRイメージを設定してデプロイしたところ、次のエラーが発生しました。
CannotPullContainerError: inspect image has been retried 1 time(s): failed to resolve ref "123456789123.dkr.ecr.ap-northeast-1.amazonaws.com/dev/app:latest": pulling from host 123456789123.dkr.ecr.ap-northeast-1.amazonaws.com failed with status code [manifests latest]: 403 Forbidden
解決方法
公式ページにも色々と解決策が記載されていますが、私の場合はAWSのマネージメントコンソールの下記に移動して権限を付与する必要がありました。
Amazon ECS --> Amazon ECR --> リポジトリ --> <pullしようとしたimageのリポジトリ> --> 許可 --> ポリシーJSONの編集
付与する権限は次のようになります。
< xxx >
の箇所(5行目と8行目)は自身の環境にあったものに書き換えます。
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "<任意のSID>",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::< imageをpullしてきたい環境のaccount-id >:root"
},
"Action": [
"ecr:BatchCheckLayerAvailability",
"ecr:BatchGetImage",
"ecr:GetDownloadUrlForLayer"
]
},
]
}
上記以外のパターン
こちらの記事にも書きましたが、ECSに十分なメモリが確保できていない場合にも同様のエラーが発生しました。
※ エラー文の末尾が403からnot foundに変わっています
Cannotpullcontainererror: pull image manifest has been retried 1 time(s): failed to resolve ref 123456789123.dkr.ecr.ap-northeast-1.amazonaws.com/test-api:latest: 123456789123.dkr.ecr.ap-northeast-1.amazonaws.com/test-api:latest: not found
分かりにくい...