0

I have an EC2 instance that uses @reboot to run a python script every time the instance starts up. The python script uses conn.stop_instances(instance_ids=[my_id]) to stop the instance after the script has finished (more details here). Unfortunately, I can no longer ssh into my instance because the python script stops the instance immediately. Is there anything I can do to reset the instance or change the settings manually?

If not, is there any way to grab files from an instance without having to ssh in?

Kylie
  • 95
  • 7

1 Answers1

1

Create a shell script that deletes your reboot script.

#! /bin/bash
rm -f /path/to/my/python_script.py

Add this script as User Data to your EC2 instance.

Reboot the instance. The script will run deleting your python reboot script.

Notice the -f flag. This means force, which will handle files set to read-only.

Go back and remove this script from User Data once you can control / access your instance.

Running Commands on Your Linux Instance at Launch

John Hanley
  • 74,467
  • 6
  • 95
  • 159
  • Hi, @John Hanley I tried this but I'm not sure if the script ever runs because the instance immediately goes from 'pending' to 'stopping'. Maybe `@reboot` has priority over `User Data` scripts? – Kylie Jan 14 '19 at 05:41
  • More details on how you configured and how you are running your `@reboot` script. I have written other SO answers on how to detach the boot volume, modify it and then reattach as another alternative. – John Hanley Jan 14 '19 at 05:53
  • the command I ran was `@reboot python script_name.py`. The script contains `conn.stop_instances(instance_ids=[my_id])` which then shuts the instance down before I can ever delete the file. Is there a way to changing the reboot settings without starting the instance? – Kylie Jan 14 '19 at 06:15
  • From your comment I do not understand how you configured your script to run or what your script contains. Do not leave out details. It is often the little items that point to the real problem / solution. – John Hanley Jan 14 '19 at 06:17