3 simple ways of saving up to 90% of EC2 costs
An EC2 instance is billed at an hourly rate depending on the instance type and the region by default. This pricing model is called on-demand. It is the simplest but also most expensive way of using EC2 instances. There are two other purchasing options for EC2 instances: reserved instances and spot instances. Reserved instances offer a discount on the hourly rate if you commit yourself paying for an instance for 1 or 3 years. Spot instances allow you to bid for unused capacity and save up to 90% of EC2 costs. Read on to learn about spot instances and explore three different ways to use them.
Spot Instances
Infrastructure providers like AWS are running at large scale. This also implies spare capacity at scale. Infrastructure providers try to avoid spare capacity and usage fluctuations. This is why electric power providers offer cheaper rates during the nights and why AWS offers the ability to bid on unused computing capacity.
A spot market is a market where standardized products are traded for immediate delivery. The price of the products on the market depends on supply and demand. On the AWS spot market, the traded products are EC2 instances, and they’re delivered by starting an EC2 instance.
The following figure shows the price chart for an EC2 instance. If the current spot price is lower than your maximum price, your spot request will be fulfilled, and an EC2 instance will start. If the current spot price exceeds your bid, your virtual machine will be terminated (not stopped) automatically after two minutes.
There are many different spot markets available. A spot market is defined by:
- Instance type (e.g. m3.medium)
- Region (e.g. eu-west-1)
- Availability Zone (e.g. eu-west-1a)
Each spot market is offering a separate current price. So when using spot instances it is an advantage to be able to use different instance types, in different availability zones or even regions, as this allows you to pick the lowest price available.
1. Defined-Duration Workloads
A spot instance is terminated automatically if the spot price exceeds your bid. This may sound scary at first. If you need EC2 instances for defined-duration workloads less than 6 hours there is a simple solution for that.
It is possible to bid on a spot block. A spot block defines a duration between 1 and 6 hours. An EC2 instances launched by a spot block will run continuously without interruption until it reaches the end of the defined time block. Pricing depends on the requested spot block: typically 30% to 45% less than on-demand pricing. So you are paying an extra charge on the spot price for a continuously running EC2 instance.
Creating a spot block request is simple. Executing the following command with the help of the AWS CLI will request a spot block for 1 instance running 1 hours with a maximum price of USD 0.15 per hour.
$ aws ec2 request-spot-instances \
--block-duration-minutes 60 \
--instance-count 1 \
--spot-price "0.15" ...
Whenever you are using EC2 instances for tasks running less than 6 hours using spot blocks is a very easy and reliable way of saving EC2 costs.
2. Spot Fleet
Managing and optimizing spot requests was a time consuming task. Fortunately AWS launched the Spot Fleet API in May 2015. Creating a spot fleet helps you to manage a fleet of one to thousands of EC2 instances running as spot instances.
Spot Fleet helps you:
- Compensating price fluctuations in the different spot markets by creating a diversified fleet of multiple instance types running in multiple availability zones.
- Scaling the number of EC2 instances with a simple API call.
- Replacing EC2 instances that have been terminated because the spot price exceeded the maximum bid.
AWS CloudFormation supports Spot Fleets. The following code snippet shows a typical example. The described Spot Fleet tries to launch two m3.medium or one m4.large instance in two different availability zones depending which option would be cheaper at the current spot prices:
{
"Resources": {
"IamRole": {
"Type": "AWS::IAM::Role",
"Properties": {
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [{"Effect": "Allow", "Principal": {"Service": ["spotfleet.amazonaws.com"]}, "Action": "sts:AssumeRole"}]
},
"ManagedPolicyArns": ["arn:aws:iam::aws:policy/service-role/AmazonEC2SpotFleetRole"],
"Path": "/"
}
},
"SpotFleet": {
"Type": "AWS::EC2::SpotFleet",
"Properties": {
"SpotFleetRequestConfigData": {
"IamFleetRole": {"Fn::GetAtt": ["IamRole", "Arn"]},
"SpotPrice": "0.30",
"TargetCapacity": "2",
"LaunchSpecifications": [
{
"InstanceType": "m3.medium",
"ImageId": "ami-bff32ccc",
"SubnetId": "subnet-105b3d49",
"WeightedCapacity": "1"
},
{
"InstanceType": "m4.large",
"ImageId": "ami-bff32ccc",
"SubnetId": "subnet-6ceacd09",
"WeightedCapacity": "2"
}
]
}
}
}
}
}
Spot Fleet is a powerful tool if you want to run 1 to 1.000 EC2 instances at the lowest possible price. It can be used to launch worker clients for a Hadoop cluster for example.
3. Auto Scaling with Spot Instances
It is also possible to use spot instances for typical web applications running behind a load balancer. Especially if you are using an Elastic Load Balancer (ELB), an Auto Scaling Group, and the concept of stateless servers.
In this case an EC2 instance is replaceable at any time without interrupting your users. This allows you to use spot instances to run your web application as well. If a spot instance is terminated because the spot price exceeded your bid your system will still be able to answer incoming requests without interruption.
If you want to make use of spot instances it is a best practice to use at least two Auto Scaling Groups:
- On-Demand instances (eventually partly covered by reserved instances).
- Spot Instances
When using AWS CloudFormation to define an Auto Scaling Group and its Launch Configuration it is simple to launch EC2 instances as spot instances. Just a single line containing the spot price needs to be added.
{
"Type" : "AWS::AutoScaling::LaunchConfiguration",
"Properties" : {
"ImageId" : "ami-bff32ccc",
"InstanceType" : "m3.medium",
"SpotPrice" : "0.15"
...
}
}
Perfect choice if you want to cut the expenses for your web application distributed on multiple EC2 instances.
Summary
AWS is aiming to reduce the spare capacity in their data centers. You can profit from saving up to 90% of EC2 costs when using Spot Instances instead of On-Demand Instances. There are powerful tools available helping you to use Spot Instances in different use cases: defined-duration workloads, server fleets and instances managed by Auto Scaling.
Further reading
- Article Introducing the Object Store: S3
- Article Improve AWS security: protect your keys with ease
- Article Building blocks for highly available systems
- Article Antivirus for S3 Buckets
- Tag ec2
- Tag costs