amazon-web-services - AWS Cloudformation Userdata powershell 脚本未在自定义 Windows Server AMI 上运行

标签 amazon-web-services powershell aws-cloudformation

我已尝试 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:

  1. In the Amazon EC2 console, locate or create a Windows Server 2016 AMI.
  2. Launch a Windows instance from the AMI.
  3. Connect to your Windows instance and customize it.
  4. Search for and run the EC2LaunchSettings application. It is located in the following directory by default: C:\ProgramData\Amazon\EC2-Windows\Launch\Settings.
  5. 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模板执行结果成功。

引用文献:

  1. Microsoft System Preparation Tool configuration
  2. More information on SysPrep

关于amazon-web-services - AWS Cloudformation Userdata powershell 脚本未在自定义 Windows Server AMI 上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51649950/

相关文章:

powershell - 如何在PowerShell中设置 "jupyter notebook"的别名?

Powershell 无法返回正确的退出代码

aws-lambda - CloudFormation 自定义资源未完成删除

django - 即使在 Amazon Linux 上关闭 shell 后如何运行 django web 服务器

java - AWS S3 - com.amazonaws.AmazonServiceException : Request ARN is invalid

powershell - 为什么这个 powershell for 循环在 4 次迭代后停止

amazon-web-services - 云形成 : How to update a particular without updating the entire stackset?

amazon-web-services - 阅读文档时遇到的 AWS PrivateLink + CloudFormation 设置问题

amazon-web-services - 带有 If-Modified-Since header 的 S3 存储图像不起作用

amazon-web-services - 无法在 yum 上下载 caddy 包