Memo

メモ > サーバ > サービス: AWS > Amazon ECS Exec

■Amazon ECS Exec
「Windows+WSL2+AWS CLI+Session Manager」で、ローカルPCから直接コマンドラインで操作できるようになるみたい Fargateなど外部から直接SSHアクセスできないものにも、アクセスできるようになるみたい [アップデート] 実行中のコンテナに乗り込んでコマンドを実行できる「ECS Exec」が公開されました | DevelopersIO https://dev.classmethod.jp/articles/ecs-exec/ Amazon ECS Execを使ってみる - サーバーワークスエンジニアブログ https://blog.serverworks.co.jp/ecs-exec ECS Execを使って Fargateのコンテナにアクセスする - Qiita https://qiita.com/okubot55/items/b1fb07b2de08c354275b Session ManagerプラグインをインストールしてローカルWindows PCからEC2へSession Managerで接続してみた | DevelopersIO https://dev.classmethod.jp/articles/installed-session-manager-plugin-and-connected-from-local-window... [ECS/Fargate]SSHログインせずにECS Execでログインする - ADACHIN SERVER LABO https://blog.adachin.me/archives/47413 Amazon ECS Exec(aws ecs execute-command)を便利にするツール「sssh」 | DevelopersIO https://dev.classmethod.jp/articles/sssh-ecs-exec-tool/ ■接続ユーザ用IAMの作成 ※要検証 接続する側のIAMには、最低でも以下の権限が必要 ecs:ExecuteCommand ssm:StartSession ecs:DescribeTasks Amazon ECS Execを使ってみる - サーバーワークスエンジニアブログ https://blog.serverworks.co.jp/ecs-exec ■ツールのインストール AWS CLIが使える環境に、追加で「session-manager-plugin」をインストールする必要がある 以下はWSL2にインストールを試したときの内容 【Windows WSL2】UbuntuにAWS CLIとSession Managerをインストール https://zenn.dev/yukiko_bass/articles/6b2c47357e83e1
$ aws --version aws-cli/2.2.44 Python/3.8.8 Linux/5.10.16.3-microsoft-standard-WSL2 exe/x86_64.ubuntu.20 prompt/off $ session-manager-plugin session-manager-plugin: command not found $ curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/ubuntu_64bit/session-manager-plugin.deb" -o "session-manager-plugin.deb" % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3292k 100 3292k 0 0 1119k 0 0:00:02 0:00:02 --:--:-- 1119k $ sudo dpkg -i session-manager-plugin.deb [sudo] password for refirio: Selecting previously unselected package session-manager-plugin. (Reading database ... 32566 files and directories currently installed.) Preparing to unpack session-manager-plugin.deb ... Preparing for install Unpacking session-manager-plugin (1.2.463.0-1) ... Setting up session-manager-plugin (1.2.463.0-1) ... Creating symbolic link for session-manager-plugin $ session-manager-plugin The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.
インストールには成功した ■ECS Execでの接続の流れ ECS Execを使って Fargateのコンテナにアクセスする - Qiita https://qiita.com/okubot55/items/b1fb07b2de08c354275b [アップデート] 実行中のコンテナに乗り込んでコマンドを実行できる「ECS Exec」が公開されました | DevelopersIO https://dev.classmethod.jp/articles/ecs-exec/ Amazon ECS Execを使ってみる - サーバーワークスエンジニアブログ https://blog.serverworks.co.jp/ecs-exec 構築したECSでは、各ロールは以下のようになっている タスクロール: (未選択のまま) タスク実行ロール: ecsTaskExecutionRole よって「ecsTaskRole」というロールを作り、それをタスクロールに割り当てるのが良さそう 具体的には、 ポリシー「ECSTaskRolePolicy」を作成し、JSONで内容を定義する ロール「ecsTaskRole」を作成し、上で作成したポリシーを割り当てる タスク定義で「タスクロール」に、上で作成したロールを割り当てる サービスを更新(デプロイ)する という流れで作業する 以降は具体的な内容 ■ポリシーの作成 セキュリティ認証情報 → ポリシー → ポリシーの作成 「JSON」タブに以下を入力する
{ "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [ "ssmmessages:CreateControlChannel", "ssmmessages:CreateDataChannel", "ssmmessages:OpenControlChannel", "ssmmessages:OpenDataChannel" ], "Resource": "*" } ] }
「次のステップ:タグ」をクリック そのまま「次のステップ:確認」をクリック ポリシーの作成画面になるので以下を入力 名前: ECSTaskRolePolicy 説明: Policy for ECS Task 「ポリシーの作成」をクリック ■タスクロールの作成 IAM → アクセス管理 → ロール → ロールを作成 信頼されたエンティティを選択 信頼されたエンティティタイプ: AWSのサービス AWSアカウント 他のAWSのサービスのユースケース: Elastic Container Service → Elastic Container Service Task を選択 「次へ」ボタンをクリック 許可を追加 上の手順で作成した「ECSTaskRolePolicy」を検索してチェックを入れる 「次へ」ボタンをクリック 名前、確認、および作成 ロール名: ecsTaskRole 「ロールを作成」ボタンをクリック ■タスク定義の更新 Elastic Container Service → Amazon ECS → タスク定義 「Container-Test-Task」にチェックを入れて「新しいリビジョンの作成」をクリック (以下、前回タスクを作成した時の内容をベースに入力する) 「タスクロール」が空欄になっているはずなので、上の手順で作成した「ecsTaskRole」を選択 これでタスクを作成する ■サービスの更新(デプロイ) Elastic Container Service → Amazon ECS → クラスター → Container-Test-Cluster → サービス → Container-Test-Service → サービスを更新 デプロイ設定で以下2箇所を調整する 「新しいデプロイの強制」にチェックを入れる 「リビジョン」を最新のものに変更する 「更新」をクリック ■ECSの情報を確認 awscli ECS 関連コマンドメモ - Qiita https://qiita.com/notakaos/items/eda64c3c38b17f181698 ECSクラスターの一覧を表示
$ aws ecs list-clusters
ECSクラスター「Container-Test-Cluster」の詳細を表示
$ aws ecs describe-clusters --cluster Container-Test-Cluster
ECSサービスの一覧を表示
$ aws ecs list-services --cluster Container-Test-Cluster
ECSサービス「Container-Test-Service」の詳細を表示
$ aws ecs describe-services --cluster Container-Test-Cluster --services Container-Test-Service
ECSタスクの一覧を表示
$ aws ecs list-tasks --cluster Container-Test-Cluster --service-name Container-Test-Service
ECSタスクの詳細を表示
$ aws ecs describe-tasks --cluster Container-Test-Cluster --tasks 581cb32055144824ad8e314ea3e9a0d5
■ECS Execを有効化 サービスの「enableExecuteCommand」の設定を確認
$ aws ecs describe-services --cluster Container-Test-Cluster --services Container-Test-Service | grep enableExecuteCommand
サービスの「enableExecuteCommand」の設定を有効化
$ aws ecs update-service --cluster Container-Test-Cluster --service Container-Test-Service --enable-execute-command
サービスの「enableExecuteCommand」の設定を無効化する場合
$ aws ecs update-service --cluster Container-Test-Cluster --service Container-Test-Service --disable-execute-command
■サービスの更新(デプロイ) 上記の設定は、タスクに対してではなくサービスに対して行われている つまりECS Execは「enableExecuteCommand」を有効にした後に起動したタスクでのみ利用可能なので、再度「サービスの更新(デプロイ)」を行う必要がある リビジョンはそのままに、「新しいデプロイの強制」にチェックを入れて「更新」をクリックする ■接続 ECSタスクの一覧を表示
$ aws ecs list-tasks --cluster Container-Test-Cluster --service-name Container-Test-Service
タスクを指定して命令を実行
$ aws ecs execute-command --cluster Container-Test-Cluster --task 581cb32055144824ad8e314ea3e9a0d5 --container ecr_php81_apache --interactive --command "ps aux" $ aws ecs execute-command --cluster Container-Test-Cluster --task 59ede1ca54a743dfa9952370a7a2e073 --container ecr_php81_apache --interactive --command "ps aux"
タスクを指定して命令を実行(実際の実行結果例)
$ aws ecs execute-command --cluster Container-Test-Cluster --task 581cb32055144824ad8e314ea3e9a0d5 --container ecr_php81_apache --interactive --command "ps aux" The Session Manager plugin was installed successfully. Use the AWS CLI to start a session. Starting session with SessionId: ecs-execute-command-0d8440147a6931247 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0.7 86192 29400 ? Ss 09:53 0:00 apache2 -DFOREG root 17 0.0 0.3 1322756 15032 ? Ssl 09:53 0:00 /managed-agents www-data 22 0.0 0.3 86296 13692 ? S 09:53 0:00 apache2 -DFOREG www-data 23 0.0 0.3 86296 13692 ? S 09:53 0:00 apache2 -DFOREG www-data 24 0.0 0.3 86296 13692 ? S 09:53 0:00 apache2 -DFOREG www-data 25 0.0 0.3 86296 13704 ? S 09:53 0:00 apache2 -DFOREG www-data 26 0.0 0.3 86296 13700 ? S 09:53 0:00 apache2 -DFOREG root 35 0.0 0.6 1408580 25216 ? Sl 09:53 0:00 /managed-agents www-data 48 0.0 0.3 86296 13692 ? S 09:57 0:00 apache2 -DFOREG root 143 0.0 0.5 1250848 21428 ? Sl 10:11 0:00 /managed-agents root 152 0.0 0.0 8592 3248 pts/0 Rs+ 10:11 0:00 ps aux Exiting session with sessionId: ecs-execute-command-0d8440147a6931247.
タスクを指定してコンテナに接続
$ aws ecs execute-command --cluster Container-Test-Cluster --task 581cb32055144824ad8e314ea3e9a0d5 --container ecr_php81_apache --interactive --command "/bin/bash" # pwd /var/www/html # ls -l total 20 -rw-rw-r-- 1 root root 266 May 17 03:05 env.php -rw-rw-r-- 1 root root 237 May 17 03:05 index.php -rw-rw-r-- 1 root root 394 May 17 03:05 mysql.php -rw-rw-r-- 1 root root 391 May 17 03:05 mysql2.php -rw-rw-r-- 1 root root 19 May 17 03:05 phpinfo.php # exit Exiting session with sessionId: ecs-execute-command-04be407417812e929.
■viの利用 以下のとおり、Dockerfileで「vim」をインストールする
RUN apt-get update RUN apt-get install -y \ ↓ RUN apt-get update RUN apt-get install -y \ vim \
■llの利用 以下のとおり、Dockerfileでエイリアスを設定する
RUN echo 'alias ll="ls -al"' >> ~/.bashrc
■RDSへの接続 ※mysqlコマンドを使えるようになることは検証できたが、実際にRDSへ接続するところまでは検証できていない ECS Execからphpコンテナ経由でRDSに接続しようとしたが、mysqlの命令は使えなかった これは「default-mysql-client」をインストールすることで対応できる 調査のために常にインストールしておくといいか。もしくは余計なものは省いて、Cloud9など他の方法で接続するべきか Docker WordPress コンテナから MySQL コマンドが使いたい - かもメモ https://chaika.hatenablog.com/entry/2022/07/25/083000 PHPのコンテナ内からmysqlの命令は使えない
# mysql bash: mysql: command not found
以下のとおり、Dockerfileで「default-mysql-client」をインストールする
RUN apt-get update RUN apt-get install -y \ ↓ RUN apt-get update RUN apt-get install -y \ default-mysql-client \
PHPのコンテナ内からmysqlの命令が使えるようになった
# mysql --version mysql Ver 15.1 Distrib 10.5.19-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper
■トラブル1 以下のようなエラーが表示されたら、接続に失敗している サービスの「enableExecuteCommand」の設定を有効にしているか、 有効にした後でサービスの更新(デプロイ)を行っているかを確認する また、サービスの更新を行うとタスクのIDが変更されるので、古いIDで接続しようとしていないかも確認する
$ aws ecs execute-command --cluster Container-Test-Cluster --task 2095d9a520f74a6ea003ff117c261cd4 --container ecr_php81_apache --interactive --command "ps aux" 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.
■トラブル2 「ECS Execを有効化したECSサービスでAuto Scaling グループキャパシティープロバイダーを利用するとタスクが起動できなくなるようです。」とのこと 実際に遭遇したわけでは無いが、今後のためにメモ もし「オートスケーリングによって起動されたタスクに接続できない」ということだとしたら、スケーリングに難ありということで少々厄介のような ECS Execの有効化が原因でECSタスクがPROVISIONING状態から遷移しなくなった話 | DevelopersIO https://dev.classmethod.jp/articles/ecs-exec-cant-use-with-asg-capacity-provider/

Advertisement