HeavyNounours
HeavyNounours

Reputation: 418

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 ?

Upvotes: 3

Views: 1516

Answers (3)

Sarat Chandra
Sarat Chandra

Reputation: 6120

Simple..

Just Create a c4.2xlarge Instance with all the Configuration Defined.

While you launch the instance, Create a “user-data script” in the advanced tab, So that it runs the script after launch of the instance and issues a terminate command to itself after the job is done..

To call the launching script, you can set a cron job either locally or use aws lambda.

Upvotes: 0

Sergey Kovalev
Sergey Kovalev

Reputation: 9411

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.

Upvotes: 2

jarmod
jarmod

Reputation: 78663

Not sure what you intend to do in your cron scripts, but look at AWS Lambda scheduled events.

Note that Lambda does not natively support PHP, but there appear to be options. Personally, I'd consider rewriting it in Python or JavaScript if possible.

Upvotes: 2

Related Questions