Amazon ECS Exec(aws ecs execute-command)を便利にするツール「sssh」

ECS Execするときに、Cluster名や、コンテナ名、タスクIDを確認してaws ecs execute-commandコマンド実行をしてましたが、Cluster数、タスク数が多いと非常に面倒ですよね。 そんなあなたに、楽にしてくれるツールがありました。
2023.05.08

こんにちはコカコーラ大好き、カジです。

ECS Execするときに、Cluster名や、コンテナ名、タスクIDを確認してaws ecs execute-commandコマンド実行をしてましたが、Cluster数、タスク数が多いと非常に面倒ですよね。

そんなあなたに、楽にしてくれるツールがありました。

ssshとは

ECS Execを実行したいECS Clusterやタスクを対話式で選択して、実行できるツールとなります。 今回はVersion 2を試しました。

作者は、弊社事例に掲載されている株式会社集英社 プラットフォーム室 安達様で、コンテナ移行時に作成され、ツールをご紹介頂き、私が試したところ非常に良いツールだったのでご紹介となります。(安達様にご承諾頂き、ブログでのご紹介となりました)

前提条件

Mac OS Xでの利用法を記載します。他のOSでもAWS CLI、jq、Bashなどが実行できれば可能と推測しています。

操作端末

  • Mac OS X 12.6 Monterey
  • AWS CLIをインストール済み
  • jq をインストール済み

AWS環境

  • ECS-FargateのECSクラスターを2つ用意
  • それぞれ1タスク起動している環境でテストしました。

事前準備

依存関係でpecoのインストールが必要なためbrewでインストールします。

brew install peco

また、AWS CLI 用の Session Manager プラグインのインストールが必要です。

バンドルされたインストーラをダウンロード
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

詳細はAWS CLI 用の Session Manager プラグインをインストールする - AWS Systems Manager

ssshの使い方

ssshをダウンロードします。
curl "https://gist.githubusercontent.com/yuki777/e6feba842934e3100ecd45370969a9a9/raw/sssh?clearCache=`date +%Y%m%d%H%M%S`" -o sssh

実行権限を付与します。
chmod u+x sssh

起動します
./sssh

ssshを起動すると、AWS CLIで利用しているAWSプロファイルの一覧が表示されて、接続したいECSのあるプロファイルを選択します。 (スイッチロールまたは、Assume Roleの環境でもプロファイルをきちんと設定していれば利用可能です。)

AWS Profile:            IgnoreCase [4 (1/1)]
default
xxxxxx
yyyyyy
zzzzzz

MFA設定があると場合は、MFA入力になります。

次に、そのアカウントにあるECS Clusterの選択に移ります。(感動です。)

Cluster:                 IgnoreCase [2 (1/1)]
sample-fargate1-cluster
sample-fargate2-cluster

ECSタスクが複数あると選択状態になりますが、私は1つのみのためそのままログインとなりました。psコマンドでnginxのプロセスを確認してみました。

Select service.
Service: arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:service/sample-fargate1-cluster/sample-fargate1-service

Select task.
Task: arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:task/sample-fargate1-cluster/7375cd854d444a75af2b715e821de3df

Select container.
Container: sample-fargate1-container

aws ecs execute-command --profile default --cluster sample-fargate1-cluster --container sample-fargate1-container --task arn:aws:ecs:ap-northeast-1:xxxxxxxxxxxx:task/sample-fargate1-cluster/7375cd854d444a75af2b715e821de3df --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-0f9dd875b261c8c0c
/ # ps -ef | grep nginx
    1 root      0:00 nginx: master process nginx -g daemon off;
    7 nginx     0:00 nginx: worker process
  135 root      0:00 grep nginx
/ #
/ # exit


Exiting session with sessionId: ecs-execute-command-0f9dd875b261c8c0c

ECS Execができない場合のトラブルシュート

起こりうるエラーとしては以下となります。その場合は、以下を確認ください。

  • 対象のAWS環境にAWS CLIで実行できる権限があること
  • 対象のECS Task Roleがあること
  • ECS Execの動作に必要なIAM Roleが割り当てられていること

詳細は以下のブログを参照ください。

[小ネタ]ECS ExecのenableExecuteCommandを有効にしようとするとエラーになってしまう

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


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.

また、 GitHub - aws-containers/amazon-ecs-exec-checker: ? Pre-flight checks for ECS Execでテストすると、AWS環境側の問題も特定できます。

参照元