本文介绍了Amazon EC2 自定义 AMI 未运行引导程序(用户数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 EC2 实例上创建自定义 AMI(映像)时遇到问题.如果我使用自定义引导程序/用户数据脚本启动 Windows 默认 2012 服务器实例,例如;

I have encountered an issue when creating custom AMIs (images) on EC2 instances. If I start up a Windows default 2012 server instance with a custom bootstrap/user-data script such as;

<powershell>
PowerShell "(New-Object System.Net.WebClient).DownloadFile('http://download.microsoft.com/download/3/2/2/3224B87F-CFA0-4E70-BDA3-3DE650EFEBA5/vcredist_x64.exe','C:vcredist_x64.exe')"
</powershell>

它将按预期工作并转到 URL 并下载文件,并将其存储在 C: 驱动器上.

It will work as intended and go to the URL and download the file, and store it on the C: Drive.

但如果我设置了一个 Windows Server 实例,然后从中创建一个映像,并将其存储为自定义 AMI,然后使用完全相同的自定义用户数据脚本部署它,它将无法工作.但是,如果我转到实例 url (http://169.254.169.254/latest/user-data),它将显示脚本已成功导入但尚未执行.

But if I setup a Windows Server Instance, then create a image from it, and store it as a Custom AMI, then deploy it with the exact same custom user-data script it will not work. But if I go to the instance url (http://169.254.169.254/latest/user-data) it will show the script has imported successfully but has not been executed.

在检查错误日志后,我经常注意到这一点:

After checking the error logs I have noticed this on a regular occasion:

Failed to fetch instance metadata http://169.254.169.254/latest/user-data with exception The remote server returned an error: (404) Not Found.

推荐答案

更新 4/15/2017:适用于 EC2Launch 和 Windows Server 2016 AMI

根据 EC2Launch 的 AWS 文档,Windows Server 2016 用户可以继续使用 EC2Config 2.1.10 中引入的持久标签:

Update 4/15/2017: For EC2Launch and Windows Server 2016 AMIs

Per AWS documentation for EC2Launch, Windows Server 2016 users can continue using the persist tags introduced in EC2Config 2.1.10:

对于 EC2Config 版本 2.1.10 及更高版本,或者对于 EC2Launch,您可以使用true 在用户数据中启用插件后用户数据执行.

用户数据示例:

<powershell>
    insert script here
</powershell>
<persist>true</persist>

对于后续启动:

Windows Server 2016 用户必须另外启用 配置和启用EC2Launch 而不是 EC2Config.EC2Config 在 Windows Server 2016 AMI 上已被弃用,以支持 EC2Launch.

Windows Server 2016 users must additionally enable configure and enable EC2Launch instead of EC2Config. EC2Config was deprecated on Windows Server 2016 AMIs in favor of EC2Launch.

运行以下 powershell 以安排将在下次启动时运行用户数据的 Windows 任务:

Run the following powershell to schedule a Windows Task that will run the user data on next boot:

C:ProgramDataAmazonEC2-WindowsLaunchScriptsInitializeInstance.ps1 –Schedule

根据设计,此任务在第一次运行后被禁用.但是,使用persist 标签会导致Invoke-UserData 通过Register-FunctionScheduler 安排一个单独的任务,以便在后续启动时保留您的用户数据.您可以在 C:ProgramDataAmazonEC2-WindowsLaunchModuleScriptsInvoke-Userdata.ps1 中亲自查看.

By design, this task is disabled after it is run for the first time. However, using the persist tag causes Invoke-UserData to schedule a separate task via Register-FunctionScheduler, to persist your user data on subsequent boots. You can see this for yourself at C:ProgramDataAmazonEC2-WindowsLaunchModuleScriptsInvoke-Userdata.ps1.

进一步的故障排除:

如果您的用户数据脚本有其他问题,您可以在 C:ProgramDataAmazonEC2-WindowsLaunchLogUserdataExecution.log 中找到用户数据执行日志对于源自 WS 2016 基础 AMI 的实例.

If you're having additional issues with your user data scripts, you can find the user data execution logs at C:ProgramDataAmazonEC2-WindowsLaunchLogUserdataExecution.log for instances sourced from the WS 2016 base AMI.

在初始启动后自动禁用用户数据执行.创建映像时,很可能已禁用执行.这可以在 C:Program FilesAmazonEc2ConfigServiceSettingsConfig.xml 中手动配置.

User data execution is automatically disabled after the initial boot. When you created your image, it is probable that execution had already been disabled. This is configurable manually within C:Program FilesAmazonEc2ConfigServiceSettingsConfig.xml.

文档使用 EC2Config 服务配置 Windows 实例" 建议了几个选项:

  • 使用 schtasks.exe/Create 以编程方式创建在系统启动时运行的计划任务,并将计划任务指向 C 处的用户数据脚本(或其他脚本):Program FilesAmazonEc2ConfigServerScriptsUserScript.ps1.

  • Programmatically create a scheduled task to run at system start using schtasks.exe /Create, and point the scheduled task to the user data script (or another script) at C:Program FilesAmazonEc2ConfigServerScriptsUserScript.ps1.

以编程方式在 Config.xml 中启用用户数据插件.

Programmatically enable the user data plug-in in Config.xml.

示例,来自文档:

<powershell>
$EC2SettingsFile="C:Program FilesAmazonEc2ConfigServiceSettingsConfig.xml"
$xml = [xml](get-content $EC2SettingsFile)
$xmlElement = $xml.get_DocumentElement()
$xmlElementToModify = $xmlElement.Plugins

foreach ($element in $xmlElementToModify.Plugin)
{
    if ($element.name -eq "Ec2SetPassword")
    {
        $element.State="Enabled"
    }
    elseif ($element.name -eq "Ec2HandleUserData")
    {
        $element.State="Enabled"
    }
}
$xml.Save($EC2SettingsFile)
</powershell>

  • 从 EC2Config 版本 2.1.10 开始,您可以使用 在用户数据执行后启用插件.
  • 示例,来自文档:

    <powershell>
        insert script here
    </powershell>
    <persist>true</persist>
    

    这篇关于Amazon EC2 自定义 AMI 未运行引导程序(用户数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 14:46