ECS on Fargate 構築時のエラーと対応方法

ECS on Fargate 構築時のエラー一覧と対応方法です。
約半年ぐらい前に構築した内容で自分への振り返りとして纏めました。
初めてECSを業務で利用したので振り返ると初歩的なエラーばかりです。

一部環境ですが、以下構成イメージです。(CI/CD含む)

1、AWSServiceRoleForECSが存在しないエラー

エラーメッセージ

CloudFormationでECS Cluster作成時にエラーが発生

Resource handler returned message: "Invalid request provided: CreateService error: Unable to assume the service linked role. Please verify that the ECS service linked role exists.
(Service: AmazonECS; Status Code: 400; Error Code: InvalidParameterException; Request ID: xxxxxxxxxx; Proxy: null)" (RequestToken: xxxxxxxxxx, HandlerErrorCode: InvalidRequest)
原因

IAMロール「AWSServiceRoleForECS」が存在しない

対応方法

以下のどちらかで対応
対応パターン① AWSマネジメントコンソールからECSを作成する
       ⇒「AWSServiceRoleForECS」が自動で生成
対応パターン② CloudFormationでAWSServiceRoleForECSを作成。

  IAMRoleAWSServiceRoleForECS:
    Type: "AWS::IAM::ServiceLinkedRole"
    Properties:
      AWSServiceName: "ecs.amazonaws.com"
      Description: "Role to enable Amazon ECS to manage your cluster."

複数の環境でECSを作成するため、私は②(CloudFormation)で対応しました。
異なるエラーコード、メッセージでも「AWSServiceRoleForECS」が存在しないために発生するエラーがあるため、このIAMロールの存在は確認したほうが良いです。

詳細については以下のサイトに説明されています。
IAMのService-Linked RolesがCloudFormationに対応したので、とてもナイスなリリースということを詳しく書いてみた。 | DevelopersIO

2、IAMポリシーの不足

エラーメッセージ

CodePipelineのCodeCommitでエラーが発生

The service role or action role doesn’t have the permissions required to access the Amazon S3 bucket named 検証環境S3バケット
Update the IAM role permissions, and then try again. Error: Amazon S3:AccessDenied:User: arn:aws:sts::111111111111:assumed-role/ロール名/222222222222 is not authorized to perform:
kms:GenerateDataKey on this resource because no identity-based policy allows the kms:GenerateDataKey action
(Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: xxxxx; S3 Extended Request ID: xxxxx; Proxy: null)
原因

dev環境(111111111111)のロールにKMSのポリシーが不足していて、検証環境のS3バケットを参照できない

対応方法

dev環境(111111111111)のロールにKMSのポリシー(kms:GenerateDataKey)を追加


3、KMSのエイリアスが利用できないエラー

エラーメッセージ

CodePipelineのCodeCommitでエラーが発生

The service role or action role doesn’t have the permissions required to access the Amazon S3 bucket named xxx. Update the IAM role permissions, and then try again.
Error: Amazon S3:AccessDenied:Access Denied (Service: Amazon S3; Status Code: 403; Error Code: AccessDenied; Request ID: xxxxx; S3 Extended Request ID: xxxxx; Proxy: null)
原因

KMSを作成していないアカウント(構成図ではdev環境)からだとKMSのエイリアスは利用できないためエラー。

対応方法

KMSのエイリアスを利用しないように変更しました。
今振り返るともっと良い対応方法があったはず。


4、ECR参照先が存在しない

エラーメッセージ

CodePipelineのCodeBuildでエラーが発生

COMMAND_EXECUTION_ERROR: Error while executing command: docker push $REPOSITORY_URI:latest. Reason: exit status 1
CLIENT_ERROR: no matching artifact paths found
原因

buildspec.ymlに設定したECR参照先が存在しない

対応方法

buildspec.ymlに正しいECRの参照先を設定


5、buildspec.yml内の変数の規定外の文字列(記号)を使用

エラーメッセージ

CodePipelineのCodeBuildでエラーが発生

s3-cicd-api: not a valid identifier
原因

buildspec.yml内の変数に「-」があったため

対応方法

buildspec.ymlの変数に「-」などの記号を取る
詳細は以下の記事にしました。 rikues2012.hatenablog.com

6、エンドポイントが未作成

エラーメッセージ

CodePipelineのCodeDeployでエラーが発生

Resourceinitializationerror: unable to pull secrets or registry auth: execution resource retrieval failed:
unable to retrieve ecr registry auth: service call has been retried 3 time(s): RequestError: send request failed caused by:
Post "https://api.ecr.ap-northeast-1.amazonaws.com/": dial tcp xx.xx.xx.xx:443: i/o timeout
原因

ECRのエンドポイントが未作成のため、イメージを取得できない

対応方法

ECRのエンドポイントを作成
・サービス名:com.amazonaws.ap-northeast-1.ecr.api



参考サイト

以下、FargateのCI/CDパイプラインのエラー原因ポイントが記載されています。

AWS Fargate with CI/CDパイプラインでデプロイ失敗する原因について | クラウド・AWSのIT技術者向けブログ SKYARCH BROADCASTING

以下、CodeBuildのエラー原因のポイントが記載されています。

AWS CodeBuildで失敗したときに確認するポイント #AWS - Qiita