LoginSignup
12
9

More than 3 years have passed since last update.

ECS Exec

Last updated at Posted at 2021-03-23

概要

実行中のコンテナに乗り込んでコマンドを実行できる機能「ECS Exec」が利用可能になりました。

  • これまで

    • EC2 : SSH でログインした後に docker exec コマンドを実行。
    • Fargate : そもそもできない。
  • 仕組み

    • セッションマネージャーの仕組みを利用しており、これには、ECS Exec に対応済みの特定バージョン以降を利用している必要がある。⬇️
  • ECS Exec に対応済みの特定バージョン

    • EC2 : ECS-optimized AMIの 2021年1月20日以降にリリースされたバージョン。
    • Fargate : Fargate のプラットフォームバージョン 1.4.0以降。
  • AWS CLI (現時点では、CLIからしか行えません。)

    • AWS CLI v1 : バージョン 1.19.28 またはそれ以降
    • AWS CLI v2 : バージョン 2.1.31 またはそれ以降

AWS CLI に加えて、Session Manager plugin をインストールする必要があります。⬇️

ECSタスクロール

ECSタスクロールを新規作成して、下記で作成したIAMポリシーをアタッチします。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": [
        "ssmmessages:CreateControlChannel",
        "ssmmessages:CreateDataChannel",
        "ssmmessages:OpenControlChannel",
        "ssmmessages:OpenDataChannel"
      ],
      "Resource": "*"
    }
  ]
}

--enable-execute-command

サービスを新規に作成する場合

aws ecs create-service を実行する際に --enable-execute-command を付与して、ECS Exec機能を有効にする必要があります。

今回は、既存のサービスを変更するので、⬇︎の通り行なってみました。

既存のサービスを変更する場合

$ aws ecs update-service \
> --cluster ecs-cluster \
> --service ecs-service \
> --enable-execute-command
        "enableExecuteCommand": true

なお、ECS Exec機能を無効にする場合は、--disable-execute-command です。

タスクを起動する

ECS Exec機能は --enable-execute-command で有効にした後に起動したタスクでのみ利用可能です。

現在起動中の既存のタスクは、⬇︎の通り、ECS Exec機能は無効(enableExecuteCommand": false)になっています。

$ aws ecs describe-tasks \
> --cluster ecs-cluster \
> --tasks 31ebca28dd044e46a6d253550e38f2b2

            "enableExecuteCommand": false,

--desired-count 2 にしタスクをもう一つ起動すると、新しく起動したタスクは、ECS Exec機能が有効(enableExecuteCommand": true) となっていることがわかります。

$ aws ecs update-service \
> --cluster ecs-cluster \
> --service ecs-service \
> --desired-count 2

        "desiredCount": 2,

            "enableExecuteCommand": true,

execute-command

execute-command で、bin/sh などを実行してみましょう。

$ aws ecs execute-command \
> --cluster ecs-cluster \
> --task b243e1cda7ba4212b71637ade1124c48 \
> --container ecs-conntener \
> --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-0aa48e585657edbd0

リンク

Amazon ECS

12
9
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
12
9