最初に
ECSの実装をしていると、「ECSタスクの起動に失敗しました!」という趣旨の、いわゆるDockerイメージのPullに失敗するエラーがありますよね。エラー自体はありふれているのですが、厄介なのは「何が悪いのかまったく教えてくれないこと」です!
問題範囲が広範囲に及ぶため、筆者もこのエラーには何度も苦しめられてきました。
だったのですが、半年ほど前でしょうか。
なんとAWS公式からこのエラーを自動で診断してくれるツールが登場しています!!
今回はそのツールについて紹介していきます。
ツールについて
AWSSupport-TroubleshootECSTaskFailedToStart
という名前です。
ドキュメントはこちらにあり、これ読めばだいたい解決しますが、本記事でも要約、解説、実践をしていきます。
要約
- 以下の原因を検証し、切り分けることができる
- コンテナレジストリへのネットワーク接続
- タスク実行ロールの IAM権限
- VPCエンドポイント接続
- セキュリティグループ
- AWS Secrets Manager
- ログ記録の構成
- ツールが診断するLambdaおよびIAM権限を自動で作成する
- 診断完了後にこのリソースは削除される
- つまり、LambdaおやIamなどの作成/削除権限が必要になる
- 機能としては、
Systems Manager Automation
の一部
実際に使ってみる
ドキュメントページからも遷移できますが、診断ページはこちらにあります。
次に調査対象のタスクIDと権限の設定を行います。
-
AutomationAssumeRole
- 調査を実行するRoleを指定します。私の場合はすでに管理者権限でAWSアカウントにログインしていたので、特に設定する必要はありませんでした。もし実行ユーザーに必要な権限がない場合は、別途IAMRoleを作成する必要がありそうです
-
TaskId
-
ClusterName
- タスクを実行したクラスタ名を記述します
-
CloudwatchRetentionPeriod
- ログの保存期間のようです
これだけの設定でOKです!!
実行結果
画像の感じで結果が出力されます。
スクショはまだ実行中ですが、重要なのは5,7番なので結果を確認していきます。(1~4, 8~10 は検証準備と後片付け処理のようです)
5 CoreFailureReasonEvaluation
や7 ExecutionResults
を見てみるとエラー原因が書いてありました、
以下、結果詳細を載せておきます。
IDやIPはマスクしております。
5 CoreFailureReasonEvaluation
REGISTRY CHECKS =============== The registry domain [.dkr.ecr.ap-northeast-1.amazonaws.com] encountered socket connection failure for DNS Resolved IP(s) [''] with same network settings used by ECS / Fargate Agent. For more information check the following Knowledge Center articles: - https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-api-error-ecr/ - https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-error/ You have enabled ECR related private endpoints ['s3'], However missing endpoint/s {'ecr.api', 'ecr.dkr'} See https://docs.aws.amazon.com/AmazonECR/latest/userguide/vpc-endpoints.html The route table rtb-************ associated with Task Subnet subnet-*********** does not have network access either via Internet Gateway or Nat Gateway. If you are leveraging custom networking path via peered network or via Transit Gateway please ensure the subnet where the task is placed have network connectivity to pull all required resources The route table rtb-************* associated with Task Subnet subnet-************* does not have network access either via Internet Gateway or Nat Gateway. If you are leveraging custom networking path via peered network or via Transit Gateway please ensure the subnet where the task is placed have network connectivity to pull all required resources Log Configuration Checks ======================== The registry domain [logs.ap-northeast-1.amazonaws.com] encountered socket connection failure for DNS Resolved IP(s) ['*********'] with same network settings used by ECS / Fargate Agent.
7 ExecutionResults
REGISTRY CHECKS
The registry domain [.dkr.ecr.ap-northeast-1.amazonaws.com] encountered socket connection failure for DNS Resolved IP(s) [''] with same network settings used by ECS / Fargate Agent. For more information check the following Knowledge Center articles:
- https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-api-error-ecr/
- https://aws.amazon.com/premiumsupport/knowledge-center/ecs-pull-container-error/
You have enabled ECR related private endpoints ['s3'], However missing endpoint/s {'ecr.api', 'ecr.dkr'}
See https://docs.aws.amazon.com/AmazonECR/latest/userguide/vpc-endpoints.html
The route table rtb-*********** associated with Task Subnet subnet-*********** does not have network access either via Internet Gateway or Nat Gateway.
If you are leveraging custom networking path via peered network or via Transit Gateway please ensure the subnet where the task is placed have network connectivity to pull all required resources
The route table rtb-*********** associated with Task Subnet subnet-*********** does not have network access either via Internet Gateway or Nat Gateway.
If you are leveraging custom networking path via peered network or via Transit Gateway please ensure the subnet where the task is placed have network connectivity to pull all required resources
結論
今回は、The route table rtb-*********** associated with Task Subnet subnet-*********** does not have network access either via Internet Gateway or Nat Gateway.
ということで、IGW
もNATGW
もないぞということを言われている訳ですね。
よくよく見てみたら、NatGWのないPrivateサブネットを指定してタスクを実行していたので、外部に通信できなかったというわけでしょう。
ネットワークをPublicサブネットに切り替えたところ、タスクが完了しました。
終わりに
今までは勘と経験と総当たりでエラーに対処していたのが、診断ツールを走らせて放置するだけでよくなったのはとてもよさそうですね。今後はストレスフリーにECS開発ができそうで、心が晴れやかです。
エラー原因が英語でよくわからなくても、GPTに投げれば正確に答えてくれそうなので、そこも含めてとてもよいツールだと思います。皆さんもぜひ触ってみてください!