Remote orderly shutdown of EC2 instance?

I'm writing a script which shuts down EC2 instances remotely using ec2-terminate-instances. I would like to perform orderly shutdowns (similar to the ones triggered by Linux 'shutdown' command) instead of simply turning the power off. This would give me time to shut down running services and properly unmount an EC2 volume.

Unfortunately, I can't log into these instances using SSH without doing a lot of extra work. Is there an easy way to trigger a remote, orderly shutdown? Or will I need to write some sort of control daemon that lives on the server, and which I can poke when I want the server to shut down?


Solution 1:

EC2 instances that have been either stopped or terminated using the API will be sent appropriate commands that can be intercepted by power daemons and used to initiate a shutdown. Most AMI's and default operating systems already know what to do when the get these signals. It is the same on desktop computers where the power button sends a special message requesting a shut down. The API terminate command does not such "pull the plug" on the instance.

Solution 2:

Take a look at ec2-stop-instances! This does a shutdown -h now and sends the instance into stopped state, but does not terminate the instance. If you want to terminate the instance, use ec2-terminate-instances after this.