4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

「ECSタスクが起動できません」を解決するAWS公式診断ツール!

Posted at

最初に

ECSの実装をしていると、「ECSタスクの起動に失敗しました!」という趣旨の、いわゆるDockerイメージのPullに失敗するエラーがありますよね。エラー自体はありふれているのですが、厄介なのは「何が悪いのかまったく教えてくれないこと」です!
問題範囲が広範囲に及ぶため、筆者もこのエラーには何度も苦しめられてきました。

image.png

だったのですが、半年ほど前でしょうか。
なんとAWS公式からこのエラーを自動で診断してくれるツールが登場しています!!

今回はそのツールについて紹介していきます。

ツールについて

AWSSupport-TroubleshootECSTaskFailedToStartという名前です。
ドキュメントはこちらにあり、これ読めばだいたい解決しますが、本記事でも要約、解説、実践をしていきます。

要約

  • 以下の原因を検証し、切り分けることができる
    • コンテナレジストリへのネットワーク接続
    • タスク実行ロールの IAM権限
    • VPCエンドポイント接続
    • セキュリティグループ
    • AWS Secrets Manager
    • ログ記録の構成
  • ツールが診断するLambdaおよびIAM権限を自動で作成する
    • 診断完了後にこのリソースは削除される
    • つまり、LambdaおやIamなどの作成/削除権限が必要になる
  • 機能としては、Systems Manager Automationの一部

実際に使ってみる

ドキュメントページからも遷移できますが、診断ページはこちらにあります。

ひとまず「Single Execution」を選択
image.png

次に調査対象のタスクIDと権限の設定を行います。

image.png

  • AutomationAssumeRole

    • 調査を実行するRoleを指定します。私の場合はすでに管理者権限でAWSアカウントにログインしていたので、特に設定する必要はありませんでした。もし実行ユーザーに必要な権限がない場合は、別途IAMRoleを作成する必要がありそうです
  • TaskId

    • 失敗したタスクページにある番号がタスクIDです
      image.png
  • ClusterName

    • タスクを実行したクラスタ名を記述します
  • CloudwatchRetentionPeriod

    • ログの保存期間のようです

これだけの設定でOKです!!

実行結果

画像の感じで結果が出力されます。
スクショはまだ実行中ですが、重要なのは5,7番なので結果を確認していきます。(1~4, 8~10 は検証準備と後片付け処理のようです)
image.png

5 CoreFailureReasonEvaluation7 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:

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.ということで、IGWNATGWもないぞということを言われている訳ですね。
よくよく見てみたら、NatGWのないPrivateサブネットを指定してタスクを実行していたので、外部に通信できなかったというわけでしょう。
ネットワークをPublicサブネットに切り替えたところ、タスクが完了しました。

終わりに

今までは勘と経験と総当たりでエラーに対処していたのが、診断ツールを走らせて放置するだけでよくなったのはとてもよさそうですね。今後はストレスフリーにECS開発ができそうで、心が晴れやかです。
エラー原因が英語でよくわからなくても、GPTに投げれば正確に答えてくれそうなので、そこも含めてとてもよいツールだと思います。皆さんもぜひ触ってみてください!

4
1
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
4
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?