Introduction and example
EC2 Instances are booted either from the “Instance Store” (a process in which the AMI is copied from S3 onto an ephemeral boot device) or from EBS volumes (in which case the boot device is an EBS created from a snapshot of existing EBS volume serving as a template).
While the two types differ in many aspects, there is difference thing that really draws everyone attention: Instance Store Instances will Terminate upon shutdown (planned or unplanned) whereas EBS backed Instances have the ability to enter a Stopped state similarly to physical computers - when we turn it off it doesn’t disappear; it stays inactive until it is powered back on.
While the added persistency offered by EBS-backed volumes has many benefits - there are situations where the non-persistent nature of Instance Store Instances can be very convenient…
For example: in the past, I found myself provisioning individual all-in-a-box environments to various business partners for demo or development purposes.
In these scenarios I found it very convenient to instruct them to shut down the OS when done (or alternately rig the said Instance to shut itself down at a certain date) - thereby causing the Instance to Terminate; freeing any compute resources it was consuming and ending any charges it was incurring as such.
Note that this is not an airtight solution as we humans tend to forget Instances running and/or the Instance OS may fail to properly shut itself down but it is simple, readily available and best of all - does not require any kind of AWS account access or communication from the said Instance, completely containing the contents of the Instance for security purposes.
So how can we set EBS backed Instances to auto-Terminate?
EBS-backed Instances default to entering the Stopped state upon shutdown (after all, supporting this state is the primary reason why we EBS-backed Instances were conceived!), but that’s configurable using the AWS API and command line tools.
There are two methods of changing this behavior (read: attribute) of the instance, called “Instance Initiated Shutdown Behavior”.
Method I: Configure during the initial creation of the Instance
When we provision an EBS-backed Instances through the RunInstances we can supply the shutdown behavior argument and specify it to terminate upon shutdown like in the following example using the EC2 command line tools:
ec2-run-instances ami-12345678 –instance-initiated-shutdown-behavior terminate
Method II: Modifying an existing Instance
We can use the self-explanatory ModifyInstanceAttribute command on existing Instances
ec2-modify-instance-attribute i-12345678 –instance-initiated-shutdown-behavior terminate
Reverting back to Stop upon shutdown
Building on the previous method of modifying an existing Instance; we can also set the Instance to stop upon shutdown, thus reverting any previous setting to terminate.
Update March 24th, 2011:
AWS now supports changing the shutdown behavior of the Instance through the web console - just right-click on your Instance and select the “Change Shutdown Behavior” option.