Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to automatically start, execute and stop EC2?

I want to test my Python library in GPU machine once a day. I decided to use AWS EC2 for testing. However, the fee of gpu machine is very high, so I want to stop the instance after the test ends.

Thus, I want to do the followings once a day automatically

  1. Start EC2 instance (which is setup manually)
  2. Execute command (test -> push logs to S3)
  3. Stop EC2 (not remove)

How to do this?

like image 671
Yohei Avatar asked Sep 15 '25 00:09

Yohei


2 Answers

It is very simple...

Run script on startup

To run a script automatically when the instance starts (every time it starts, not just the first time), put your script in this directory:

/var/lib/cloud/scripts/per-boot/

Stop instance when test has finished

Simply issue a shutdown command to the operating system at the end of your script:

sudo shutdown now -h
like image 110
John Rotenstein Avatar answered Sep 16 '25 14:09

John Rotenstein


You can push script logs to custom coudwatch namespaces. Like when the process ends publish a state to cloudwatch. In cloudwatch create alarms based on the state of process, so if it has a completed state trigger an AWS lambda function that will stop instance after completion of your job.

Also if you want to start and stop on specific time you can use ec2 instance scheduler to start/stop instances. It just works like a cron job at specific intervals.

like image 26
matesio Avatar answered Sep 16 '25 13:09

matesio