There is no readily available AWS solution for this fine-grained optimization, but you can use the existing building blocks for your own creation based on the launch time of the current instance (see Dmitry Samskyโs solution for finding out How long did this EC2 instance run? ).
Game "Chicken"
Shlomo Svidler studied this optimization in his article Play Chicken with Spot Instances , albeit a slightly different motivation in the context of Amazon EC2 Spot Instances :
AWS Spot instances have an interesting economic feature that makes the game a little bit on the system. Like all EC2 instances, when you initiate the termination of Spot Instance, then you pay for the entire hour, even if you use less than an hour. But when AWS completes the instance because the spot price exceeds the purchase price, you do not pay for the current hour.
Of course, the mechanics are the same, so you can just reuse the script that he built, i.e. execute this script instead of or in addition to marking the instance as idle :
After completing the task, you can cancel the scheduled completion instead of or in addition to marking the instance with non_idle as follows:
sudo shutdown -c
This is also a red button emergency command during testing / operation, see, for example, Shlomo Warning:
Make sure you really understand what this script does before using this. If you mistakenly plan to close the instance, you can cancel it with this command, run on the instance: sudo shutdown -c
Adding CloudWatch to the game
You could take another Shlomo approach by integrating with Amazon CloudWatch , which recently added the Use Amazon CloudWatch option to detect and turn off unused Amazon EC2 instances , see the Amazon CloudWatch intro blog post - Signal actions for details:
Today we give you the opportunity to stop or end your EC2 when a CloudWatch alarm is triggered . You can use this as (detect an abnormal state and then act) or as part of your application processing logic (expect the expected condition and then act). [emphasis mine]
Your use case is listed in the Application Integration section:
You can also create CloudWatch alarms based on Custom Metrics , which you monitor an instance individually. You could, for example, measure calls to your own web services APIs, page requests or posting messages per minute and respond as desired.
Thus, you can use this new feature to publish custom metrics to CloudWatch to indicate whether the instance should end (there is an idle ) based on and Dmitriy detects the start time and reset the metric again when the task arrives, and the instance should continue to work (this is non_idle ) - for example, EC2 will take care of completion, 2 of 3 automation steps would be moved from the instance to the operating environment, and accordingly the management and visibility of the automation process would be improved.