在 AWS CloudFormation 堆栈中帮助程序脚本无法通过 Windows 实例引导,应该如何排查问题?
上次更新时间:2020 年 6 月 19 日
我想要在 AWS CloudFormation 堆栈中使用 Windows 实例引导帮助程序脚本。
简短描述
如果帮助程序脚本在重启 Windows 实例后未运行,请完成引导问题故障排查部分中的步骤。
如果您收到错误消息“期待 X 时收到 0 条件或者在指定时间内未收到 X 资源信号”,请完成 cfn-signal 问题故障排查部分中的步骤。
最后,完成在 AWS CloudFormation 中使用 Windows 操作系统的最佳实践部分中的步骤。
解决方法
引导问题故障排查
在 Windows 实例中,UserData(作为 EC2ConfigService 的子进程运行)调用 cfn-init.exe。cfn-init.exe 执行的某些步骤可能需要系统重启。例如,如果重命名计算机或将计算机加入一个域中,您必须重启系统。在系统重启后,cfn-init 在 C:\cfn\cfn-init\resume_db.json 的帮助下继续运行 AWS::CloudFormation::Init 中剩余的其他配置。
如果脚本在重启后未运行,请完成以下步骤:
1. 在 cfn-init 配置设置的命令部分,确认 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) 配置日志,Windows 2016 之前的版本位于 C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2ConfigLog.txt (versions before Windows 2016),Window 2016 及之后的版本位于 C:\ProgramData\Amazon\EC2-Windows\Launch\Log\
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. 根据 AWS CloudFormation Windows 示例,将 WaitCondition 的超时时间增加至 1800 或 3600 秒。
注意:步骤 2 是必需的,因为与 Linux 实例相比,Windows 实例通常需要更长时间才能完成初始启动过程。
3. 如果您使用自定义 Amazon 系统映像 (AMI),则必须在开始前使用 Sysprep 创建 AMI。如果不使用 Sysprep,您可能会遇到元数据问题,并在元数据的 userdata 日志中收到以下错误:
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
在 AWS CloudFormation 中使用 Windows 操作系统的最佳实践
在 PowerShell 脚本的开头包含 $ErrorActionPreference = "Stop",以允许捕获异常。
如果您在 AWS CloudFormation 模板中引用了 Windows 路径,则必须为所有 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 CloudFormation Windows 堆栈。