0

Amazon bills you for the full hour of EC2 usage even if you have only run the instance for 10 minutes. So before terminating any unused EC2 instances I want to check if the launch time - current time is under 10 minutes.

Let me explain this to you by an example:

Let's say I launch my EC2 instance at: 2016-Feb-01 13:02:22. Then I would like to terminate it only when the time is 10 minutes from the launch time (regardless of the date). So if it is 16:58:54 then it means that current time is a good time to terminate it because I will be billed at 17:02:22 again (in about 4 minutes as per this example)? On the other hand if the time is 13:03:01 then it is not a good time since I'm already billed for the hour.

Here is what I could come up with, but it's not working when the current time is 58 and launch time is 02, etc

<?php

$launch = '2016-03-03 13:02:22';
$now = '2016-03-04 12:58:22';

$epochLaunch = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($launch))));
$epochNow = strtotime(sprintf('1970-01-01 00:%02d:00', date('i', strtotime($now))));

echo $epochLaunch - $epochNow;
supersan
  • 5,671
  • 3
  • 45
  • 64
  • Why do you think if you use an instance for under 10 minutes you won't be billed for the full hour? I can't find anything about that in the docs – TommyBs Mar 04 '16 at 08:37
  • See this: http://stackoverflow.com/questions/26371892/how-exactly-does-aws-ec2-count-hourly-costs – supersan Mar 04 '16 at 08:37
  • For under 10 mins you will still be billed for a full hour. Also are your example times still wrong? You jump from 16:58 to 16:02 back to 13:03 – TommyBs Mar 04 '16 at 08:39
  • Yes, you are right. I've fixed the example times. Also, that is exactly my question. I want to terminate my instance only when I have like 10 minutes remaining before it is re-billed (since I'm already billed for the whole hour). I've setup a cron job that run every 5 minutes to check which instances are idle and are going to be billed in the next 10 minutes – supersan Mar 04 '16 at 08:42
  • Ah I see sorry, I was reading your question wrong seeing it as you wanted to terminate an instance if it was under 10 minutes in its launch time, not 10 minutes before an hour (e.g it's been launched for 50 mins). This question might be of use http://stackoverflow.com/questions/29012927/php-comparing-date-checking-if-within-last-hour – TommyBs Mar 04 '16 at 08:44

1 Answers1

1

What you need is something like this:

function shouldTerminate($launch, $minsToTerminate, $current = null)
{
    if (!is_null($current)) {
        $current = strtotime($current);
    }
    else {
        $current = time();
    }
    $launchEpoch = strtotime($launch);  

    return (($current - $launchEpoch) % 3600) > (3600 - $minsToTerminate * 60);
}

echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-03 13:50:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-03 13:59:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:58:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:03:25') ? 'Terminate': 'Do not terminate', PHP_EOL;
echo shouldTerminate('2016-03-03 13:02:22', 4, '2016-03-05 13:51:25') ? 'Terminate': 'Do not terminate', PHP_EOL;

The output of the above will be:

Do not terminate
Terminate
Terminate
Do not terminate
Do not terminate

USAGE: The shouldTerminate() function is straightforward to use, supply it with the string of launch time, the minutes before the whole hour to allow for termination and an optional datetime string parameter to test against - or the function will use the current time if you do not supply any.

See the code live at https://eval.in/530108

EXPLANATION: The idea is that you are interested in the remainder of minutes in a whole hour. That is why you get the remainder of seconds in an hour from the difference between the two epoch time, i.e.

$current - $launchEPoch % 3600

The above will give you how many seconds that do not fit in a whole hour (remaining seconds) they have passed and when those seconds are greater than the remaining hour minus the threshold you have set, i.e. > 3600 - $minsToTerminate * 60 then you can terminate the instance according to your rule.

Ioannis Lalopoulos
  • 1,491
  • 1
  • 12
  • 20
  • Thanks, this is exactly what I was looking for! The code is also very neat and clear to understand! – supersan Mar 04 '16 at 09:22