Automatic Termination of Temporary Instances on Amazon EC2

| 10 Comments

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?

10 Comments

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!

Leave a comment

Ubuntu AMIs

Ubuntu AMIs for EC2:


More Entries

Replacing a CloudFront Distribution to "Invalidate" All Objects
I was chatting with Kevin Boyd (aka Beryllium) on the ##aws Freenode IRC channel about the challenge of invalidating a…
Email Alerts for AWS Billing Alarms
using CloudWatch and SNS to send yourself email messages when AWS costs accrue past limits you define The Amazon documentation…
Cost of Transitioning S3 Objects to Glacier
how I was surprised by a large AWS charge and how to calculate the break-even point Glacier Archival of S3…
Running Ubuntu on Amazon EC2 in Sydney, Australia
Amazon has announced a new AWS region in Sydney, Australia with the name ap-southeast-2. The official Ubuntu AMI lookup pages…
Save Money by Giving Away Unused Heavy Utilization Reserved Instances
You may be able to save on future EC2 expenses by selling an unused Reserved Instance for less than its…
Installing AWS Command Line Tools from Amazon Downloads
When you need an AWS command line toolset not provided by Ubuntu packages, you can download the tools directly from…
Convert Running EC2 Instance to EBS-Optimized Instance with Provisioned IOPS EBS Volumes
Amazon just announced two related features for getting super-fast, consistent performance with EBS volumes: (1) Provisioned IOPS EBS volumes, and…
Which EC2 Availability Zone is Affected by an Outage?
Did you know that Amazon includes status messages about the health of availability zones in the output of the ec2-describe-availability-zones…
Installing AWS Command Line Tools Using Ubuntu Packages
Here are the steps for installing the AWS command line tools that are currently available as Ubuntu packages. These include:…
Ubuntu Developer Summit, May 2012 (Oakland)
I will be attending the Ubuntu Developer Summit (UDS) next week in Oakland, CA.  This event brings people from around…
Uploading Known ssh Host Key in EC2 user-data Script
The ssh protocol uses two different keys to keep you secure: The user ssh key is the one we normally…
Seeding Torrents with Amazon S3 and s3cmd on Ubuntu
Amazon Web Services is such a huge, complex service with so many products and features that sometimes very simple but…
CloudCamp
There are a number of CloudCamp events coming up in cities around the world. These are free events, organized around…
Use the Same Architecture (64-bit) on All EC2 Instance Types
A few hours ago, Amazon AWS announced that all EC2 instance types can now run 64-bit AMIs. Though t1.micro, m1.small,…
ec2-consistent-snapshot on GitHub and v0.43 Released
The source for ec2-conssitent-snapshot has historically been available here: ec2-consistent-snapshot on Launchpad.net using Bazaar For your convenience, it is now…
You Should Use EBS Boot Instances on Amazon EC2
EBS boot vs. instance-store If you are just getting started with Amazon EC2, then use EBS boot instances and stop…
Retrieve Public ssh Key From EC2
A serverfault poster had a problem that I thought was a cool challenge. I had so much fun coming up…
Running EC2 Instances on a Recurring Schedule with Auto Scaling
Do you want to run short jobs on Amazon EC2 on a recurring schedule, but don’t want to pay for…
AWS Virtual MFA and the Google Authenticator for Android
Amazon just announced that the AWS MFA (multi-factor authentication) now supports virtual or software MFA devices in addition to the…
Updated EBS boot AMIs for Ubuntu 8.04 Hardy on Amazon EC2 (2011-10-06)
Canonical has released updated instance-store AMIs for Ubuntu 8.04 LTS Hardy on Amazon EC2. Read Ben Howard’s announcement on the…