First published at https://nivleshc.wordpress.com
Bootstrapping is an awesome way of customising your instances in AWS (similar capability exists in Azure).
To enable bootstrapping, while configuring the launch instance, in Step 3: Configure Instance Details scroll down to the bottom and then expand Advanced Details.
You will notice a User data text box. This is where you can provide your bootstrap script. The script will be run when your instance is first launched.
AWS_BootstrapScript
I went ahead and entered my script in the text box and proceeded to complete my instance configuration. Once my instance was running, I initiated a Remote Desktop connection to it, to confirm that my script had run. Unfortunately, I couldn’t see any customisations (which meant my script didn’t run)
Thinking that the instance had not been able to access the user data, I opened up Internet Explorer and then browsed to the following url (this is an internal url that can be used to access the user-data)
http://169.254.169.254/latest/user-data/
I was able to successfully access the user-data, which meant that there were no issues with that.  However when checking the content, I noticed a typo! Aha, that was the reason why my customisations didn’t happen.
Unfortunately, according to AWS, user-data is only executed during launch (for those that would like to read, here is the official AWS documentation). To get the fixed bootstrap script to run, I would have to terminate my instance and launch a new one with the corrected script (I tried re-booting my windows instance after correcting my typo, however it didn’t run).
I wasn’t very happy on terminating my current instance and then launching a new one, since for those that might not be aware, AWS EC2 compute charges are rounded up to the next hour. Which means that if I terminated my current instance and launched a new one, I would be charged for 2 x 1hour sessions instead of just 1 x 1 hour!
So I set about trying to find another solution. And guess what, I did find it 🙂
Reading through the volumes of documentation on AWS, I found that when Windows Instances are provisioned, the service that does the customisations using user-data is called EC2Config. This service runs the initial startup tasks when the instance is first started and then disables them. HOWEVER, there is a way to re-enable the startup tasks later on 🙂 Here is the document that gives more information on EC2Config.
The Amazon Windows AMIs include a utility called EC2ConfigService Settings. This allows you to configure EC2Config to execute the user-data on next service startup. The utility can be found under All Programs (or you can search for it).
AWS_EC2ConfigSettings_AllApps
AWS_EC2ConfigSettings_Search
Once Open, under General you will see the following option
Enable UserData execution for next service start (automatically enabled at Sysprep) eg. or <powershell></powershell>
AWS_EC2ConfigSettings
Tick this option and then press OK. Then restart your Windows Instance.
After your Windows Instance restarts, EC2Config will execute the userData (bootstrap script) and then it will automatically remove the tick from the above option so that the userData is not executed on subsequent restarts (or service starts)
There you go. A simple way to re-run your bootstrap scripts on an AWS Windows Instance without having to terminate the current instance and launching a new one.
There are other options available in the EC2ConfigService Settings that you can explore as well 🙂

Category:
Amazon Web Services, DevOps
Tags:
, , , , ,

Join the conversation! 11 Comments

  1. YOU ARE A LIFE SAVER!! I spent all my day trying to bootstrap a Windows AMI, after doing this im now able to run the user data!!
    I’m using terraform, so in the user data I have
    user_data = “${file(“webserversetup.ps1″)}”
    After doing the steps you showed I created a new AMI and booted from that.
    Worked like a charm!!

  2. YOU ARE A LIFE SAVER!! I spent all my day trying to bootstrap a Windows AMI, after doing this im now able to run the user data!!
    I’m using terraform, so in the user data I have
    user_data = “${file(“webserversetup.ps1″)}”
    After doing the steps you showed I created a new AMI and booted from that.
    Worked like a charm!!

  3. is there a way to do this with powershell instead of checking the box in the gui?

  4. is there a way to do this with powershell instead of checking the box in the gui?

  5. AMI does not consist of this value when I create

  6. AMI does not consist of this value when I create

  7. Thanks for this. For folks looking for an automated way to do this:

    1) You can edit the following section and set it to ‘Enabled’ in the file :C:\Program Files\Amazon\Ec2ConfigService\Settings\config.xml

    Ec2HandleUserData
    Enabled

    2) Via script : (you may add additional plugins to enable in $enableElements array)

    $EC2SettingsFile = “C:\\Program Files\\Amazon\\Ec2ConfigService\\Settings\\Config.xml”
    $xml = [xml](get-content $EC2SettingsFile)
    $xmlElement = $xml.get_DocumentElement()
    $xmlElementToModify = $xmlElement.Plugins
    $enableElements = “Ec2HandleUserData”
    $xmlElementToModify.Plugin | Where-Object {$enableElements -contains $_.name} | Foreach-Object {$_.State=”Enabled”}
    $xml.Save($EC2SettingsFile)

Comments are closed.