A user is billed for an EC2 instance for each hour it is in a "running" state. Partial hours are rounded up to a full hour and billed as one.
As an example, if an instance is to be terminated 15 minutes into a billing-hour, it will instead install the Folding@Home client and spend the remainder of it's paid-for time performing protein folding and sending results back to Stanford University. This example can be extended to any number of computational grid computing problems, such as prime number discovery and so on.
Code: https://github.com/raids/dusk
Dusk is composed of a CLI tool as well as some short-lived infrastructure in AWS per termination.
The CLI tool is essentially a wrapper around the "ec2 terminate-instances" command, where it performs the following:
- Calculates the remaining time until the next instance-hour cost is incurred
- Creates a cron expression from this time (required for a CloudWatch Event Rule)
- Launches a Cloudformation stack containing the orchestration to terminate the instance at the scheduled time
- TODO: Interacts with SSM to tell the instance to perform a predefined action (via an SSM document), or create an SSM document on the fly