エラーの状況
AWS CI/CD for Amazon ECSハンズオンを試していたときのこと
https://pages.awscloud.com/rs/112-TZM-766/images/AWS_CICD_ECS_Handson.pdf
CodePipelineの「Release Change」をポチっとしたところ、Action=BuildでFailedになってしまいました。
エラーを見ると、
[Container] 2021/07/29 05:15:19 Entering phase PRE_BUILD
[Container] 2021/07/29 05:15:19 Running command $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument operation: Invalid choice, valid choices are:
batch-check-layer-availability | batch-delete-image
batch-get-image | complete-layer-upload
create-repository | delete-lifecycle-policy
delete-registry-policy | delete-repository
delete-repository-policy | describe-image-scan-findings
describe-images | describe-registry
describe-repositories | get-authorization-token
get-download-url-for-layer | get-lifecycle-policy
get-lifecycle-policy-preview | get-registry-policy
get-repository-policy | initiate-layer-upload
list-images | list-tags-for-resource
put-image | put-image-scanning-configuration
put-image-tag-mutability | put-lifecycle-policy
put-registry-policy | put-replication-configuration
set-repository-policy | start-image-scan
start-lifecycle-policy-preview | tag-resource
untag-resource | upload-layer-part
get-login-password | wait
help
[Container] 2021/07/29 05:15:24 Command did not exit successfully $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email) exit status 252
[Container] 2021/07/29 05:15:24 Phase complete: PRE_BUILD State: FAILED
[Container] 2021/07/29 05:15:24 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email). Reason: exit status 252
Code language: plaintext (plaintext)
11行目、引数が間違っている。
30行目、get-login ではなく、get-login-password を使うようにという意味?
32行目、終了コードは252
buildspec.ymlの、aws ecr get-loingの行で失敗していました。
version: 0.2
phases:
pre_build:
commands:
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
- REPOSITORY_URI=${Your account ID}.dkr.ecr.us-east-1.amazonaws.com/php-sample
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
Code language: YAML (yaml)
Cloud 9のターミナルで実行してみると、awsコマンドのバージョンは、1.19.112でした。
$ aws --version
aws-cli/1.19.112 Python/2.7.18 Linux/4.14.238-182.422.amzn2.x86_64 botocore/1.20.112
Code language: Bash (bash)
Cloud 9のターミナルで、aws ecr の行を実行してみると、
$ $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
Note: AWS CLI version 2, the latest major version of the AWS CLI, is now stable and recommended for general use. For more information, see the AWS CLI version 2 installation instructions at: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
aws help
aws <command> help
aws <command> <subcommand> help
aws: error: argument --region: expected one argument
Code language: Bash (bash)
エラーです。--regionオプションに引数がない、とのこと。
シェル変数AWS_DEFAUL_REGIONを表示してみると、
$ echo $AWS_DEFAULT_REGION
$
Code language: Bash (bash)
設定されていませんでした。
AWS_DEFAULT_REGIONに us-east-1 を設定して、aws ecrコマンドを実行してみると、
$ AWS_DEFAULT_REGION=us-east-1
$ $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
WARNING! Using --password via the CLI is insecure. Use --password-stdin.
WARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
Code language: Bash (bash)
Cloud 9のターミナルでは、動きました。
そこでbuildspec.ymlにも、シェル変数AWS_DEFAULT_REGIONを設定してみました。
version: 0.2
phases:
pre_build:
commands:
- AWS_DEFAULT_REGION=us-east-1
- $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
Code language: YAML (yaml)
ところが、CodePipelineではあいかわらず、Action=BuildでFailedとなり、ターミナルの実行結果を見るとexit 252です。
buildspec.ymlで、シェル変数ではなく、直接 us-east-1 を指定してみました。
version: 0.2
phases:
pre_build:
commands:
- $(aws ecr get-login --region us-east-1 --no-include-email)
Code language: YAML (yaml)
CodePipelineではあいかわらず、Action=BuildでFailedです。ターミナルの実行結果を見るとexit 252でした。
原因
「aws ecr get-login 252」で検索すると、
この記事によると、AWS CLI Version 2では、get-loginは削除されたとのこと。
そういえば、Cloud 9のターミナルの実行結果にも、そんなメッセージが表示されていましたね。
$ $(aws ecr get-login --region $AWS_DEFAULT_REGION --no-include-email)
Note: AWS CLI version 2, the latest major version of the AWS CLI, is now stable and recommended for general use. For more information, see the AWS CLI version 2 installation instructions at: https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html
Code language: Bash (bash)
ハンズオンのPDFは2020年4月なのに、もう古いんだね...
対策
Cloud 9のターミナルで、get-login-passwordを試したところ、Loginできました。
$ DOCKER_URI=https://<aws_account_id>.dkr.ecr.<region>.amazonaws.com
$ aws ecr get-login-password | docker login --username AWS --password-stdin $DOCKER_URI
ARNING! Your password will be stored unencrypted in /home/ec2-user/.docker/config.json.
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
Login Succeeded
Code language: plaintext (plaintext)
buildspec.ymlを修正して、
version: 0.2
phases:
pre_build:
commands:
- DOCKER_URI=https://<aws_account_id>.dkr.ecr.us-east-1.amazonaws.com
- aws ecr get-login-password | docker login --username AWS --password-stdin $DOCKER_URI
- REPOSITORY_URI=${Your account ID}.dkr.ecr.us-east-1.amazonaws.com/php-sample
- IMAGE_TAG=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
Code language: YAML (yaml)
CodePipelineで「Release Change」したところ、Source、Build、Deployまでグリーンになりました!