LoginSignup
34
13

More than 5 years have passed since last update.

【備忘録】CodeBuildで`aws ecr get-login`コマンド実行時にエラーが発生する

Posted at

問題点

CodeBuildでaws ecr get-loginコマンド実行時にエラーが発生する

ビルド結果

codebuild03.png

buildspec.yml(抜粋)

buildspec.yml
version: 0.2

phases:
  pre_build:
    commands:
    - # 省略
    - $(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION})
    - # 省略

ビルドログ(抜粋)

[Container] 2018/08/23 06:59:56 Running command $(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION})

An error occurred (AccessDeniedException) when calling the GetAuthorizationToken operation: User: arn:aws:sts::xxx:assumed-role/code-build-xxx-xxx-build-service-role/AWSCodeBuild-xxx-xxx-xxx-xxx-xxx is not authorized to perform: ecr:GetAuthorizationToken on resource: *

[Container] 2018/08/23 06:59:57 Command did not exit successfully $(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION}) exit status 255
[Container] 2018/08/23 06:59:57 Phase complete: PRE_BUILD Success: false
[Container] 2018/08/23 06:59:57 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: $(aws ecr get-login --no-include-email --region ${AWS_DEFAULT_REGION}). Reason: exit status 255

原因

ビルドを実行するロールにECRの操作を許可するポリシーがアタッチされてない

解決方法

ビルドを実行するロールにECRを操作を許可するポリシーをアタッチする

1. ビルドで利用してるロールを確認する

CodeBuildのブロジェクト詳細でビルドに利用しているロール名を確認

codebuild01.png

2. IAMでロールにポリシーをアタッチする

IAMでロールにAmazonEC2ContainerRegistryPowerUser(ECRの操作を許可する)ポリシーをアタッチする。

codebuild02.png

3. ビルドが成功するようになったことを確認する

codebuild04.png

34
13
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
34
13