0

I have an EC2 Instance with Windows, and I need to run a python3/selenium code automatically right after it starts.

I have tried USER-DATA but no success.

The objective is set a process to run automated tests to our portal automatically after the instance boot, without any manual interaction.

I have tried creating an USER-DATA script like:

<script>
python c:\script.py
</script>

or

<powershell>
python c:\script.py
</powershell>"

I expected that at the end of boot it will start the python script, but no success

  • What is in the log file? For recent Windows versions, look in `C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log`. For older Windows versions, look in `C:\Program Files\Amazon\Ec2ConfigService\Logs\Ec2Config.log`. Are you launching a new instance, or merely changing the User Data on an existing instance? See also: [Running Commands on Your Windows Instance at Launch](https://docs.aws.amazon.com/AWSEC2/latest/WindowsGuide/ec2-windows-user-data.html) – John Rotenstein Oct 03 '19 at 05:27
  • User data only executes once, unless you have a persist flag to tell EC2 to execute your script at every boot (stop/start). Check the logs mentioned above to see what tags are being picked up and I'd your code was actually ran. If your code ran without any output and nothing seemed to happen, you can add in a small test to make a temp file. If the file is created and your script didn't run, I'd revisit the syntax you are using – M2065 Oct 07 '19 at 04:10

1 Answers1

1

I don't know if you ever figured this out or not but if anyone finds this from google I was able to fix this issue by taking a look at the userdata logs inside of C:\ProgramData\Amazon\EC2-Windows\Launch\Log\UserdataExecution.log and I found out that it was running the script in windows powershell and for whatever reason my windows powershell wasn't recognizing python.

So instead of doing python <your script path> inside of the userdata

I did py <your script path> and that solved my issue.

Also this helped me as well https://stackoverflow.com/a/47889881/7789281 using sysprep

Drew Scatterday
  • 267
  • 1
  • 2
  • 14