Amazon Inspectorを利用するために、awsagentをインストールしている環境がありました。最初は問題無く動いていたawsagentが数時間後にエラーとなる事象にあったので情報残しておきます。
要点
IMDSv2の強制設定と、awsagentの起動は順序が大事。
IMDSv2強制設定
⇒ awsagentの起動
事象
インストール後数時間は問題無く動くのですが、翌日などにInspectorの画面から見てみると以下の用にHealthyでなくUNKNOWNのステータスになっていることがありました。
ホストにログオンしてログを見てみたところです。起動後数時間後にエラーが出ているのがわかります。
$ sudo journalctl --full --no-pager -u awsagent.service
# 省略--
Jan 03 23:44:50 ip-xxx awsagent[4603]: Region : 'ap-northeast-1'
Jan 03 23:44:50 ip-xxx awsagent[4603]: [ OK ]
Jan 03 23:44:50 ip-xxx systemd[1]: Started LSB: Amazon Inspector service.
Jan 03 23:47:57 ip-xxx awsagent[4669]: Core/MetaDataClient.cpp:88:HTTP Request on URL http://169.254.169.254/latest/api/token to EC2 metadata client service returned nullptr.
Jan 03 23:47:57 ip-xxx awsagent[4669]: Core/MetaDataClient.cpp:106:HTTP Request on URL http://169.254.169.254/latest/meta-data/placement/availability-zone to EC2 metadata client service failed with HttpResponseError: 401 on retry 0.
Jan 03 23:47:57 ip-xxx awsagent[4669]: Publishers/Publisher.cpp:382:Failed to retrieve availability zone: -97
Jan 04 00:48:06 ip-xxx awsagent[4669]: Publishers/ArsenalPublisher.cpp:1588:PublishEvents operation not permitted failure : Publishing data to service with no subscription.
Jan 04 02:05:25 ip-xxx awsagent[4669]: Core/MetaDataClient.cpp:88:HTTP Request on URL http://169.254.169.254/latest/meta-data/placement/availability-zone to EC2 metadata client service returned nullptr.
Jan 04 02:05:25 ip-xxx awsagent[4669]: Publishers/Publisher.cpp:382:Failed to retrieve availability zone: -1
Jan 04 02:58:07 ip-xxx awsagent[4669]: Core/MetaDataClient.cpp:106:HTTP Request on URL http://169.254.169.254/latest/meta-data/placement/availability-zone to EC2 metadata client service failed with HttpResponseError: 401 on retry 0.
Jan 04 06:19:13 ip-xxx awsagent[4669]: Publishers/ArsenalPublisher.cpp:742:RequestConfig failure : Unable to parse ExceptionName: ExpiredTokenException Message: The security token included in the request is expired
Jan 04 06:19:13 ip-xxx awsagent[4669]: Service/MainInspectorThread.cpp:79:Config retrieval failed : -97
Jan 04 06:21:13 ip-xxx awsagent[4669]: Publishers/ArsenalPublisher.cpp:557:UpdateHealth failure : Unable to parse ExceptionName: ExpiredTokenException Message: The security token included in the request is expired Msg: {"t":1609741273921,"proxy":0,"o":"Amazon Linux release 2 (Karoo)","k":"4.14.209-160.335.amzn2.x86_64","r":"Unrecognized failure : Unable to parse ExceptionName: ExpiredTokenException Message: The security token included in the request is expired","s":17,"d":0,"l":51,"m":0}
Jan 04 06:21:13 ip-xxx awsagent[4669]: Service/MainInspectorThread.cpp:96:Regular health update failed : 0
Jan 04 07:19:20 ip-xxx awsagent[4669]: Publishers/ArsenalPublisher.cpp:742:RequestConfig failure : Unable to parse ExceptionName: ExpiredTokenException Message: The security token included in the request is expired
Jan 04 07:19:20 ip-xxx awsagent[4669]: Service/MainInspectorThread.cpp:79:Config retrieval failed : -97
Jan 04 07:21:20 ip-xxx awsagent[4669]: Publishers/ArsenalPublisher.cpp:557:UpdateHealth failure : Unable to parse ExceptionName: ExpiredTokenException Message: The security token included in the request is expired Msg: {"t":1609744880720,"proxy":0,"o":"Amazon Linux release 2 (Karoo)","k":"4.14.209-160.335.amzn2.x86_64","r":"Unrecognized failure : Unable to parse ExceptionName: ExpiredTokenException Message: The security token included in the request is expired","s":17,"d":0,"l":132,"m":0}
Jan 04 07:21:20 ip-xxx awsagent[4669]: Service/MainInspectorThread.cpp:96:Regular health update failed : 0
気になるのは以下のメッセージです。
The security token included in the request is expired
既にIMDSv2を有効化していくつもハマってきた後だったので、awsagentがIMDSv2に対応していないのではと疑ってサポートに聞いてみました。
解決
何度かやりとりして調査していった結果、要約に記載した通りです。
Inspector エージェントはawsagentはIMDSv1 および IMDSv2 の両方に対応しているのですが、バージョンの判別をサービス起動時に行うようです。
最初は起動テンプレート内のユーザデータに以下のように設定していました。これだとIMDSv2と判別されずトークンのやりとりが上手くいかなくなり、セッショントークンの期限切れと共にエラーとなります。
curl -o /tmp/install https://inspector-agent.amazonaws.com/linux/latest/install
/bin/bash /tmp/install
aws ec2 modify-instance-metadata-options \
--region ${REGION} \
--instance-id ${INSTANCE_ID} \
--http-tokens required
ということで以下のように起動テンプレート内の処理の順序を変えることで会場しました。
aws ec2 modify-instance-metadata-options \
--region ${REGION} \
--instance-id ${INSTANCE_ID} \
--http-tokens required
# IMDSv2強制後にインストール
curl -o /tmp/install https://inspector-agent.amazonaws.com/linux/latest/install
/bin/bash /tmp/install
# またはリスタートするなど
/etc/init.d/awsagent restart