michelangelus - Fotolia
Avoid data loss during AWS Spot Instance termination
While Spot Instances can be economical, they can also cause data loss without proper backup. Use databases to save instance data, and set up interruption notices to be warned.
AWS provides spare compute capacity with its EC2 Spot Instances. Whether you handle high-performance computing workloads, run a stateless web server or transcode video, these instance types could be the most economical way to use compute power.
But, while Spot Instances cost up to 90% less than their On-Demand counterparts, they only run as long as your bid price is above the current Spot Instance price. It's important to prepare for potential AWS Spot Instance termination, especially when you have data on the instances that you don't want to lose. Take these measures to prevent data loss.
Hold on to your data
First, store your Spot Instance data in a database, such as Amazon Relational Database Service or Amazon DynamoDB -- or you can even use Amazon S3. When you store data, you prevent unnecessary losses in the event of AWS Spot Instance termination, and you can simply provision new ones and continue where you left off.
Alternatively, you could opt to preserve the entire disk. By default, AWS deletes all Amazon Elastic Block Store (EBS) volumes when Spot Instances are terminated. To keep your disk, uncheck Delete for EBS volumes when you create a Spot Instance request in AWS Management Console.
Beat the buzzer
AWS also provides Spot Instance interruption notices, which can help prevent data loss. When your bid price becomes lower than the current price, AWS' Spot Instance termination process begins, but it doesn't happen immediately. Instead, AWS sends you a two-minute warning, which gives you time to offload your data and complete other preparations, such as detaching your instances from a load balancer.
There are multiple ways to detect whether your instances are about to terminate. For example, the instance metadata updates with the exact time of the shutdown:
curl http://169.254.169.254/latest/meta-data/spot/termination-time
You can also use DescribeSpotInstanceRequests, rather than poll from the instance itself.
Additionally, AWS added Spot Instance interruption notices to Amazon CloudWatch Events in January 2018. This lets you prepare for AWS Spot Instance termination via Lambda or Simple Notification Service topics, which can help automate the data backup process. AWS has also upgraded Spot Instances with other features, like streamlined access to capacity, instance hibernation and an option to purchase without placing a bid, which help make them a worthwhile option for budget-conscious users.