In this blog, I will be going over how to set up and run a Spot Fleet on AWS and initialize and run a code deployment to the Spot Fleet

What are spot fleets and how do they work?
A spot fleet utilizes EC2 instances that are currently not being used by on-demand users and are available for less than the on-demand price. As a result, using a spot fleet is to your advantage as you can make significant savings on your EC2 usage. In some cases using a spot instance can save you as much as 90% off the on-demand price. AWS users initially request a Spot fleet by “bidding” on a maximum price that they would like to use a spot instance for. Below are savings for a spot fleet I created.

A spot fleet will become active automatically, if the maximum price you requested for the spot fleet exceeds the current spot price. As the spot price at any given time is dictated by the number of spot instances available, those who made the highest “bids” will get their requests serviced first. While utilizing spot fleets are cost effective, they do come with drawbacks.

1. Spot fleet requests will not be fulfilled until an EC2 instance is available (all EC2 instances are busy fulfilling on-demand requests) or if the price for a spot request currently exceeds the maximum price you requested
2. AWS will hibernate the spot fleet operation if the price of the spot request exceeds the price you initially set as your highest bid, or all EC2 instances become busy fulfilling on-demand requests. This is why you will see the spot requests yo-yoing in and out of use

Because of these drawbacks, spot requests are  better for tasks where you will not need instances to always be live. Instead they are better for the likes of batch jobs, data analysis and large scale testing. If your use case is to host a website that needs to continuously be online to handle user traffic, you will need to have at least one on-demand EC2 instance. In my own use case we are using a combination of Spot Fleets and on-demand EC2 instances for our HTTP servers, TCP servers  and API servers with all using AutoScaling.

You can read more about spot requests here:

Now, we let’s launch our spot fleet!

Step 0: Launch your Application and its Revision
Prerequisites for launching the spot request is that you must have launched an application on AWS CodeDeploy with at least one successful revision beforehand. Once you’ve created your application, create a deployment group (use the same deployment group name in the subsequent steps) and upload a revision to Amazon S3.

To read more about AWS CodeDeploy and AWS S3 see the following links:

Step 1: Create a Launch Template
Just like launching an on-demand EC2 instance, to launch a spot instance you should first create a launch template. This launch template should contain configuration information such as AMI ID, instance type, and network settings.

You can find the Launch Template under the instances tab, in the EC2 AWS console. After selecting the launch template tab click the “Create Launch Template” button.

You are then presented with options on the settings for the launch template (next photo). Select the AMI ID,  instance type, the name of your EC2 key pair .pem file, and the security group. In this case I went for the AMI which was Amazon Linux 2 AMI and the t2.small instance type.
Add a key-value pair as well under the tags (use this same key-value in the commands).

In the User Data section write the following commands:

sudo apt install -y awscli

aws deploy create-deployment-group –application-name <your-application-name> –deployment-group-name <your-deployment-name> –deployment-config-name <your-deployment-config-name> –ec2-tag-filters Key=<key_name>,Value=<your_value>,Type=KEY_AND_VALUE –service-role-arn <service-role-of-user> –region <your-region>

aws deploy create-deployment –application-name <your-application-name> –deployment-group-name <your-deployment-name> –deployment-config-name <your-deployment-config-name> –region<your-region> –update-outdated-instances-only

This will create your deployment group and a deployment to your code.

Step 2: Launching the Spot Request
To launch the spot request  we need to first access the spot requests tab. Then, click the Request Spot Instances button, as shown in the following photos.

To configure the instance, load the same launch template that you created in Step 1 (remember to match the version number as well) . Configure the instance type you want as your minimum compute unit, in the launch template’s settings. Check the maintain target capacity box under the “Tell us how much capacity you need,” section. You can either use the AWS fleet recommendations or set the instance types in the fleet to your liking, like I did. Unselect the apply defaults checkbox and enter your Target Group.

After you have done all of this, click the launch  button on the bottom of the page.

Step 3: AutoScaling
Now that we’ve initialized the spot request, we can create the settings to enable AutoScaling for it. So, that we can increase or decrease the load to a single instance. Go to the spot request tab, and then select the request that you launched in Step 2. Underneath the list of the spot requests, are the settings for the selected spot request. Click the AutoScaling tab and then add policy link.

Within the policy click ‘Create new alarm’ link next to the policy trigger section. Click the  ‘Create new alarm,’ and search for the metric ‘CPUUtilization.’  Under alarm details name it ‘ScaleUp,’ set the CPUUtilization to ‘>= 10’ and then click ‘Create Alarm.’ Change the modify capacity setting under the Spot Instances’ details, and set the instance addition rate to 1.

Repeat the process for the ‘ScaleDown’ policy. This time however you will name the policy ‘ScaleDown’, set the CPUUtilization policy to ‘< 5’ and set instead of add this time in the modify capacity section.

Congratulations, you’ve now launched your first ever spot fleet!