如何排查无法在具有 Windows 实例的 CloudFormation 堆栈中引导的帮助程序脚本的问题?

2 分钟阅读
0

我的帮助程序脚本无法在具有 Windows 实例的 AWS CloudFormation 堆栈中引导。

简短描述

重启 Windows 实例。如果在您重新启动 Windows 实例后帮助程序脚本未运行,请完成排查引导问题部分中的步骤。

如果收到以下错误,则您可能遇到了 cfn-signal 问题: “Received 0 conditions when expecting X or Failed to receive X resource signal(s) within the specified duration.” 要解决此问题,请完成排查 cfn-signal 问题部分中的步骤。

对于这两个问题,请完成使用 CloudFormation 遵循 Windows 操作系统最佳实践部分中的步骤。

解决方法

排查引导问题

检查 cfn-init 配置集的 commands 部分,验证 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 v2 代理发布 Windows 事件日志。
  • (对于使用 EC2Config 服务的 Windows 实例)配置日志: C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt
  • (对于使用 EC2Launch v1 代理的 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::WaitConditionTimeout。有关示例配置,请参阅启动 Windows 堆栈的示例
    **注意:**Windows 实例通常比 Linux 实例需要更长的时间来完成其初始启动过程。

  3. 如果使用自定义亚马逊机器映像(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 个月前