I frequently fire up a temporary Ubuntu server on Amazon EC2 to test out some package feature, installation process, or other capability where I’m willing to pay a few pennies for a clean install and spare CPU.
I occasionally forget that I started an instance and leave it running for longer than I intended, turning my decision to spend ten cents into a cost of dollars. In one case, I ended up paying several hundred dollars for a super-sized instance I forgot I had running. Yes, ouch.
Because of this pain, I have a habit now of pre-scheduling the termination of my temporary instances immediately after creating them. I used to do this on the instance itself with a command like:
echo "sudo halt" | at now + 55 min
However, this only terminates the instance if its root disk is instance-store (S3 based AMI). I generally run EBS boot instances now, and a shutdown or halt only “stops” an EBS boot instance by default which leaves you paying for the EBS boot volume at, say, $1.50/month.
So, my common practice these days is to pre-schedule an instance termination call, generally from my local laptop, using a command like:
echo "ec2kill i-eb89bb81" | at now + 55 min
The at utility runs the commands on stdin with the exact same environment ($PATH, EC2 keys, current working directory, umask, etc.) as are in the current shell. There are a number of different ways to specify different times for the schedule and little documentation, but it will notify you when it plans to run the commands so you can check it.
After the command is run, at returns the output through an email. This gives you an indication of whether or not the terminate succeeded or if you will need to follow up manually.
Here’s an example email I got from the above at command:
Subject: Output from your job 114
Date: Mon, 20 Sep 2010 14:01:05 -0700 (PDT)
INSTANCE i-eb89bb81 running shutting-down
I already have a personal custom command which starts an instance, waits for it to move to running, waits for the ssh server to accept connections, then connects with ssh. I think I’ll add a --temporary option to do this termination scheduling for me as well.
You can get a list of the currently scheduled at jobs with
at -l
You can see the commands in a specific job id with:
at -c [JOBID]
If you decide along the way that the instance should not be temporary and you want to cancel the scheduled termination, you can delete a given at job with a command like:
at -d [JOBID]
I’ve been thinking of writing something simple that would regularly monitor my AWS/EC2 resources (instances, EBS volumes, EBS snapshots, AMIs, etc.) and alert me if it detects patterns that may indicate I am spending money where I may not have intended to.
How do you monitor and clean up temporary resources on Amazon AWS/EC2?



Follow Eric Hammond on Twitter
I did the same thing, in a custom AMI using
shutdown -h -P +300
in the boot script. This is a sales demo AMI for which the demo starts at boot and is only useful for a few hours.
justinpitts: That works fine for S3 based AMIs (instance-store instances) but for EBS boot, it will simply "stop" the instance instead of "terminate" it unless you also override this default behavior with an option like:
--instance-initiated-shutdown-behavior terminate
Oh absolutely.
And that's what I get for replying to the first paragraph. :)
I have been thinking about moving to EBS boot volume AMIs, and wondering how to handle this. I don't generally like the idea of scheduling the kill from my local machine; I'd still like the VM to 'own' its own lifecycle management.
I liked using shutdown because 'shutdown -c' is such an easy way to cancel it.
In the end, I will likely try to embed a ec2-api based killswitch in the AMI.
I also experience the fear of leaving resources up and running. At one point I had rigged up this thing: http://bit.ly/p0mnD to show me how much money I was spending at any given moment. The color updated about every half-hour or so. It was nice and unintrusive. I never left anything running unintentionally, and I never had to have my instances get killed automatically or deal with any kind of incoming alert.
I would like a smartphone app that does something similar, but I haven't really looked into whether one exists.
Good question and nice tip.
After getting stung recently with a large ec2 bill (my fault, not theirs), I implemented a simple awskill.sh script that I run from cron nightly on a non-cloud server to terminate all running instances. I'm getting tempted by a 3 year micro reserved instance so will likely use ec2 metadata to tag instances that should not be terminated.
http://gist.github.com/615128
I'm also curious what others are doing in this regard.
BTW, I've been a long time admirer of this blog - thanks for sharing so many useful insights.
Craig: Here are some more tips on protecting your instance from getting terminated accidentally:
http://alestic.com/2010/01/ec2-instance-locking
Can you tag instances with, say, "temp=true" and have a recurring job that checks if these exist (and, if it is fancy, lets them only live for an hour or two)?
Sorry if this is a dumb suggestion, I'm relatively new to AWS and have never used the tags.
sophware:
I like your idea about using tags to indicate which instances are temporary and automatically terminating them.
This could be extended to indicate an arbitrary time after which point some action should be taken including: terminate instance; send you an alert about the instance still running; email/text you a warning and then terminate an hour later.
You could combine this with the ability to modify the tag to extend the lifetime a bit longer if you're not quite done with using it.
As of the last couple of days, a new option has appeared in the EC2 launch instance wizard to specify shutdown behaviour: 'stop' or 'terminate'. Be careful - it defaults to 'terminate' which I find an odd choice given that instances used to go into 'stop'.
So now you can specify that an instance should terminate on shutdown and use the "sudo halt" method to effectively auto-terminate your instance. I prefer this method as it requires less configuration (no need to supply certs and PK files to ec2kill):
echo "sudo halt" | at now + 55 min
Cheers,
Chris
BTW - thanks for this post, very useful!