Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically create EC2 instance, run command, and terminate

I seek a method to implement a cron-like system on AWS EC2 instance : every morning, I have some tasks to run, and they are using a lot of RAM (about 8 Go for each script). I don't want to pay a full-time c4.2xlarge instance, that's the point.

What I think about :

  1. At 00h, each day, create a c4.2xlarge instance
  2. When the system is running, run a PHP script
  3. When the PHP script ends, terminate the instance

How to automate these actions ?

like image 939
HeavyNounours Avatar asked Aug 24 '26 16:08

HeavyNounours


1 Answers

If you created an instance with --instance-initiated-shutdown-behavior terminate then it will terminate itself when stopped. All you need to do is run shutdown -h now at the end of your script when it's done.

So how do you start an instance at a specific time?

  1. There are third party services that do exactly that. Like GorillaStack.

OR

  1. Create a CloudWatch scheduled event. It's basically a cron in the cloud that can run a Lambda function, which in turn may start you instance.
like image 76
Sergey Kovalev Avatar answered Aug 27 '26 05:08

Sergey Kovalev



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!