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

2 分的閱讀內容
0

我的協助程式指令碼不會在 Windows 執行個體的 AWS CloudFormation 堆疊中引導。

簡短說明

重新啟動 Windows 執行個體。如果重新啟動 Windows 執行個體之後,協助程式指令碼不會執行,請完成疑難排解引導問題區段中的步驟。

如果您收到下列錯誤,您可能會發生 cfn-signal 的問題: 「收到 0 個條件,但預期為 X 個,或無法在指定的持續時間內收到 X 個資源訊號。」 若要解決此問題,請完成疑難排解 cfn-signal 問題區段中的步驟。

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

解決方法

疑難排解引導問題

檢查 cfn-init 組態集的命令區段,驗證 waitAfterCompletion 是否已設定為 forever。範例:

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

**注意:**forever 值會指示 cfn-init 結束,並且只有在重新啟動完成之後才會回復。如需詳細資訊,請參閱命令

驗證組態之後,請檢閱下列日誌以取得錯誤:

  • cfn-init 日誌: C:\cfn\log\cfn-init.log
  • Windows 事件日誌: C:\Windows\System32\winevt\logs
    **注意:**Amazon Elastic Cloud Compute (Amazon EC2) EC2Launch 第 2 版代理程式會發佈 Windows 事件日誌。
  • (對於使用 EC2Config 服務的 Windows 執行個體) 組態日誌: C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt
  • (對於使用 EC2Launch 第 1 版代理程式的 Windows 執行個體) 啟動日誌: C:\ProgramData\Amazon\EC2-Windows\Launch\Log

疑難排解 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. 增加 AWS::CloudFormation::WaitCondition逾時。如需組態範例,請參閱引導 Windows 堆疊的範例
    **注意:**Windows 執行個體通常需要比 Linux 執行個體更長時間才能完成其初始啟動程序。

  3. 如果您使用自訂 Amazon Machine Image (AMI),則必須使用 Sysprep 來建立 AMI。如果您不使用 Sysprep,則可能會在中繼資料的使用者資料日誌中收到下列錯誤: "Failed to get metadata: The result from 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 官方已更新 4 個月前