Self-Terminating AWS EC2 Instance?

To have an instance terminate itself do both of these steps:

  1. Start the instance with --instance-initiated-shutdown-behavior terminate or the equivalent on the AWS console or API call.
  2. Run shutdown -h now as root. On Ubuntu, you could set this up to happen in 55 minutes using:

    echo "sudo halt" | at now + 55 minutes
    

I wrote an article a while back on other options to accomplish this same “terminate in an hour” goal:

Automatic Termination of Temporary Instances on Amazon EC2
http://alestic.com/2010/09/ec2-instance-termination

The article was originally written before instance-initiated-shutdown-behavior was available, but you’ll find updates and other gems in the comments.

Leave a Comment