# Elastic Compute Cloud (EC2)

## Icon

![EC2 Icon](https://icon.icepanel.io/AWS/svg/Compute/EC2.svg)

## Basics

* [Introduction](../../../cloudpractitioner/03_ec2_introduction)

### User Data

* User Data is basically bootstrap script that runs once at the instance's **first start** of its entire lifecycle.
* This enables us to run software installation, updates.
* This script runs with **root privilege**.

## Instance Types

* They are categorized based on instance types.
* Detailed list can be found [here](https://aws.amazon.com/ec2/instance-types/).
* For comparison of instance type visit [this](https://instances.vantage.sh/).

### Naming convention

* \<instance-class>\<class-generation>**.**\<size-of-instance-class>
*   Example: `m5.2xlarge`

    ```
    m: instance class
    5: generation class
    2xlarge: size within instance class
    ```

## Instances Puchasing Options

* The following are different purchasing options available for EC2 instance.

### On-Demand Instances

* Pay as per your usage
  * Windows and Linux, billing per second, after first minute.
  * Other OS, billing per hour.
* Highest cost but no upfront payment.
* No long-term commitment.
* Recommended for short and uninterrupted workloads, where you can't predict application behavior.

### Reserved (1 or 3 years)

#### Reserved Instances

* Upto `72%` discount _(aws may change this over time)_ compared to On-demand.
* Reserve based on specific instance attribute _(Instance Type, Region, Tenancy, OS)_.
* Payment Options - No/partial/fully upfront, where all upfront gives maximum discount.
* Scope can be regional or zonal.
* Recommended for steady-state usage applications.
* Can buy or sell reserved instances from **Reserved Instance Marketplace**.

#### Convertible Reserved Instances

* Can changes EC2 insance type, family, OS, scope and tenancy.
* Upto `66%` discount _(aws may change this over time)_.

### Savings Plan (1 & 3 years)

* Discount based on long term usage _(upto `72%` same as Reserved instances)_ _(aws may change this over time)_.
* Requires commitment to certain type of usage _(like Rs1000 per hour)_.
* Usage beyond the savings plan is billed on On-Demand instance family.
* Locked to a specific instance family and region. However instance-size, OS, tenancy can be changed.

### Spot Instances

* Can get upto 90% discount compared to On Demand.
* These are instances which you can lose at any point in time if max price is less than the current spot price.
* This is the most cost-efficient instances in AWS.
* Recommended for work-load that is resillient to failure like batch jobs, data analysis, image processing, workloads with flexible start and end time.
* **Not suitable for critical jobs or databases**.

### Dedicated Instances

* Instances run on hardware that's dedicated to you.
* May share hardware with other instances in same account, but not with other account.
* No control over instance placement.

### Dedicated Hosts

* A physical server with EC2 instance capacity fully dedicated to your use, will not be shared with any other customer _(dedicates a physical server)_.
* Can control instance placement.
* Recommended to address strong regulatory or compliance requirement.
* Purchasing options can be like on-demand and reserved _(1 year or 3 year)_. **Most expensive option**.
* Useful for softwares that have complicated licensing model _(BYOL - Bring Your Own License)_.
* Gives you visibility to the underlying hardware unlike Dedicated Instances.

### Capacity Reservations

* Reserve On-demand instances capacity in a specific AZ for any duration.
* You always have EC2 capacity when you need it.
* No time commitment, no billing discounts.
* To get discounts one can combine them with Regional Reserved Instances and Savings Plan.
* Charged based on On-demand rate whether you run instances or not.
* Suitable for short-term, uninterrupted workloads in a specific AZ.

## References

* Read more about EC2 instance pricing [here](https://aws.amazon.com/ec2/pricing/).
* Read more about EC2 instance life-cycle [here](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html).
* Difference between [Dedicated Host v/s Dedicated Instance](https://www.trek10.com/blog/dedicated-hosts-and-dedicated-instances)
