Amazon Web Services, AWS, Cloud, PowerShell, Script, Server, User Data

Re-execute User Data script for windows instance in AWS

September 11, 2017

User data is one of the cool feature of AWS. You can define the code which want to execute immediately after instance is created. But once it is executed the feature is immediately disabled. If you will execute the user data changes want apply.

If you want to execute code using user data in later, you need to make changes to “config.xml”. You can use below script to enable user data feature.

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)

There is another way to do this.

Starting with EC2Config version 2.1.10, you can use true to enable the plug-in after user data execution.

Example, from the documentation:

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


Happy Coding!

You Might Also Like

No Comments

Leave a Reply