2

I tested with simple PowerShell script mkdir C:\tempdir When I specify it as UserData for instance launched from Amazon image, everything works fine. If I launch instance with specified UserData and make image of that instance, UserData is not executed for instances created from my custom image. What is interesting, "C:\Program Files\Amazon\Ec2ConfigService\Scripts\UserScript.ps1" contains UserData of initial launch (before image was created), although, it should contain data specified during last launch. More interesting, even UserScript.ps1 is present, it is not executed (it should create text file but text file is not present). Here is UserData from second launch

<powershell>
  mkdir C:\tempdir
</powershell>
zdenko.s
  • 931
  • 1
  • 12
  • 29

2 Answers2

2

User data scripts only run once when the instance is launched (created not started up). They are then disabled on the instance. So when when you grab an AMI from that instance and launch a new one, user data has already been disabled.

Enable UserData

To allow it run again when you launch a new one from an AMI you created you must turn user data support back on at the EC2ConfigService Settings utility by checking the UserData box (before grabbing the AMI).

Persist Enabling UserData

Now after this if you reboot the instance again then the UserData will run and again disable itself. To disable this functionality too you will need to add <persist>true</persist> to the instance's UserData which will keep the checkbox in the same state (on if you turn it on).

More info:

Community
  • 1
  • 1
eirc
  • 1,654
  • 14
  • 14
  • When running EC2ConfigService settings utility, it displays "UserData will be enabled after SysPrep by default). I tried to enable User Dtaa check box, than run SysPrep (second Tab) and make image from AWS web console. -Did not work I repeated the procedure but simply shut down instance and created image Did not work.Option to enable it permanetly is not sutiable for my scenario. – zdenko.s Jun 09 '16 at 12:01
  • I don't use SysPrep since I really don't want it to run in my case. I just ensure the checkbox is on, shut down the instance, image it and launch a new one. In my setup the old instance only has `true` in the user data and when launching the new one I use `...false` to run the script only on the first boot of the new image. – eirc Jun 09 '16 at 12:26
  • When I added `true` to base instance (I use to make image), UserData worked. I am still confused why it did not work when I checked appropriate check box. Nevertheless, problem solved. Thanks. – zdenko.s Jun 09 '16 at 14:57
2

I had the same problem. Here's what solved it

  1. Launch an instance from your image if you haven't already
  2. RDP into it
  3. Open powershell, and run this C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule
  4. If you had a custom password for the instance, it will have been reset in the previous step. If you want it back, open powershell and run the following (assuming your username is Administrator net user Administrator "your_new_password" (if you didn't have a custom password, and were just using one generated by a .pem file, then you can ignore this step)
  5. Now go into the AWS console (i.e in the browser) and create an AMI from the instance
  6. Ensure your user data is of following form (the persist/true part is essential).
<powershell>
# Some powershell code here
</powershell>
<persist>true</persist>

That's it. Your userdata powershell script will now run next time your instance is launched.

stevec
  • 41,291
  • 27
  • 223
  • 311