前準備
1. session-manager-puginのインストール
(オプション) AWS CLI 用の Session Manager プラグインをインストールする
2. タスクのIAMロールにSSMエージェント実行用ポリシーをアタッチ
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"ssmmessages:CreateControlChannel",
"ssmmessages:CreateDataChannel",
"ssmmessages:OpenControlChannel",
"ssmmessages:OpenDataChannel"
],
"Resource": "*"
}
]
}
3. 下記コマンドを実行
aws ecs update-service --region ap-northeast-1 --cluster ${ECS_CLUSTER_NAME} --service ${SERVICE_NAME} --enable-execute-command
4. タスクを再起動する(現在起動中のタスクにはecs execできない)
aws ecs update-service --force-new-deployment --cluster ${ECS_CLUSTER_NAME} --service ${SERVICE_NAME}
注意点
-
aws ecs update-service後、タスク再起動の必要あり。古いタスクはecs execできない - リードオンリーなコンテナにはそもそもアクセスできない
いざコンテナにアクセス
aws ecs execute-command --cluster ${ECS_CLUSTER_NAME} --task ${TASK_ID} --interactive --command "bin/sh"
うまくいかないとき
amazon-ecs-exec-checkerを使って原因調査する。うだうだ原因調査するよりこれが一番早い。
git clone https://github.com/aws-containers/amazon-ecs-exec-checker.git
cd amazon-ecs-exec-checker
./check-ecs-exec.sh <YOUR_ECS_CLUSTER_NAME> <YOUR_ECS_TASK_ID>
使用条件:
- jqがインストール済みであること
- aws cliがインストール済みであること