我已尝试 this question 中的所有故障排除没有效果。
"UserData": {
"Fn::Base64": {
"Fn::Join": [
"",
[
"<powershell>\n",
"New-Item C:\\Temp\\test.txt -ItemType file \n",
"</powershell>\n",
"<persist>true</persist>"
]
]
}
}
test.txt 文件不会在 C:\Temp 文件夹中创建。
C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log:
2018/01/19 02:26:55Z: Userdata execution begins
2018/01/19 02:26:55Z: Zero or more than one <persist> tag was not provided
2018/01/19 02:26:55Z: Unregistering the persist scheduled task
2018/01/19 02:27:18Z: Zero or more than one <runAsLocalSystem> tag was not provided
2018/01/19 02:27:18Z: Zero or more than one <script> tag was not provided
2018/01/19 02:27:19Z: Zero or more than one <powershellArguments> tag was not provided
2018/01/19 02:27:19Z: <powershell> tag was provided.. running powershell content
2018/01/19 07:49:57Z: Userdata execution begins
2018/01/19 07:49:57Z: Failed to get metadata: The result from http://169.254.169.254/latest/user-data was empty
2018/01/19 07:50:00Z: Unable to execute userdata: Userdata was not provided
2018/01/19 07:50:00Z: Userdata execution done
但我可以在 http://169.254.169.254/latest/user-data
<powershell>
New-Item C:\Temp\test.txt -ItemType file
</powershell>
<persist>true</persist>
最佳答案
TLDR; By running SysPrep on the instance used to create the custom AMI, my userdata scripts are now executing on first launch by Cloudformation.
使用 EC2Launch 设置应用程序运行 Sysprep:
- In the Amazon EC2 console, locate or create a Windows Server 2016 AMI.
- Launch a Windows instance from the AMI.
- Connect to your Windows instance and customize it.
- Search for and run the EC2LaunchSettings application. It is located in the following directory by default:
C:\ProgramData\Amazon\EC2-Windows\Launch\Settings
.- Select or clear options as needed. These settings are stored in the LaunchConfig.json file. 6.Choose Shutdown with Sysprep.
为 Windows Server 2016 配置 Sysprep 后 可以安全地创建 AMI 的自定义镜像。添加新创建的 AMI转CFN模板执行结果成功。
引用文献:
block 引用>
关于amazon-web-services - AWS Cloudformation Userdata powershell 脚本未在自定义 Windows Server AMI 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51649950/