3

A user data script will run the first time an EC2 is started.

How can I restore/reactivate this ability on a windows EC2?

Note

I have tried the script suggested here but it fails immediately as there is no file C:\Program Files\Amazon\Ec2ConfigService\Settings\Config.xml and nothing similarly named (not that I found; not even an Ec2ConfigService directory)

Also note, my question is identical to this question but for windows ec2, not linux

stevec
  • 41,291
  • 27
  • 223
  • 311
  • https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html#user-data-execution – jefftrotman Jul 12 '19 at 01:46
  • Why do you want to run the same script after reboot? The userdata script is typically designed to bootstrap the instance, which is a one-time thing. All operating systems have their own specific way to run code after reboot, and you would normally use that mechanism (e.g. init.d on Linux or Task Scheduler on Windows). – jarmod Jul 12 '19 at 02:03
  • @jarmod I have scheduled a lambda to launch an EC2 from a custom AMI and run a script every 6 hours. I thought everything would work just fine until I realised the (powershell) userdata I provide at launch won’t run (and therefore the script I want to run every six hours wont be triggered by the powershell script). This is because the userdata only runs once per image. Since I’m using a custom image, I need to learn how to make that image accept and run user data when it is launched, just as though it had never been launched before – stevec Jul 12 '19 at 02:10
  • @jarmod I spent several unsuccessful hours trying to get task scheduler to run a powershell script. I tried everything SO had to offer. Ultimately task scheduler would run the task on start up but ***only*** after I had logged in through RDP. So the whole point of automation was lost. I spent many an unfruitful hour on SO trying to work around that one.. – stevec Jul 12 '19 at 02:18
  • Ok, so you’re just trying to work out how to create an AMI from an existing Windows instance where the AMI, when launched, will execute the userdata script. You’re not trying to re-run the userdata script after an instance restart. – jarmod Jul 12 '19 at 02:19
  • @jarmod correct. I guess to be technical I'm trying to edit the AMI I have already made. But if I have to I'll start again and make a new one. – stevec Jul 12 '19 at 02:20
  • Related question: https://stackoverflow.com/questions/26158411/amazon-ec2-custom-ami-not-running-bootstrap-user-data – jarmod Jul 12 '19 at 02:29
  • @jarmod wow good find. Is it your understanding that I should: 1. launch the AMI 2. Open cmd 3. run `C:\ProgramData\Amazon\EC2-Windows\Launch\Scripts\InitializeInstance.ps1 –Schedule` 4. Create a new image based on that state. Is there anything else to it? (I will try this now) – stevec Jul 12 '19 at 02:31
  • I would follow the docs. Report back if it doesn’t work and hopefully someone can jump in and help. – jarmod Jul 12 '19 at 02:32

2 Answers2

1

By default, the user data scripts are run one time when you launch the instance. To run the user data scripts every time you reboot or start the instance, add <persist>true</persist> to the user data.

<powershell>
$file = $env:SystemRoot + "\Temp\" + (Get-Date).ToString("MM-dd-yy-hh-mm")
New-Item $file -ItemType file
</powershell>
<persist>true</persist>

refer this for further information https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html

isuranger
  • 15
  • 3
0

I understand that the point is about just running user-data, and not all the other stuff ...

To run (only) user-data script, you can run it by:

Import-Module (Join-Path (Join-Path $env:ProgramData -ChildPath "Amazon\EC2-Windows\Launch") -ChildPath "Module\Ec2Launch.psd1")
Invoke-Userdata -OnlyExecute

let's say you save this as 'C:\ProgramData\Amazon\EC2-Windows\Launch\Config\run-user-data.ps1', then you can use PowerShell to schedule a new task to run at startup:

$Action = New-ScheduledTaskAction -Execute 'Powershell.exe' -Argument '-ExecutionPolicy Bypass C:\ProgramData\Amazon\EC2-Windows\Launch\Config\run-user-data.ps1'
$Trigger = New-ScheduledTaskTrigger -AtStartup
$Settings = New-ScheduledTaskSettingsSet
$Task = New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings
Register-ScheduledTask -TaskName 'Execute user-data' -InputObject $Task -User 'NT AUTHORITY\SYSTEM' -Force

I use this sort of solution by creating the mentioned file and command on 'AWS::CloudFormation::Init' sections.

Hope it helps!

Nacho Coll
  • 523
  • 9
  • 10