ECS の Windows コンテナで ECS Exec がすぐ終了するときの対処方法

2022.10.19

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

困っていた内容

ECS で稼働している Windows コンテナのデバックのため、AWS公式ドキュメントを参考に ECS Exec を有効化しました。
ECS Exec のコマンド(execute-command)を実行したところ、セッションがすぐに終了してしまいます。対処方法を教えてください。

実行内容

$ aws ecs execute-command --cluster cluster-name \
    --task task-id \
    --container container-name \
    --interactive \
    --command "/bin/sh"

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

Starting session with SessionId: ecs-execute-command-0123456789abcdef
/bin/sh : The term '/bin/sh' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the 
spelling of the name, or if a path was included, verify that the path is correct and try again.
At line:1 char:1
+ /bin/sh
Exiting session with sessionId: ecs-execute-command-0123456789abcdef.

どう対応すればいいの?

--commandにコンテナ上で実行可能なコマンドを指定してください。

--commandで指定したコマンドが実行されるのは、接続先の Windows コンテナとなります。そのため、接続先にないコマンドを指定した場合はエラーが表示され、ECS Exec が終了します。
Windows コンテナの場合は、PowerShell がインストールされている場合が多いため、例えばpowershell.exeなど想定されます。

コマンド例

$ aws ecs execute-command --cluster cluster-name \
    --task task-id \
    --container container-name \
    --interactive \
    --command powershell.exe

参考資料

留意点

…(中略)…

コンテナ上に存在するコマンド、スクリプト、Shell のみを使用することができます。