AWS EC2 Instances purchasing options
Tags: aws, ec2, devops, cloud
Published: 2024-04-01 04:58:38

I know that understanding the different purchase options for AWS EC2 instances can be confusing at the beginning. In this blog i will explain you how these models works and help you understand how you can benefit from each of them. Optimizing your compute usage can significantly impact your billing. One of the most important decisions when defining the instances for your workload is to determine the lifecycle of an instance(short-period of time? long period?. There are two different ways to run your instances (**on-demand, spot**), even if you bought reserved instances those are running on-demand. Then we have the tenancy (shared or dedicated) that determines if your instances will be retrieved from a shared pool of VM's or from a dedicated host, instance(the dedicated tenancy will not be explained on this blog).

### OPTIONS AVAILABLE
so what are the options available?
- On-Demand: Pay per second, only for the time that the instance is used. Each instance has a fixed price per hour, so there's a record of the time used by the instances. Some need specific licenses like Windows OS, for that reason, there's an extra fee due to the use of those licenses. **This model is a good option for testing or short-term launches**.
- Saving Plans
- Reserved Instances
- Spot Instances
- Capacity Reservation
- Dedicated Hosts:Pay for a physical host that is fully dedicated to running your instances, and bring your existing per-socket, per-core, or per-VM software licenses to reduce costs.
- Dedicated Instances: Pay, by the hour, for instances that run on single-tenant hardware.

> i will not cover dedicated hosts and instances due that is a very specific topic and. don't see a high value on knowing them at detail for the general public

### [Saving Plans](https://aws.amazon.com/es/savingsplans/compute-pricing/):

The Saving Plans offer a flexible pricing model that allows you to save money on some AWS services. This option offers lower prices for EC2 regardless of the family, instance type, OS, tenancy, or region. It also applies to Fargate and Lambda workloads. How does it work? It offers a discounted pricing compared to on-demand that can be, in the best case, a 72% discount. For compute (applies to EC2 instance usage, AWS Fargate, and AWS Lambda service usage regardless of instance family, size, AZ, region, OS, or tenancy),For EC2 instances (EC2 Instance Saving Plans provide the lowest prices, offering savings up to 72% in exchange for commitment to usage of individual instance families in a region (e.g. M5 usage in N. Virginia). This automatically reduces your cost on the selected instance family in that region regardless of AZ, size, OS or tenancy) it can also be used with sagemaker but that is beyond my knowledge. **this model work in exchange for a commitment to using a specified amount of compute power (measured per hour) for a 1 or 3 year period instead of making commitments to specific instance configurations**. When you subscribe to a saving plan, the prices that you will pay will remain fixed for the period of the plan. Here you have 3 options of payment for your commitment:

- All-upfront: All payment in the first month
- Partial upfront: An initial payment
(at least the 50%) and then a reduced amount of the total per month
- No-upfront: No payment is done at the beginning, each month you will pay a portion of the total

The percentage of savings depends on various factors such as the commitment period, the hourly commitment usage.if the EC2 Instance saving plan is chosen region, and instance family become part of the factors to consider. **For services that utilize EC2 behind the scenes, the saving plans do not apply to those services, but instead to the underlying EC2 instances**.

### [Reserved Instances](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-reserved-instances.html) 

Provides a significant saving on your EC2 costs compared to on-demand instance pricing. **These instances are not physical instances but rather a discount applied to on-demand instances**, these on-demand must match certain attributes like instance type and region to apply the billing discount.
So how does it work? Let's imagine you have an m6g on-demand instance, when you buy the reserved instances you make the attributes to match the running instances, and automatically the discount will be applied if there is a match between the RI and the on-demand instance. You could also buy the RI first and then launch the instance.

> **Which values determine the pricing of the instance?**
instance type, region, tenancy and OS platform
**which terms commitments are available?**
are the same options as a saving plan, from 1 to 3 years(3 offering a bigger discount).

    

> once you bougth a reserved instances it can't be canceled but a new feature got integrated this week to allow you  [regret your purchase before 7 days](**https://docs.aws.amazon.com/savingsplans/latest/userguide/sp-purchase.html#return-sp**) you can also modify, change or sell your RI if your needs change.

#### offering class

this is an option that allows you to modify or exchange your  RI's depending on the offering class selected

{% cta https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/reserved-instances-types.html#offering-class-differences %} comparision table of offering classes {% endcta %}
- Standard: Offers a significant discount but can only be modified. This means that you can adjust some of its attributes during the years of the RI but can't be exchanged for a different RI. 
- Convertible: Provides a lower discount than Standard RI's but the main advantage is that it can be exchanged for different Convertible RI's with different attributes or modify the same RI.

The first thing to define before buying is the scope of the RI. The scope doesn't affect the price but makes a difference on the features available. For example, in the case of a regional the RI doesn't reserve capacity. On the other hand, selecting a zonal RI reserves capacity on that AZ and the discount only applies if the instance is launched on that AZ. Although this scope is not flexible on the instance type and size, the regional applies to the use of a specific family regardless of the size (**this concept is known as instance size flexibility**). I encourage you to take a look into this 

{% cta https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/reserved-instances-scope.html#reserved-instances-regional-zonal-differences %} comparison table {% endcta %} 
to understand this in detail.

- Regional: the RI is bougth for a specific Region(regional RI)
- Zonal: the RI is bougth for a specific Availability Zone(zonal RI)

#### how to use the RIs?
a common mistake that happens when we hear Reserved instances is that we think that a physical instance is getting reserved for our use but this is not true(unless a zonal RI is selected), that really happnes is that a reserved instance is a discound applied to on-demand instances, as i explained at the beggining of the IRs section a match between the on-demand instances and the RI attributes must exists to apply the discount automatically.

#### getting deep into instance size flexibility
the flexibility of the instance is determined by a concept called **normalization factor**. the discount applies total or partially to the ondemand instances depending on the size of the RI (**only applies to regionals RI's**). the only attributes that must match are the family, tenency and platform. 

{% cta https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/apply_ri.html#ri-normalization-factor %} normalization-factor-table {% endcta %}

each instance has a normalization factor, which is applied acording to the size of the Instances, thsi value is used to apply the discount. example a t3.medium has a normlization factor of 2 but imagine you launched 2 t3.small instances with a normalization value of 1, in this case the benefit applies as if you were using only one medium instance due to the normalization factor. but if for example you had a t3.large instances that has a NF of 4 then only 50% of the discount will be applied. 

> this is applied from the smallest instance size to the largest in use
{% cta https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/apply_ri.html#ri-usage-examples %} examples of RI's with normalization factor {% endcta %}
> under organiation accounts the discount is applied first in the account where the RI was bougth and then if applicable to another account in the organization.(i don't know under which condtions this applies)

I will not delve further into the topic of reserved instances but here are three links that may be useful to you and that I have not covered in my blog

- [can i sell and buy reserved instances in the market?](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ri-market-general.html)
- [can i modify attributes of my RI's and what are the limitations?](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ri-modifying.html)?
- [can i exchange RI's convertibles and how the normalization factor affects?](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ri-convertible-exchange.html)

{% cta https://aws.amazon.com/es/ec2/pricing/reserved-instances/pricing/ %}RI pricing calculator {% endcta %}

### [SPOT INSTANCES](https://aws.amazon.com/es/ec2/spot/)
the spot instances uses compute that is not currently utilized of ECE, this is available at a lower pricing than compared to on-demand. tha can be up to 90% in the best case. aws defines the spot pricing per hour called **spot price** The Spot price of each instance type in each Availability Zone is set by Amazon EC2, and is adjusted gradually based on the long-term supply of and demand for Spot Instances

> spot instances runs only when unused capacity is available

spot instnaces are a good option for workloads than can be interrupted. allowing you to have the most cost-effctive solution with a trade-off of losing instance availabily

#### KEY CONCEPTS
- Spot capacity Pool: a group of unused EC2 instaces with the same instance type and Az
- Spot Price: price of the spot instance per hour
- Spot instance Request: request for a spot instace when capacity is available. can be a one time or persitent to be reused in future requests. ec2 automatically recreates a persistent request when it has been fulfilled and interrupted.
- Spot Instance Interruption: ec2 terminates, stops or hibernates your spot instance when it needs the capacity back, an interruption notices is sent to give two minutes windows time before interrupting your instance so you can rebalance your workload.
- EC2 Instance Rebalance: **EC2 emits an EC2 instance rebalance reconmendation**  to notify that a spot instance is at high risk of getting interrupted. this gives you the opportunity to rebalance your wokads usign other spots intances without having to wait for the 2 minute interruption
i will not delve into the usage of spot instances because this is a very extensive topic and i expect to create an specific post in the future to show its usage at detail in conjuntion with eks karpenter

### [CAPACITY Reservation](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/capacity-reservation-overview.html)
a CR allows you to reserve compute capcity for EC2 instances under an specific AZ. this is very useful on strict capacity requirements for workloads that require capacity assurance for High Availability, with this you can ensure that you'll always have access to the ec2 capcitity you've reserved it for as long as you need it.
there are other uses cases as machine learning models training  where you can need more GPU's during the training porcess but there is no more capcity available at that time, with CR's you can ensure that oy the capatity needed for the time of the training(i will not delve into the capacity blocks for ML due to my lack of knwoledge in the Ml area).
one of the main advantages of this purchare model is that you can buy this at any time without a commitment of one-year or three-year term.  **The capacity becomes available and billing starts as soon as the Capacity Reservation is provisioned in your account. When you no longer need the capacity assurance, cancel the Capacity Reservation to release the capacity and to stop incurring charges**
this model can be combined with the billing discounts of Savings Plans and Regional RI's to reduce the cost of a Capacity Reservation. zonal RI's doesn't need a capacity reservation becasue that feature is already provided by that purchase model.
> CR's can only be used by instances that match their attributes. By default they are automatically used by running instances that match the attributes. If you don't have any running instances that match the attributes of the Capacity Reservation, it remains unused until you launch an instance with matching attributes

{% cta https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-capacity-reservations.html#capacity-reservations-differences %} Differences between Capacity Reservations, Reserved Instances, and Savings Plans {% endcta %} 

this a great table to understand the differences between the main pricing models

>important to know that you will get billed as soon as the CR starts and ends once gets expired or cancelled

### How to make decisions?

it's fully dependant on your use case. for an initial POC you can start with on-demand once you have a clear definition of your mimum usage i will go with saving plans, not to cover all the costs but at least have a discount with a defined minimum usage of my ec2 instances. then once you have a clear definition of your usage and your expectations for the future, i'll take a look into the RI's at least for a 1 year commitment.(but only for those workloads that i'm sure will be using that capacity for that period of time). 3 years commitment for RI's? is a great discount but the commitment is a lot of compromise. i will prefer to go with a convertible in case that the business needs change over time. spot instances? only for those workloads that can be interrupted and i want to optimize the cost of it. i have seen amazing histories of costs optimizations using spot instances with eks karpenter so if you know how to use it you can get a huge cost optimization. at the end i will say that probably you will not find the best option at the first try but the cost-optimization is a work of progression over time and will get mature in conjuntion with your workload expectations and business needs. 

i hope you liked this content. i'm making this EC2 at detail serie as part of my deep study of EC2. i found myself in some situations where i was creating cost-estimations and i didn't know the differences between the purchase options of ec2 and now that i got the opportunity to delve into this topic i feel very confident about providing the nearest rigth solution depending on the use case and the needs of my clients. maybe that happened to others... being on the aws cost calculator and not knowing the difference between each purchase option😄