LoginSignup
4
3

More than 3 years have passed since last update.

ecspressoで作ったサービスでexecute-commandを使えるようにする

Last updated at Posted at 2021-04-09

普段からECSを使っている方なら知っていると思いますが、2021/03/16(JST)にECSの管理下にあるコンテナに入ることができるようになりました :tada:
特にFargateでは、以前はなかなか回りくどい方法をとらないといけなかったのが、公式に機能が提供されたインパクトが大きく、界隈が盛り上がっていたように思います。

コンテナに入るコマンド aws ecs execute-command ... を使えるようにする方法は、公式ドキュメント他多くの情報があります。

ところで、私は普段ECSでデプロイを行う際に ecspresso を使っています。

ecspressoの導入や使い方についてはREADMEに詳しく書いてあるので省略しますが、execute-commandを使えるようにするのに少し躓いたので、有効化する方法を紹介します。

なお、以下は2021/04/09現在の最新版である v1.4.0 での事例であり、バージョンアップによって解消しているかもしれないのでご了承ください。

(2021/04/13追記) v1.5.0でexecute-commandに対応しました :sparkles:

ということで、v1.5.0以降のバージョンを使いましょうw

GUIでサービスを作る場合、execute-commandを有効化できない

2021/04/09現在、GUIでは設定できないようです。aws-cliで設定すればいいのですが、ecspressoの設定ファイルにサービスを定義する設定ファイルが含まれているので、そのファイルにexecute-commandを有効にする設定を追加すれば良さそうな気がしますよね。

設定を追加してデプロイするも、有効にならず

execute-commandが有効なサービスでは、サービス定義に "enableExecuteCommand": true という情報が含まれているので、サービス定義ファイルにそのまま追加してデプロイしてみます。

ecs-service-def.json
{
  "deploymentConfiguration": {
    "deploymentCircuitBreaker": {
      "enable": false,
      "rollback": false
    },
    "maximumPercent": 200,
    "minimumHealthyPercent": 100
  },
  "desiredCount": 1,
  "enableECSManagedTags": true,
+ "enableExecuteCommand": true,
  "launchType": "FARGATE",
  (以下省略)
}

デプロイが完了したら、execute-commandを実行してみましょう。

$ aws ecs execute-command --cluster CLUSTER_NAME --task TASK_ID --interactive --command sh
実行結果
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.

おや?有効になっていないぞ… :thinking:
aws-cliでサービスの詳細を出力しても、 "enableExecuteCommand": true になっていません。

$ aws ecs describe-services --cluster CLUSTER_NAME --services SERVICE_NAME
実行結果(抜粋)
            ...
            "enableECSManagedTags": true,
            "propagateTags": "NONE",
            "enableExecuteCommand": false # trueになっていてほしい
        }
    ],
    "failures": []
}

おそらくですが、ecspressoで使われている aws-sdk-go がexecute-commandに対応していないからだと思います。SDKやGoの事情はあまり詳しくありませんが、単にSDKのバージョンの問題なのかもしれません。

aws-cliで有効化する

対応していないものはしょうがないので、aws-cliでexecute-commandを有効化します。

$ aws ecs update-service --cluster CLUSTER_NAME --service SERVICE_NAME --enable-execute-command
実行結果(抜粋)
        ...
        "enableECSManagedTags": true,
        "propagateTags": "NONE",
        "enableExecuteCommand": true
    }
}

設定は有効になりましたが、execute-commandが使えるようになるのは次に起動するタスクからです。すぐに使いたい場合は、--force-new-deployment というオプションを付けてupdate-serviceを実行するとタスクを作り直してくれるので、新しく作成されたタスクでexecute-commandを実行できます :clap:

$ aws ecs execute-command --cluster CLUSTER_NAME --task NEW_TASK_ID --interactive --command sh
実行結果
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.


Starting session with SessionId: ecs-execute-command-0123456789abcdef
/ #

最後に

ecspressoもすぐに対応されるんじゃないかと思うので、気長に待ちましょう :tea:

余談ですが、TerraformのAWSプロバイダはすでに対応されています。早いですね〜。

4
3
2

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
4
3