1
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 Exec を利用してコンテナにログイン

Posted at

はじめに

ECS Fargate を利用している場合にコンテナに入りたいと思うことが多々あります。
そういう場合に ECS Exec を利用すると便利です。

事前準備

ECS Exec を利用するには session-manager-plugin をインストールする必要があります。
また、今回行う PC 環境は Mac で zsh を利用しています。

Session Manager プラグインのインストール

以下のコマンドで pkg をダウンロードしてインストールを行います。

download
% curl "https://s3.amazonaws.com/session-manager-downloads/plugin/latest/mac/session-manager-plugin.pkg" -o "session-manager-plugin.pkg"
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 3822k  100 3822k    0     0   550k      0  0:00:06  0:00:06 --:--:--  695k
install
% sudo installer -pkg session-manager-plugin.pkg -target /
Password:
installer: Package name is session-manager-plugin
installer: Installing at base path /
installer: The install was successful.

インストールが完了したら zsh の設定ファイルに追加し読み込みをします。

パスの追加
% echo 'export PATH=$PATH:/usr/local/sessionmanagerplugin/bin' >> ~/.zshrc
.zshrc
% source ~/.zshrc

以下のように successfully と表示されればインストール完了です。

確認
% session-manager-plugin                                                  

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

Execute Command の有効化

今度はタスク側の確認を行います。
ECS Exec コマンドを利用するにはタスク側で Execute Command が有効になっている必要があります。
確認方法は describe-tasks で出力される enableExecuteCommand の項目で行えます。

enableExecuteCommandの確認
% aws ecs describe-tasks --cluster <クラスター名> --tasks <タスクID> --query 'tasks[].enableExecuteCommand' --output text
True

True とあるので今回は有効になっていますが、もし False となっていた場合はサービスを更新する必要があります。
その際にオプションに --enable-execute-command を付けてアップデートしてください。

execute command の有効化
aws ecs update-service --cluster <クラスター名> --service <サービス名> --enable-execute-command

コンテナログイン

それではコンテナへログインを行いたいと思います。

ログイン情報の取得

コンテナにログインを行うためには以下の情報が必要になります。

  • クラスター名
  • タスクID
  • コンテナ名

まず初めに、稼働している クラスター名 を確認します。

クラスター名の確認
% aws ecs list-clusters
{
    "clusterArns": [
        "arn:aws:ecs:ap-northeast-1:012345678901:cluster/<クラスター名>"
    ]
}

次に クラスター名 から タスクID を取得します。

タスクIDの取得
% aws ecs list-tasks --cluster <クラスター名>
{
    "taskArns": [
        "arn:aws:ecs:ap-northeast-1:012345678901:task/<クラスター名>/<タスクID>"
    ]
}

取得したら今度は タスク で稼働してる コンテナ名 を確認します。

コンテナ名の確認
% aws ecs describe-tasks --cluster <クラスター名> --tasks <タスクID> --query 'tasks[].containers[].name' --output text
laravel nginx

ここでは laravelnginx の2つの名前で コンテナ が稼働しています。

ログイン

ということで情報が揃ったのでログインしてみます。

コンテナログイン
aws ecs execute-command \
  --cluster <クラスター名> \
  --task <タスクID> \
  --container <コンテナ名> \
  --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-mlclvkyb6iz7m2nfcwjk4gunxm
/ #

無事にログインができました。
また、コンテナから抜けるには exit コマンドを実行すれば OK です。

おわりに

コンテナにログインができることでちょっとした確認などができるのでとても便利なコマンドです。
ただ、セキュリティの観点からはできるだけログインはできない方が良いのかなと思います。
本番は無効にして開発環境のみ有効にするなどが良さそうです。

参考

1
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
1
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?