Amazon web services AWS Cloudformation Userdata powershell脚本未在自定义Windows服务器AMI上运行

Amazon web services AWS Cloudformation Userdata powershell脚本未在自定义Windows服务器AMI上运行,amazon-web-services,powershell,amazon-cloudformation,Amazon Web Services,Powershell,Amazon Cloudformation,我尝试了所有的故障排除,但都没有用 "UserData": { "Fn::Base64": { "Fn::Join": [ "", [ "<powershell>\n",

我尝试了所有的故障排除,但都没有用

"UserData": {
                    "Fn::Base64": {
                        "Fn::Join": [
                            "",
                            [
                                "<powershell>\n",
                                "New-Item C:\\Temp\\test.txt -ItemType file \n",
                                "</powershell>\n",
                                "<persist>true</persist>"
                            ]
                        ]
                    }
                }
TLDR;通过在用于创建自定义AMI的实例上运行SysPrep,my userdata脚本现在在第一次启动时由 云层形成

使用EC2Launch设置应用程序运行Sysprep:

  • 在Amazon EC2控制台中,找到或创建Windows Server 2016 AMI
  • 从AMI启动Windows实例
  • 连接到Windows实例并对其进行自定义
  • 搜索并运行EC2LaunchSettings应用程序。它位于以下位置: 默认目录:
    C:\ProgramData\Amazon\EC2 Windows\Launch\Settings
  • 根据需要选择或清除选项。这些设置存储在LaunchConfig.json中 文件 6.使用Sysprep选择关机 为Windows Server 2016配置Sysprep后 自定义映像可以安全地创建AMI。添加新创建的 AMI到CFN模板执行成功

    参考资料:


  • 对于自定义AMI,已禁用用户数据以启用它,请在powershell中运行以下命令

    C:\ProgramData\Amazon\EC2 Windows\Launch\Scripts\InitializeInstance.ps1–计划


    然后userdata脚本应该可以工作。

    运行命令并获取AMI

    C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule
    
    <powershell>
    New-Item C:\Temp\test.txt -ItemType file 
    </powershell>
    <persist>true</persist>
    
    C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule