はじめに
AWS ECS のタスクに対して enableExecuteCommand を有効にすることで、コンテナに直接アクセスできるようになります。
本記事では、手動でこの設定を変更し、適用する手順を説明します。設定の確認から適用までの流れを簡潔にまとめました。
書こうと思ったきっかけ
ECSのコンテナにアクセスしようとした際に enableExecuteCommand が無効であることに気付きました。
この設定を変更する手順を明確にしておけば、今後同じ問題に直面した際にスムーズに対応できると考えました。
また、他の開発者にも参考になるように記録を残すことにしました。
1. 現在の enableExecuteCommand 設定を確認
まず、現在のタスクの enableExecuteCommand の設定を確認します。
aws ecs describe-tasks --cluster my-ecs-cluster --tasks $(aws ecs list-tasks --cluster my-ecs-cluster --query "taskArns[0]" --output text) --query "tasks[].enableExecuteCommand"
2. サービスで enableExecuteCommand を有効化
次に、対象の ECS サービスで enableExecuteCommand を有効化します。
aws ecs update-service --cluster my-ecs-cluster --service my-ecs-service --enable-execute-command
3. 設定が反映されたかを確認
有効化後、設定が反映されたかどうかを再度確認します。
aws ecs describe-tasks --cluster my-ecs-cluster --tasks $(aws ecs list-tasks --cluster my-ecs-cluster --query "taskArns[0]" --output text) --query "tasks[].enableExecuteCommand"
4. 強制的に新しいデプロイを実行
もし変更が即時に反映されない場合、新しいデプロイを実行して反映させます。
aws ecs update-service --cluster my-ecs-cluster --service my-ecs-service --force-new-deployment
5. 最終確認
最後に、もう一度 enableExecuteCommand の設定が有効になったかを確認します。
aws ecs describe-tasks --cluster my-ecs-cluster --tasks $(aws ecs list-tasks --cluster my-ecs-cluster --query "taskArns[0]" --output text) --query "tasks[].enableExecuteCommand"
6. コンテナへログインする方法
設定が有効になったら、以下のコマンドを実行してコンテナにログインできます。
aws ecs execute-command --cluster my-ecs-cluster --task $(aws ecs list-tasks --cluster my-ecs-cluster --query "taskArns[0]" --output text) --container <コンテナ名> --command "/bin/sh" --interactive
<コンテナ名> には対象のコンテナ名を指定してください。これにより、コンテナ内部でシェルを操作できます。
まとめ
今回の手順で、ECS タスクの enableExecuteCommand を有効化し、設定が反映されることを確認しました。
特に force-new-deployment を実行することで、確実に設定が適用されることを理解しました。
さらに、execute-command を使用することで、直接コンテナにログインできるようになりました。
この方法を覚えておけば、今後も迅速にコンテナへアクセスできるようになります。