AWS-InstallWindowsUpdatesの再起動フラグを有効にすると再起動後に発生するパッチも対応してくれます

AWS-InstallWindowsUpdatesの再起動フラグを有効にすると再起動後に発生するパッチも対応してくれます

Clock Icon2025.02.07

初めに

AWS System ManagerではWindowsUpdateを適用するためのSSMドキュメントとしてAWS-InstallWindowsUpdatesを提供しています。

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/patch-manager-ssm-documents.html#patch-manager-ssm-documents-recommended-AWS-InstallWindowsUpdates

このドキュメントは現時点のバージョン(v1)ではAllow Rebbotオプションが提供されており、これをTrueとすることで適用後にそのまま再起動まで実施してくれます。

Allow Reboot
(Optional) Specify whether to reboot the instance after installing Windows Updates. The default value is True, which means that instances reboot if any updates are installed. Warning: If you specify False, then some updates might fail to install.

今回こちらを適用する対象のEC2インスタンス(Windows Server)では元より夜間停止が予定されているため本SSMドキュメントの再起動を利用せずとも自然と翌日には再起動による適用が行われる状態となっておりました。

  • Allow Reboot = Falseとして夜間の自動停止、翌日の自動再開で適用する
  • 上記の停止とは別にAllow Reboot = Trueによる明示的な再起動を行う

「シャットダウン」と「再起動」自体の挙動の違いもありますが、このドキュメントを利用して再起動をすることによる違いはあるのでしょうか。

結論

Allow Rebbot = Trueの場合、再起動後にもAWS-InstallWindowsUpdatesに記載のスクリプトと同等のものが実行されます。

そのため適用されたパッチに依存のあるパッチが存在する場合、再起動後に自動的にスキャン+適用が行われさらに再起動が行われます。

詳細

AllowRebootによる制御判定

AWS-InstallWindowsUpdatesのドキュメントは以下より確認でします。
※ Github等外部のものが見つからずマネジメントコンソールのURLとなりますのでログインが必要です

https://ap-northeast-1.console.aws.amazon.com/systems-manager/documents/AWS-InstallWindowsUpdates/content?region=ap-northeast-1

こちらのコードを追ってみるとAllowRebootによる制御に関連する処理は以下となることが追跡できます。

 if($installedUpdates -and (-not $NoReboot))
 {
     $Logger.Info("Windows Updates were successfully installed. Rebooting.")
     exit 3010
 }

$NoRebootの値はAllowRebootの真偽を反転した値が入り、$installedUpdatesはパッチのインストール処理を通った場合にTrueとなるため、何らかインストール処理が行われたかつAllowReboot = Trueが行われた場合、このスクリプトを終了コード3010で終了することがわかります。

RunCommandにおける終了コード3010

このコード終了コード自体はWindows側の予約コードとして終了されているものとなりますが、Run Commandを利用した場合は特殊な挙動をするようで以下の2つが指示されるようです。

  • スクリプト終了後に対象のノード(インスタンス)を再起動する
  • 再起動後に同様のスクリプトが実行される

https://dev.classmethod.jp/articles/reboot-ssm-run-command/

https://docs.aws.amazon.com/ja_jp/systems-manager/latest/userguide/send-commands-reboot.html
Windows マネージドインスタンスの場合は、スクリプトで exit 3010 を指定します。Linux と macOS マネージドノードの場合は、exit 194 を指定します。終了コードは AWS Systems Manager エージェント (SSM Agent) にマネージドノードの再起動を指示し、再起動の完了後にスクリプトを再起動します。再起動を開始する前に、SSM Agent はクラウドの Systems Manager サービスに対して、サーバーの再起動中に通信が中断されることを知らせます。

再起動後に加え再度AWS-InstallWindowsUpdates内のスクリプトが実行されますので、依存する後続のパッチがあればインストール再起動を行なってくれます。

実際に実行してみると以下のとおりです。
(日本語OS引っ張ってきたせいで文字化けしてます...)

