Skip to content

ecs execute-commandできない事象の調査

Published: at 18:06

3行で頼む

ecs execute-commandの失敗

必要な設定を終えたはずのECSタスクに対してexecute-commandを実行したところ、the execute command agent isn't runningというエラーで失敗した。

$ aws ecs execute-command --cluster sshd --task 7a2bf7bfeaf1446a9c9a72d58cde2ad7 --container sshd --interactive --command /bin/bash

An error occurred (InvalidParameterException) when calling the ExecuteCommand operation: The execute command failed because execute command was not enabled when the task was run or the execute command agent isn’t running. Wait and try again or run a new task with execute command enabled and try again.

ecs-exec-checker を対象のタスクに使ったところ、エラーメッセージの通りManaged Agent StatusSTOPPEDになっていることがわかった。Reason: nullになっていて理由がわからない…。

$ bash <( curl -Ls https://raw.githubusercontent.com/aws-containers/amazon-ecs-exec-checker/main/check-ecs-exec.sh ) sshd 7a2bf7bfeaf1446a9c9a72d58cde2ad7

-------------------------------------------------------------
Prerequisites for check-ecs-exec.sh v0.7
-------------------------------------------------------------
  jq      | OK (/usr/local/bin/jq)
  AWS CLI | OK (/usr/local/bin/aws)

-------------------------------------------------------------
Prerequisites for the AWS CLI to use ECS Exec
-------------------------------------------------------------
  AWS CLI Version        | OK (aws-cli/2.1.35 Python/3.8.8 Darwin/20.6.0 exe/x86_64 prompt/off)
  Session Manager Plugin | OK (1.2.279.0)

-------------------------------------------------------------
Checks on ECS task and other resources
-------------------------------------------------------------
Region : ap-northeast-1
Cluster: sshd
Task   : 7a2bf7bfeaf1446a9c9a72d58cde2ad7
-------------------------------------------------------------
  Cluster Configuration  | Audit Logging Not Configured
  Can I ExecuteCommand?  | arn:aws:iam::xxxxxxxxxxxxxx:user/admin
     ecs:ExecuteCommand: allowed
     ssm:StartSession denied?: allowed
  Task Status            | RUNNING
  Launch Type            | Fargate
  Platform Version       | 1.4.0
  Exec Enabled for Task  | OK
  Container-Level Checks |
    ----------
      Managed Agent Status
    ----------
         1. STOPPED (Reason: null) for "sshd" - LastStartedAt: null
    ----------
      Init Process Enabled (sshd:2)
    ----------
         1. Disabled - "sshd"
    ----------
      Read-Only Root Filesystem (sshd:2)
    ----------
         1. Disabled - "sshd"
  Task Role Permissions  | arn:aws:iam::xxxxxxxxxxxxxx:role/sshd-ecs-role
     ssmmessages:CreateControlChannel: allowed
     ssmmessages:CreateDataChannel: allowed
     ssmmessages:OpenControlChannel: allowed
     ssmmessages:OpenDataChannel: allowed
  VPC Endpoints          | SKIPPED (vpc-xxxxx - No additional VPC endpoints required)

タスクを再起動してみたけど状況が変わらないので、Agentが起動しない理由を調べるためにsshdを実行するイメージを用意して、タスクにssh接続できるようにした。

Agentのログは/var/log/amazon/ssmに出力されていて、errors.logに以下のようなエラーがあった。

2022-02-02 06:38:08 ERROR [run @ agent.go.104] error occurred when starting amazon-ssm-agent: failed to start message bus, failed to start health channel: failed to listen on the channel: ipc:///var/lib/amazon/ssm/ipc/health, address in use

healthcheck用のチャンネルが競合しているのだろうか…?

同じ状況の人を探した

エラー内容で検索したところ、以下のIssueが見つかった。

内容を読んでも原因はわからなかったが、Github Issueのコメントに「AWSサポートからssm start-sessionを使うworkaroundを回答してもらった」と書いてあるのを見つけた。

workaroundを使ってみる

以下のコマンドでexecute-command相当のことができることが確認できた。

aws ssm start-session --target ecs:sshd_7a2bf7bfeaf1446a9c9a72d58cde2ad7_b141f11734c247d893c536b23fae0879-2989211759 --document-name AWS-StartInteractiveCommand --parameters '{"command":["/bin/bash"]}'

clusterNametaskIdの他に、コンテナのruntimeIdを調べてtargetオプションに指定する必要がある。

とりあえずはこのコマンドで問題なさそうだが、AWS公式のドキュメントssm start-sessionを実行できないようにIAMロールで拒否することを推奨している。

issueの内容的にAWS側が修正されそうなので、ウォッチしておくことにする。

タスク定義のHEALTHCHECKやサービス検出について

自分の利用しているECSタスクの中でexecute-commandが(試した限りは)100%成功するものと、頻繁に失敗するものがあり、違いが気になって調べてみた。

すると失敗したタスクには

という違いがあったので、これらの設定の組み合わせで10回ずつ起動を繰り返してみた。

構成Agent起動成功/タスク起動回数1回目2回目3回目4回目5回目6回目7回目8回目9回目10回目
HEALTHCHECKなし10/10
HEALTHCHECKなし+サービズ検出あり10/10
HEALTHCHECKあり7/10×××
HEALTHCHECKあり+サービズ検出あり3/10×××××××

これだけで「HEALTHCHECKが関係している」とか「サービス検出が関係している」とは言い切れないが、「HEALTHCHECKなしでは100%成功している」ということも頭に入れとこうと思う。

おわりに

この事象のおかげで ecs-exec-checker という便利ツールを知れてよかった。