9
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ECS + Fargate構成でECS Execを使おうとしたらハマったポイント

Posted at

ECS Execとは

今までFargateで動いているコンテナの中に入りたい場合、AWS System managerを使ってアクティベートコードを取得して、Dockerfileに書いてなどをする必要があった。ちょっとめんどくさい。
https://qiita.com/pocari/items/3f3d77c80893f9f1e132

それを簡単に出来るようになったのがECS Execということみたい。
docker execコマンドみたいな感じ。つまりコンテナの中に入れる。
https://aws.amazon.com/jp/blogs/news/new-using-amazon-ecs-exec-access-your-containers-fargate-ec2/

ECS Execを使うには

こちらが参考になりました!ありがとうございます。
ざっくりまとめると、

  • ECSタスクロールの作成
  • enableExecuteCommandを有効化(タスク起動時)
  • AWS CLIを実行するアカウントにexecute-commandを実行できるIAMポリシーを付ける
  • AWS CLI 用の Session Manager プラグインをインストールする
  • AWS CLIの2.1.31以降のバージョンを使う
  • ssmmessagesのVPCエンドポイント(プライベートリンク)を作る ←ここでハマった

ハマったポイント

ECSタスクロールを作ったり、AWS CLIにSession Manager プラグインを入れるのは問題なかったのですが、

aws ecs execute-command --cluster [クラスター名] \
    --task [タスクID] \
    --container [コンテナ名] \
    --interactive \
    --command "/bin/sh"

としても一向に入れない。。

An error occurred (TargetNotConnectedException) when calling the ExecuteCommand operation: The execute command failed due to an internal error. Try again later.

TargetNotConnectedException と怒られる。。色々設定変えたり試行錯誤するも変わらず。。

aws ecs describe-tasks \
    --cluster [クラスター名] \
    --tasks [タスクID]

と打って状態を確認すると、

...
"managedAgents": [
    {
        "lastStartedAt": "2021-08-02T10:24:54.476000+09:00",
        "name": "ExecuteCommandAgent",
        "lastStatus": "RUNNING"
    }
],
...
"enableExecuteCommand": true,
...

どうやらエージェントはしっかり動いている様子。じゃあなぜ入れないのだ。。

詰まったらこれ使おう Amazon ECS Exec Checker

公式の英語ドキュメント で見つけたのですが、Amazon ECS Exec Checkerというものがあるらしい。

# git cloneする
git clone https://github.com/aws-containers/amazon-ecs-exec-checker.git
# jqコマンド入れる(入ってる人は不要)
brew install jq
./check-ecs-exec.sh [クラスター名] [タスクID]

これで設定が間違っているところないかチェックしてくれます。便利。
そうするとヒントが見えてきました。

SSM PrivateLink "com.amazonaws.ap-northeast-1.ssmmessages" not found. You must ensure your task has proper outbound internet connectivity.

ssmmessagesのVPCエンドポイント(プライベートリンク)が見つからないと言っている。
おや?これか?
ssmのVPCエンドポイント(プライベートリンク)は作ってあったので、完全に盲点でした。
試しにssmmessagesのVPCエンドポイント(プライベートリンク)を作る。

再度実行すると、コンテナに入れました!
ssmmessagesのVPCエンドポイント(プライベートリンク)も必須なんですね。
Fargateは難しいと思いつつ、また一つ勉強になりました。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?