Amazon EC2 custom AMI not running bootstrap (user-data)

前端 未结 4 770
后悔当初
后悔当初 2020-11-30 07:49

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 scrip

4条回答
  •  攒了一身酷
    2020-11-30 08:21

    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:

    For EC2Config version 2.1.10 and later, or for EC2Launch, you can use true in the user data to enable the plug-in after user data execution.

    User data example:

    
        insert script here 
     
    true
    

    For subsequent boots:

    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.

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

    C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule
    

    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:\ProgramData\Amazon\EC2-Windows\Launch\Module\Scripts\Invoke-Userdata.ps1.

    Further troubleshooting:

    If you're having additional issues with your user data scripts, you can find the user data execution logs at C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log for instances sourced from the WS 2016 base AMI.


    Original Answer: For EC2Config and older versions of Windows Server

    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 Files\Amazon\Ec2ConfigService\Settings\Config.xml.

    The documentation for "Configuring a Windows Instance Using the EC2Config Service" suggests several options:

    • 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 Files\Amazon\Ec2ConfigServer\Scripts\UserScript.ps1.

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

    Example, from the documentation:

    
    $EC2SettingsFile="C:\Program Files\Amazon\Ec2ConfigService\Settings\Config.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)
    
    
    • Starting with EC2Config version 2.1.10, you can use true to enable the plug-in after user data execution.

    Example, from the documentation:

    
        insert script here
    
    true
    

提交回复
热议问题