📝

ECSデバッグ方法

2023/02/18に公開

ecsのデバッグ方法としてecs exec を使う方法が有名なので紹介します。

手順

curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip"

unzip sessionmanager-bundle.zip

sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin

インストールされたかの確認
session-manager-plugin

下記のように表示されればOK

The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.

- ECSのタスクロールに権限追加

https://docs.aws.amazon.com/ja_jp/AmazonECS/latest/userguide/ecs-exec.html#ecs-exec-logging

ecsTaskExecutionRoleに下記のインラインポリシーをアタッチする

{
   "Version": "2012-10-17",
   "Statement": [
       {
       "Effect": "Allow",
       "Action": [
            "ssmmessages:CreateControlChannel",
            "ssmmessages:CreateDataChannel",
            "ssmmessages:OpenControlChannel",
            "ssmmessages:OpenDataChannel"
       ],
      "Resource": "*"
      }
   ]
}
  • サービスの設定を更新する
aws ecs update-service \
    --region ap-northeast-1 \
    --cluster クラスター名 \
    --service サービス名 \
    --enable-execute-command
  • サービスを起動し直す

コンソール画面から強制デプロイで行い
サービスの更新を行う

  • コンテナに入る
aws ecs execute-command \
    --region ap-northeast-1 \
    --cluster クラスター名 \
    --task タスクID \
    --container コンテナ名 \
    --interactive \
    --command "/bin/sh"

Discussion