> For the complete documentation index, see [llms.txt](https://developer.harness.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.harness.io/cloud-cost-management/resources/autostopping-guides/spot-orchestration-for-aws-ec2.md).

# Spot Orchestration and fallback to on-demand for AWS EC2

Spot orchestration and fallback to on-demand for AWS EC2 in AutoStopping

AutoStopping Rules ensure that non-production resources are utilized only when needed, preventing unnecessary costs associated with idle resources. These rules also allow for the seamless orchestration of workloads on spot instances, mitigating the risk of spot interruptions by automatically recovering from disruptions.

## How spot orchestration works

Spot instances are an efficient and cost-effective solution for non-production workloads. AutoStopping Rules enable seamless orchestration of spot instances as follows.

### Periodic snapshots for spot instances

1. **Snapshot frequency** - Spot instances are monitored by taking periodic snapshots every two minutes. Additionally, a snapshot is taken right before the instance is shut down.
2. **Snapshot retention** - Only the last three successful snapshots are retained, and older snapshots are automatically deleted.
3. **Handling interruptions** - If a spot interruption occurs or the instance becomes idle, the most recent successful snapshot is used to launch a new spot instance. If there is insufficient spot capacity available, the system falls back to an on-demand instance.

### Instance termination and data preservation

1. **Termination of idle spot instances** - When a spot instance becomes idle, it is terminated after taking a successful snapshot. This saves costs while preserving data integrity.
2. **Snapshot incrementality** - AWS snapshots are incremental, so after the first snapshot, subsequent snapshots are created quickly, reducing the impact on performance.
3. **Recreation of the spot instance** - When a new spot instance is created, the root EBS volume is restored from the most recent successful snapshot, and any additional EBS volumes are reattached to the new instance.
4. **No data loss** - All network interfaces and metadata are preserved, so there is no data loss. Even though the instance is recreated, it appears as the same instance to the user.

## Use case: efficient cost management with spot instances

### Scenario

Consider an organization running multiple non-production workloads on AWS. These workloads do not need to run continuously, and running them while idle incurs unnecessary costs. The organization uses AutoStopping Rules for efficient resource management.

* **Cost savings** - EC2 spot instances and Auto Scaling Groups run only when needed. An idle workload stops automatically, saving on costs.
* **Seamless recovery from spot interruptions** - The orchestrator automatically recovers from spot interruptions by using snapshots to recreate the instance with no data loss.
* **No impact on user experience** - A new spot instance maintains all the same configurations, so end users experience no disruption.

### Benefits

* **Cost efficiency** - AutoStopping Rules reduce unnecessary costs by terminating idle resources and utilizing cost-effective spot instances.
* **Data integrity** - Snapshots ensure that there is no data loss, even during spot instance terminations or interruptions.
* **Seamless operations** - The orchestrator handles the creation and termination of instances automatically, making the entire process transparent to users.
* **Simplified management** - AutoStopping Rules let organizations manage their workloads automatically, reducing manual intervention and administrative overhead.

## Conclusion

AutoStopping Rules provide a powerful mechanism to optimize resource usage and reduce costs, especially for non-production environments. By utilizing spot instances and ensuring seamless orchestration, organizations can benefit from significant cost savings while maintaining the integrity of their workloads. The implementation of AutoStopping Rules, combined with spot orchestration, creates a more efficient, automated, and cost-effective infrastructure.

Go to [AutoStopping Rules](/cloud-cost-management/cost-optimization/autostopping-rules.md) for setup steps and best practices.

{% @harness-feedback/feedback %}
