Introduction
Amazon Web Services (AWS) provides two powerful tools for managing fleets of instances in the cloud: Amazon EC2 Spot Fleet and EC2 (Elastic Compute Cloud) Fleet. These services offer cost-effective options for launching instances across multiple Availability Zones, utilizing various instance purchasing options like On-Demand Instances, Reserved Instances, and Spot Instances.

In this article, we'll delve into the key features and use cases of both Spot Fleet and EC2 Fleet, as well as explore some best practices for their configuration.
Amazon EC2 Spot Fleet

A Spot Fleet is a collection of Spot Instances and optionally On-Demand Instances, launched based on user-defined criteria. Spot Fleet automatically selects Spot capacity pools that match your specified requirements and launches Spot Instances to achieve the target capacity set for the fleet. By default, Spot Fleets maintain the target capacity by launching replacement instances when Spot Instances are terminated. However, users can also submit Spot Fleet as a one-time request, which doesn't persist after instances are terminated.
Spot Fleet Configuration Strategies
When creating a Spot Fleet request, users have two main configuration options. They can choose between two request types:
"Request" type: In this setting, Spot Fleet places an asynchronous one-time request for the desired capacity. If the capacity diminishes due to Spot interruptions, the fleet does not attempt to replenish Spot Instances or submit requests in alternative Spot capacity pools.
"Maintain" type: With this configuration, Spot Fleet places an asynchronous request for the desired capacity and maintains it by automatically replenishing any interrupted Spot Instances. Users can specify the request type during the Spot Fleet creation process in the Amazon EC2 console.
Working with Spot Fleets
Spot Fleets offer a flexible and cost-effective solution for managing instances, especially for fault-tolerant applications and workloads that can withstand pauses. Users can leverage Spot Fleets for various use cases, such as batch processing, data analysis, CI/CD workloads, and more.
CloudWatch Metrics for Spot Fleet
AWS provides CloudWatch metrics to monitor the performance of Spot Fleets, enabling users to gain insights into fleet capacity, instance health, and termination rates. Spot Fleets are collections of Spot Instances, which are spare EC2 instances available at significantly reduced prices compared to On-Demand instances. These metrics play a vital role in optimizing and fine-tuning the fleet's configuration. For example- Suppose you have a web application hosted on a Spot Fleet that consists of 50 Spot Instances. To monitor the fleet's performance, you use CloudWatch metrics. After a certain period, you observe the following metrics:
Fleet Request Capacity: 50, Fulfilled Capacity: 45, Pending Capacity: 2, Terminating Capacity: 3, Target Capacity: 50, Instance Health: Healthy: 42, Unhealthy: 3, Unknown: 5, Spot Fleet Request Duration: 120 seconds
Automatic Scaling for Spot Fleet
To further optimize resource utilization, we combine Spot Fleets with Auto Scaling groups to adjust the fleet's capacity based on demand fluctuations. When Spot Fleets are combined with Auto Scaling groups, the fleet's capacity can automatically adjust in response to fluctuations in demand. This is achieved by setting up scaling policies that define how the fleet's size should change based on CloudWatch metrics or custom alarms. For example- users can create scaling policies that increase the fleet's capacity when CPU utilization is high and decrease it when the CPU utilization is low.
Including On-Demand Instances
Spot Fleet requests can include On-Demand Instance requests offering a blend of Spot and On-Demand capacity to meet varying workload demands. By including On-Demand Instances in the Spot Fleet request, users can create a balanced fleet that combines the cost advantages of Spot Instances with the reliability of On-Demand Instances. This approach is especially useful for applications that require a certain level of guaranteed capacity and cannot solely rely on Spot Instances due to their potential for interruptions.
Spot Fleet Recommendations
Amazon suggests using Auto Scaling groups or EC2 Fleets instead of Spot Fleet for a fleet that includes Spot Instances, as the RequestSpotFleet API (on which Spot Fleet is based) is considered a legacy API with no planned investments.