Meter’s running - How to avoid spending all your money—
Ever been in a cab, stuck in traffic watching the meter run. You watch each 50-cent increment nudge the fair up dollar by dollar, wondering if it would be quicker to walk.
Cloud hosting is charged the same way, but it’s very easy to take your eye off the meter, fire up dozens of services, store gigabytes of files, and end up spending a fortune.
I’ve left instances sitting around for days and even weeks. Between myself and our lead architect, we somehow forgot about an RDS instance for a few days, racking up charges for a machine that wasn’t even being used.
It doesn’t matter what platform you use: If it’s pay as you go, you’ll want to monitor it to prevent your $1,000-a-month bill turning into $10,000 a month.
In the tradition of programmers being lazy but resourceful, I’ve thrown together a little script using the Ruby cloud computing library fog that shows you which services are running and roughly how much they’re charging in the current hour.
The script simply connects to AWS, finds each of the main chargeable services (I’ve only done EC2, RDS, and S3 so far … feel free to contribute), and uses a table of costs to find the upper limit of costs. Because AWS tiers its price (the second million gigabytes of storage cost less than the first), the estimated costs will probably be higher than you are charged at the end of the month, but it’s better to be safe ….
Write the Script
The first few lines list the instances I use and their costs. Because I’m only working in a couple of regions, I’ve left the costs approximate.
instance_costs = { 't1.micro' => 0.02, 'm1.small' => 0.095, 'c1.medium' => 0.19, 'm2.2xlarge' => 1.14, 'm2.4xlarge' => 2.28 }
Then I list the regions. This could have been dynamic, but I don’t want to check in the [asia??] regions, so I’ve listed them.
regions = [ 'eu-west-1', 'us-west-1' ]
And finally, I need the account details. Because I run multiple accounts for dev, test, and production for multiple clients the account details are in a hash.
accounts = { 'myaccount' => { 'key_id' => 'YOUR-KEY-HERE', 'access_key' => 'YOUR-ACCESS-KEY-HERE' } }
The script uses 'fog' [URL], which means you could adapt it to work with GoGrid, Rackspace, or whichever cloud provider you use.
The rest of the script simply loops through the accounts, then through the regions, and shows you how much you’re spending in the current hour and what that might be at the end of the month. It’s easy to forget that $1 an hour is $744 dollars a month.
Run the Script
$ ruby cost-checker.rb For region 'eu-west-1' i-abc123ab....................: $ 0.19 i-def456de....................: $ 0.095 i-hij789hi....................: $ 0.095 Total for account : $ 0.48 Projected monthly for account : $353.40
Pretty useful. If an instance is stopped, it is shown because you won’t be charged for it.
i-aaa111aa....................: $--.-- [stopped]
EC2 isn’t by any means the only way to rack up charges. You need to consider storage (S3, EBS snapshots, and CloudFront), bandwidth, and any other services you’re using. This script should help you keep an eye on the meter.
The script is available on github at https://github.com/danfrost/Cloud-costs. Just clone it and run … then realize how much you should be saving.