Keeping user's filesystem persistent across AWS EC2 spot instances

I noticed that, after a new Amazon AWS EC2 spot instance is auto-launched (I have that option turned on), the state of filesystem is not current (represent the last state before previous spot instance is auto-shutdown due to current price going over my set limit). I guess the initial state of newly launched spot instance's filesystem is based on the AMI that I use to auto-launch the spot instance. I have the following questions:

Is it possible, and, if yes, what can be done, to automate saving the state of filesystem before the auto-shutdown and restoring it during the auto-launch of a new spot instance? If it's not possible, at least, is it possible to automate downloading and running shell scripts that would bring the system to a desired state (auto-update the system via apt-get, get the latest source code via git, etc.)?

Thank you!


Solution 1:

You can launch your spot instance with a persistent EBS root volume (or attach a separate EBS volume post-boot). The EBS volume will remain after your spot instance terminates.

See http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/concepts-spot-instances-applications-ebs.html

Solution 2:

Spot instances (and really all of EC2) are optimized for use cases where you don't need to maintain state. When you configured your spot instance request, you provided an AMI id which each instance uses to boot. As you've found, changes do not get committed back to that AMI.

If you truly need a shared filesystem, configure a long-running (non-spot-instance) server in EC2 and export an NFS filesystem from there to your spot instances.

If all you're really needing to do, though, is run a few commands on boot to get things updated and to deploy your code, that can be done through the user data text that you can configure in your spot instance request. Just write a shell script to do what you want, then supply that as your user data in your spot request (or any other EC2 instance launch configuration). That script will get executed as root when the instance boots.