0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ecs execute-command とセッションマネージャープラグインを使い、CLIでAWS ECSのコンテナにログインする

Last updated at Posted at 2025-02-04

自分はmacOS 13.5.1(Ventura)のターミナルから接続していますが、AWS CLIの ecs execute-command を使用して、ECS (Fargate) で起動中のコンテナにログインする方法です。なお、AWS CLI は既に導入されている前提で書いています。

以下の内容は、各自の情報に置き換えてください。

{cluster_name} ... クラスター名
{task_id} ... タスクID
{container_name} ... コンテナ名

コマンド入力
aws ecs execute-command --cluster {cluster_name} --task {task_id} --container {container_name} --interactive --command "/bin/bash"

接続できました!

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

Starting session with SessionId: ecs-execute-command-xxxxxxxxxxxxxxxxxxxxxxxxxx
bash-4.3# 

接続できない場合

先ほどのコマンドで接続できず、以下のような表示になる場合は、セッションマネージャーのプラグインを先にインストールすることが必要です。

terminal
SessionManagerPlugin is not found. Please refer to SessionManager Documentation here: http://docs.aws.amazon.com/console/systems-manager/session-manager-plugin-not-found

エラー表示に書かれているのはセッションマネージャープラグインが見つからない場合の対処方法(AWS公式)です。ここから各自の状況に合わせた対応をしていきます。
AWS docs - Session Manager プラグインが見つからない

自分はリンク先の、リンク先の、さらにリンク先にあるこちらを参考にしました。
AWS docs - AWS CLI 用の Session Manager プラグインをインストールする
 → macOS での Session Manager プラグインのインストール

基本的には上記ページに書かれた通り、コマンドを以下の順に実行しただけです。

コマンド入力
## 1. バンドルされたインストーラをダウンロードします。
 
curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/sessionmanager-bundle.zip" -o "sessionmanager-bundle.zip"

## 2. パッケージを解凍します。

unzip sessionmanager-bundle.zip

## 3. インストールコマンドを実行します。

sudo ./sessionmanager-bundle/install -i /usr/local/sessionmanagerplugin -b /usr/local/bin/session-manager-plugin

## 4. インストールが成功したことを確認します。

session-manager-plugin
terminal
##(インストールが成功すると、次のメッセージが返されます。)

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

Session Manager プラグインのインストールを検証する

ECSのコンテナにCLIでログインする際に使用する、セッションマネージャープラグインが導入されているかどうかを session-manager-plugin のコマンドで確認しましたが、自分の環境で厳密には以下の表示になりました。(is → was でちょっとだけ違う)

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

ログインできない場合

クラスター名が間違っているなどでコンテナにアクセスできないときは、以下のようなエラーが出ます。入力した情報に誤りがないか確認してみてください。

terminal
An error occurred (ClusterNotFoundException) when calling the ExecuteCommand operation: Cluster not found.

クラスター名、タスクID、コンテナ名などの確認方法は、現時点ではいったん割愛します。

参考URL

ECS のタスク ID もしくは TaskARN を取得する方法を教えてください
ECSにSSMでアクセスするの際のコマンドを少し便利にするスクリプト
AWS ECS Execに必要なTask IDをコマンドラインから取得
【ECS】 【Fargate】TASK実行container内でTask ID を取得するスクリプト
ECSとECRの環境構築

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?