LoginSignup
0
0

[ECS] AWS CLIでECSのコンテナにログインする方法

Posted at

はじめに

  • この記事は紆余曲折し、エラーを解消しながらゴールまで突き進む物語です
  • 最短の手順だけを述べたものではありません
  • 少しでも皆さんのデバッグのヒントになればと思っております

目的

構築済みのECSのアプリケーションコンテナにAWS CLIでログインする

前提

  • ECS構築済み
  • AWS CLI設定済み
  • aws-vault導入済み(どちらでも可)

最初に疎通確認

aws-vault exec sotaheavymetal21 -- aws sts get-caller-identity
{
    # ここにユーザー情報出力
}

該当のAWSアカウントに接続できていることを確認できました

aws ecs execute-commandを実行します

下記の<>の中を埋めていってください

aws-vault exec <ProfileName> -- aws ecs execute-command \
    --cluster <ClusterName> \
    --task <TaskID> \
    --container <ContainerName> \
    --interactive \
    --command "/bin/sh"

# 以下のように出力されたら OK (セッションが開始され、シェル入力画面 `#` が表示される)
The Session Manager plugin was installed successfully. Use the AWS CLI to start a session.

Starting session with SessionId: ecs-execute-command-0000000000000000
#

実行してみます!!

❯ aws-vault exec sotaheavymetal21 -- aws ecs execute-command \
    --cluster sota-dev-cluster \
    --task 00000000000000000000000000 \
    --container app \
    --interactive \
    --command "/bin/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.

ExecuteCommand オペレーションの呼び出し時にエラーが発生しました(InvalidParameterException): タスクの実行時に実行コマンドが有効になっていないか、実行コマンドエージェントが実行されていないため、実行コマンドに失敗しました。しばらく待ってから再試行するか、実行コマンドを有効にして新しいタスクを実行してから再試行してください。

エラーが発生しましたね
タスク実行時に実行コマンドが有効になっていないかもしれませんね
起動しているタスク定義を確認してみます

どうやら、タスク定義のenableExecuteCommandがfalseになっているかもしれません
タスク定義の状況をgrepコマンドで確認します

❯ aws-vault exec sotaheavymetal21 -- aws ecs describe-tasks  \
    --region ap-northeast-1 \
    --cluster  sota-dev-cluster  \
    --task 000000000000000000000000  \
| grep enableExecuteCommand


# 出力結果
"enableExecuteCommand": false,
 ~

EnableExecuteCommandを更新してみましょう

aws-vault exec sotaheavymetal21 -- aws ecs update-service 
--cluster <example-cluster-name> 
--service <example-service> 
--region <example-region> 
--enable-execute-command 
--force-new-deployment

実行します!

aws-vault exec sotaheavymetal21 -- aws ecs update-service 
--cluster sota-dev-cluster 
--service sota-service 
--region ap-northeast-1 
--enable-execute-command 
--force-new-deployment

よし!enableExecuteCommandがtrueになっています!

❯ aws-vault exec sotaheavymetal21 -- aws ecs describe-tasks  \
    --region ap-northeast-1 \
    --cluster  sota-dev-cluster  \
    --task 0000000000000000000000000000 \
| grep enableExecuteCommand


# 出力結果
"enableExecuteCommand": true,

再度execute-commandを実行してみましょう

❯ aws-vault exec sotaheavymetal21 -- aws ecs execute-command \
    --cluster sota-dev-cluster \
    --task 000000000000000000000000000 \
    --container app \
    --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-00000000000000000000000000
#

コンテナにログインできました!!
ヤッタァ!!

おわりに

この記事が少しでも皆さんの開発の役に立てばと思います
お疲れ様でした🐰

参考

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