02/07/2025 07:07:45 UTC | Info | Searching for Windows Updates.
02/07/2025 07:08:32 UTC | Info | Found 4 available Windows Updates.
02/07/2025 07:08:33 UTC | Info | ���ӂ̂����\�t�g�E�F�A�̍폜�c�[�� x64 - v5.131 (KB890830) - Published date: 01/14/2025
02/07/2025 07:08:33 UTC | Info | 2025-01 x64 (KB5050187) ���� Microsoft server operating system version 21H2 �p .NET Framework 3.5�A4.8 ������ 4.8.1 �̗ݐϓI�ȍX�V�v���O���� - Published date: 01/14/2025
02/07/2025 07:08:33 UTC | Info | Microsoft Defender Antivirus �̃Z�L�����e�B �C���e���W�F���X�X�V�v���O���� - KB2267602 (�o�[�W���� 1.421.1747.0) - ���݂̃`���l�� (�L��) - Published date: 02/07/2025
02/07/2025 07:08:33 UTC | Info | 2025-01 Microsoft server operating system version 21H2 x64 �x�[�X �V�X�e���p�̗ݐύX�V�v���O���� (KB5049983) - Published date: 01/14/2025
02/07/2025 07:08:33 UTC | Info | Downloading Windows Updates.
02/07/2025 07:08:41 UTC | Info | Successfully Downloaded: ���ӂ̂����\�t�g�E�F�A�̍폜�c�[�� x64 - v5.131 (KB890830) - Published date: 01/14/2025
02/07/2025 07:08:53 UTC | Info | Successfully Downloaded: 2025-01 x64 (KB5050187) ���� Microsoft server operating system version 21H2 �p .NET Framework 3.5�A4.8 ������ 4.8.1 �̗ݐϓI�ȍX�V�v���O���� - Published date: 01/14/2025
02/07/2025 07:08:54 UTC | Info | Successfully Downloaded: Microsoft Defender Antivirus �̃Z�L�����e�B �C���e���W�F���X�X�V�v���O���� - KB2267602 (�o�[�W���� 1.421.1747.0) - ���݂̃`���l�� (�L��) - Published date: 02/07/2025
02/07/2025 07:09:20 UTC | Info | Successfully Downloaded: 2025-01 Microsoft server operating system version 21H2 x64 �x�[�X �V�X�e���p�̗ݐύX�V�v���O���� (KB5049983) - Published date: 01/14/2025
02/07/2025 07:09:20 UTC | Info | 4 Windows Updates will be installed.
02/07/2025 07:11:03 UTC | Info | Installed: ���ӂ̂����\�t�g�E�F�A�̍폜�c�[�� x64 - v5.131 (KB890830)
02/07/2025 07:26:38 UTC | Info | Installed: 2025-01 x64 (KB5050187) ���� Microsoft server operating system version 21H2 �p .NET Framework 3.5�A4.8 ������ 4.8.1 �̗ݐϓI�ȍX�V�v���O����
02/07/2025 07:26:40 UTC | Info | Installed: Microsoft Defender Antivirus �̃Z�L�����e�B �C���e���W�F���X�X�V�v���O���� - KB2267602 (�o�[�W���� 1.421.1747.0) - ���݂̃`���l�� (�L��)
02/07/2025 07:33:42 UTC | Info | Installed: 2025-01 Microsoft server operating system version 21H2 x64 �x�[�X �V�X�e���p�̗ݐύX�V�v���O���� (KB5049983)
02/07/2025 07:33:43 UTC | Info | Windows Updates were successfully installed. Rebooting.
02/07/2025 07:36:37 UTC | Info | Searching for Windows Updates.
02/07/2025 07:37:39 UTC | Info | Found 0 available Windows Updates.

ポイントとしては最後3行で、上記の終了コード3010を返す処理の直前に出力されるログの後、処理開始同様アップデートのスキャンのログが流れていることがわかります。

今回は依存するアップデートがなかったためここで終了となっていますが、同様の処理が繰り返されているだけですのでここでアップデートが引っ掛かれば適用、そして再起動と繰り返される形になります。

終わりに

AWS-InstallWindowsUpdatesの再起動の挙動について確認してみました。

フラグの説明としては「再起動する」としかないので依存関係のあるアップデートが残っている限り再帰的に繰り返すのは意外と見落としやすいところではないでしょうか。

多くのケースでは残留するアップデートがないように本SSMドキュメントに任せた再起動をする方が好ましそうですが、起動時に処理を組み込んでいる場合想定以上の回数が呼び出される可能性もあるので注意しましょう。

Share this article

facebook logohatena logotwitter logo

© Classmethod, Inc. All rights reserved.