如何對不會在具有 Windows 執行個體的 CloudFormation 堆疊中執行引導操作的協助程式指令碼進行疑難排解?

2 分的閱讀內容
0

我的協助程式指令碼不會在具有 Microsoft Windows 執行個體的 AWS CloudFormation 堆疊中執行引導操作。如何解決此問題?

簡短說明

在 Windows 執行個體中,作為 EC2ConfigService 子處理程序執行的 UserData 會叫用 cfn-init.exe。**cfn-init.exe ** 執行的某些步驟可能需要重新啟動系統。

例如,如果您重新命名電腦,或將電腦加入網域,則必須重新啟動系統。系統重新啟動後,**cfn-init ** 會借助於 C:\cfn\cfn-init\resume_db.json,繼續執行留在 AWS::CloudFormation::Init 的剩餘組態。

對於重新啟動 Windows 執行個體後無法執行的協助程式指令碼,請完成疑難排解引導操作問題區段中的步驟。

如果您收到下列錯誤,表示您可能會遇到 cfn-signal 的問題:

「收到 0 個條件,但預設為 X 個,或無法在指定的持續時間內收到 X 個資源訊號」

若要解決上述錯誤,請完成疑難排解 cfn-signal 問題區段中的步驟。

對於這兩個問題,請完成遵循搭配 CloudFormation 使用 Windows 作業系統的最佳實務區段中的步驟。

解決方案

疑難排解引導操作問題

如果重新開機後仍未執行您的指令碼,請完成下列步驟:

1.    在 cfn-init 組態集的 commands 區段,確認 waitAfterCompletion 已設為 forever。例如:

"commands": {
 "0-restart": {
 "command": "powershell.exe -Command Restart-Computer",
 "waitAfterCompletion": "forever"
 }
 }

**注意事項:**forever 值會指示 cfn-init 結束,並且唯有重新啟動完成之後才會繼續。如需相關資訊,請參閱 AWS::CloudFormation::Init

2.    檢查下列日誌是否有錯誤:

  • Amazon Elastic Compute Cloud (Amazon EC2) 組態日誌,位於 C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt (Windows 2016 以前的版本)
  • Amazon EC2 組態日誌,位於 *C:\ProgramData\Amazon\EC2-Windows\Launch\Log* (Windows 2016 及更新版本)
  • cfn-init 日誌,位於 C:\cfn\log\cfn-init.log
  • Windows 事件日誌,位於 C:\Windows\System32\winevt\logs

疑難排解 cfn-signal 問題

1.    確認 cfn-signal 已正確設定。

**重要事項:**在 PowerShell 指令碼使用 -e $lastexitcode,並針對 Windows cmd.exe 指令碼使用 -e %ERRORLEVEL%

針對 UserData 中的 PowerShell 指令碼,請使用以下標籤:

<powershell></powershell>

範例 PowerShell 指令碼:

UserData:
        Fn::Base64:
          Fn::Sub : |
            <powershell>
            $LASTEXITCODE=0
            echo Current date and time >> C:\Temp\test.log
            echo %DATE% %TIME% >> C:\Temp\test.log
            cfn-init.exe -s ${AWS::StackId} -r SInstance --region ${AWS::Region}
            New-Item -Path "C:\" -Name userdata -ItemType directory
            cfn-signal.exe -e $LASTEXITCODE --stack ${AWS::StackId} --resource WindowsInstance --region ${AWS::Region}
            </powershell>

針對 UserData 中的 **cmd.exe ** 指令碼,請使用以下標籤:

<script></script>

範例 cmd.exe 指令碼:

UserData:
  Fn::Base64: !Sub |
    <script>
    cfn-init.exe -v -s ${AWS::StackId} -r WindowsInstance --configsets ascending --region ${AWS::Region}
    cfn-signal.exe -e %ERRORLEVEL% --stack ${AWS::StackId} --resource WindowsInstance --region ${AWS::Region}
    </script>

2.    根據引導操作 Windows 堆疊的範例,將 WaitConditionTimeout 增加為 1800/3600 秒。

**注意事項:**步驟 2 是必要的,因為在完成其初始啟動程序時,Windows 執行個體通常需要比 Linux 執行個體更長的時間。

3.    如果您使用自訂 Amazon Machine Image (AMI),則必須在開始之前使用 Sysprep 建立 AMI。如果您未使用 Sysprep,則可能會遇到中繼資料問題,並在中繼資料的使用者資料日誌中出現下列錯誤:

Failed to get metadata: The result from http://169.254.169.254/latest/user-data was empty
Unable to execute userdata: Userdata was not provided

遵循搭配 CloudFormation 使用 Windows 作業系統的最佳實務

在 PowerShell 指令碼的頂端,請包含 $ErrorActionPreference = "Stop" 以便捕捉例外狀況。

當您在 CloudFormation 範本中參考 Windows 路徑時,必須在路徑的開頭新增正斜線 (/)。例如:

"commands" : {
  "1-extract" : {
    "command" : "C:\\SharePoint\\SharePointFoundation2010.exe /extract:C:\\SharePoint\\SPF2010 /quiet /log:C:\\SharePoint\\SharePointFoundation2010-extract.log"
  }

對於 Windows 堆疊,您必須對 WaitCondition 控點 URL 再次進行 base64 編碼。例如:

cfn-signal.exe -e %ERRORLEVEL% ", { "Fn::Base64" : { "Ref" : "SharePointFoundationWaitHandle" }}, "\n"

AWS 官方
AWS 官方已更新 2 年前