如何排查 CloudFormation 堆栈中无法通过 Windows 实例引导的帮助程序脚本?
我的帮助程序脚本不会在 AWS CloudFormation 堆栈中引导 Microsoft Windows 实例。如何解决此问题?
简短描述
在 Windows 实例中,UserData(作为 EC2ConfigService 的子进程运行)调用 cfn-init.exe。cfn-init.exe 执行的某些步骤可能需要系统重启。
例如,如果重命名计算机或将计算机加入一个域中,您必须重启系统。系统重新启动后,cfn-init 将继续运行 AWS::CloudFormation::Init 中的其余配置,并在 C:\cfn\cfn-init\resume_db.json 的帮助下运行。
如果帮助程序脚本在重启 Windows 实例后未运行,请完成引导问题故障排查部分中的步骤。
如果您收到以下错误,则说明您可能在使用 cfn-signal 时遇到问题:
“在预期 X 时接收到 0 个条件,或在指定的持续时间内未能接收到 X 个资源信号”
要解决上述错误,请完成排除 cfn-signal 问题部分中的步骤。
针对这两个问题,完成在 CloudFormation 中使用 Windows 操作系统的最佳实践部分中的步骤。
解决方法
引导问题故障排查
如果脚本在重启后未运行,请完成以下步骤:
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) 配置在 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 堆栈的示例,将WaitCondition 的超时时间增加至 1800/3600 秒钟。
**注意:**步骤 2 是必需的,因为与 Linux 实例相比,Windows 实例通常需要更长时间才能完成初始启动过程。
3. 如果您使用自定义亚马逊云机器镜像(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 官方已更新 2 年前
- AWS 官方已更新 1 年前
- AWS 官方已更新 1 个月前