Amazon Elastic Container Service (Amazon ECS) Exec がアクティブ化されている AWS Fargate タスクの AWS Systems Manager Agent (SSM Agent) ログを取得したいと考えています。
簡単な説明
前提条件:
ECS Exec を使用するための要件をすべて満たします。
ECS Exec がアクティブ化されている Fargate タスクの SSM エージェントログを取得するには、mazon Elastic File System (Amazon EFS) ファイルシステムを作成します。次に、Amazon EFS ファイルシステムを Fargate コンテナにマウントします。最後に、同じファイルシステムを Amazon Elastic Compute Cloud (Amazon EC2) インスタンスにマウントして SSM エージェントログを取得します。
**重要:**EFS ファイルシステムをマウントする前に、Amazon EC2 インスタンスを起動する必要があります。Amazon EFS ファイルシステム、Amazon ECS クラスター、Fargate タスクは、同じ Amazon Virtual Private Cloud (Amazon VPC) にある必要があります。
解決策
**注:**以下の解決策は、ECS Exec がアクティブになっている Fargate タスクのみを対象としています。この解決手順は、デバッグのみに使用してください。ログが上書きされないようにするには、タスクをスタンドアロンタスクとして起動するか、Amazon ECS サービスで、desiredCount タスク数を「1」にしておきます。コンテナからの stderr/stdout 以外のログを確認する必要があるシナリオでは、次の解決策を使用することもできます。
Amazon EFS ファイルシステムを作成し、それを Fargate コンテナにマウントします。
Amazon EFS ファイルシステムを作成し、タスクまたはサービス内の Fargate コンテナにマウントするには、次の手順を実行します。
- Amazon EFS ファイルシステムを作成します。
- Amazon EFS ID とセキュリティグループ ID を書き留めておきます。
- ファイルシステムのセキュリティグループルールを編集して、Fargate タスクに関連付けられているセキュリティグループのポート 2049 でのインバウンド接続を許可します。
- Amazon ECS セキュリティグループを更新して、ポート 2049 でファイルシステムのセキュリティグループへのアウトバウンド接続を許可します。
- Amazon ECS コンソールを開きます。
- ナビゲーションペインで、タスク定義をクリックし、使用するタスク定義を選択します。
- 新しいリビジョンを作成を選択します。
- ストレージのボリュームで、ボリュームの追加を選択します。次の情報を入力します。
ボリューム名に、ボリュームの名前を入力します。
ボリュームタイプに EFS と入力します。
ファイルシステム ID に、ファイルシステムの ID を入力します。
- コンテナマウントポイントで、マウントポイントを追加を選択します。次の情報を入力します。
コンテナ で、コンテナを選択します。
ソースボリュームで、ソースボリュームを選択します。
コンテナパスに /var/log/amazon と入力します。
- 作成を選択します。
注:Amazon EFS ファイルシステムを Amazon EC2 インスタンスにマウントする前に、タスクが実行状態になっている必要があります。
Amazon EFS ファイルシステムを Amazon EC2 インスタンスにマウントし、SSM エージェントログを取得する
**注:**AWS コマンドラインインターフェイス (AWS CLI) コマンドの実行中にエラーが発生した場合は、「AWS CLI エラーのトラブルシューティング」を参照してください。また、使用している AWS CLI が最新バージョンであることを確認してください。
次の手順を実行します。
-
ファイルシステムを EC2 インスタンスにマウントします。
-
ログデータを取得するには、次の AWS CLI コマンドを実行します。
sudo mount -t nfs4 -o nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2,noresvport fs-01b0bxxxxxxxx.efs.ap-southeast-1.amazonaws.com:/ /efs
出力例:
# df -h
Filesystem Size Used Avail Use% Mounted on
fs-01b0bxxxxxxxx.efs.us-west-2.amazonaws.com:/ 8.0E 0 8.0E 0% /efs
以下は、Fargate コンテナの /var/log/amazon/ssm/amazon-ssm-agent.log パスに保存されているログの例です。
[root@ip-172-31-32-32 efs]# cd ssm/
[root@ip-172-31-32-32 ssm]# ls
amazon-ssm-agent.log audits
[root@ip-172-31-32-32 ssm]# cat amazon-ssm-agent.log | tail -n 10
2022-10-20 11:50:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] ended idempotency deletion thread
2022-10-20 11:50:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started
2022-10-20 11:50:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done
2022-10-20 11:55:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started
2022-10-20 11:55:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done
2022-10-20 12:00:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] started idempotency deletion thread
2022-10-20 12:00:34 WARN [ssm-agent-worker] [MessageService] [MessageHandler] [Idempotency] encountered error open /var/lib/amazon/ssm/170b15cacf5846ed836bcd7903cbee48-2531612879/idempotency: no such file or directory while listing replies in /var/lib/amazon/ssm/170b15cacf5846ed836bcd7903cbee48-2531612879/idempotency
2022-10-20 12:00:34 INFO [ssm-agent-worker] [MessageService] [MessageHandler] ended idempotency deletion thread
2022-10-20 12:00:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread started
2022-10-20 12:00:37 INFO [ssm-agent-worker] [MessageService] [MGSInteractor] send failed reply thread done
[root@ip-172-31-32-32 ssm]#