# Introduction to AWS EC2

* AWS [EC2](https://aws.amazon.com/ec2/?ec2-whats-new.sort-by=item.additionalFields.postDateTime\&ec2-whats-new.sort-order=desc) service is a virtual server provider _(using Compute as a Service model)_ which runs on AWS just like a raw compute server with several advantages.
  * They are high flexible, cost-effective, and quick.
  * AWS has already bought and secured the data centers with these virtual servers.
  * You can provision and launch an Amazon EC2 instance within minutes.
  * You can stop using it when you have finished running a workload.
  * You pay only for the compute time you use when an instance is running, not when it is stopped or terminated.
  * You can save costs by paying only for server capacity that you need or want.
* In brief, they provide secure, resizable compute capacity in the cloud as Amazon EC2 instances.
* EC2 instances are virtual machines which uses multitenancy. Read more about the concept of [multitenancy](https://www.cloudflare.com/en-in/learning/cloud/what-is-multitenancy/), which means sharing underlying hardware between virtual machines.
* EC2 instance allows us following hardware/softwares to be configurable
  * OS
    * Linux
    * Windows
    * MAC OS
  * Compute Power _(CPU)_
  * RAM _Resizable_ i.e vertical scaling
  * Software packages of your choosing
  * Networking
  * Storage type
  * Firewall _(Security Group)_
  * User Data _(Bootstrap Script for first launch)_

## AWS EC2 Instance types

* AWS instance types are grouped under different types of instance family.
  * General purpose
  * Compute Optimized
  * Memory Optimized
  * Accelerated Computing
  * Storage Optimized
  * Instance Features
  * Measuring Instance Performance

## Instance types Deep Dive

### General purpose

* General purpose instances provide a balance of compute, memory, and networking resources. One can use them for a variety of workloads, such as:
  * Application servers
  * Gaming servers
  * Web services
  * Backend servers for enterprise applications
  * Small and Medium databases
* Suppose that you have an application in which the resource needs for compute, memory, and networking are roughly equivalent. You might consider running it on a general purpose instance because the application does not require optimization in any single resource area.

### Compute optimized

* Compute optimized instances are ideal for compute-bound applications that benefit from high-performance processors. Like general purpose instances, you can use compute optimized instances for workloads such as web, application, batch processing and gaming servers.
* However, the difference is compute optimized applications are ideal for high-performance web servers, compute-intensive applications servers, and dedicated gaming servers. You can also use compute optimized instances for batch processing workloads that require processing many transactions in a single group.

### Memory optimized

* Memory optimized instances are designed to deliver fast performance for workloads that process large datasets in memory. In computing, memory is a temporary storage area. It holds all the data and instructions that a central processing unit (CPU) needs to be able to complete actions. Before a computer program or application is able to run, it is loaded from storage into memory. This preloading process gives the CPU direct access to the computer program.
* Suppose that you have a workload that requires large amounts of data to be preloaded before running an application. This scenario might be a high-performance database or a workload that involves performing real-time processing of a large amount of unstructured data. In these types of use cases, consider using a memory optimized instance. Memory optimized instances enable you to run workloads with high memory needs and receive great performance.

### Accelerated Computing

* Accelerated computing instances use hardware accelerators, or coprocessors, to perform some functions more efficiently than is possible in software running on CPUs. Examples of these functions include floating-point number calculations, graphics processing, and data pattern matching.
* In computing, a hardware accelerator is a component that can expedite data processing. Accelerated computing instances are ideal for workloads such as graphics applications, game streaming, and application streaming.

### Storage optimized

* Storage optimized instances are designed for workloads that require high, sequential read and write access to large datasets on local storage. Examples of workloads suitable for storage optimized instances include distributed file systems, data warehousing applications, and high-frequency online transaction processing (OLTP) systems.
* In computing, the term input/output operations per second (IOPS) is a metric that measures the performance of a storage device. It indicates how many different input or output operations a device can perform in one second. Storage optimized instances are designed to deliver tens of thousands of low-latency, random IOPS to applications.
* You can think of input operations as data put into a system, such as records entered into a database. An output operation is data generated by a server. An example of output might be the analytics performed on the records in a database. If you have an application that has a high IOPS requirement, a storage optimized instance can provide better performance over other instance types not optimized for this kind of use case.

## Pricing Options for EC2

* With Amazon EC2, you pay only for the compute time that you use. Amazon EC2 offers a variety of pricing options for different use cases.
  * **On-Demand**
    * On-Demand Instances are ideal for short-term, irregular workloads that cannot be interrupted. No upfront costs or minimum contracts apply. The instances run continuously until you stop them, and you pay for only the compute time you use.
    * Sample use cases for On-Demand Instances include developing and testing applications and running applications that have unpredictable usage patterns. On-Demand Instances are not recommended for workloads that last a year or longer because these workloads can experience greater cost savings using Reserved Instances.
  * **Savings Plan**
    * AWS offers Savings Plans for several compute services, including Amazon EC2. Amazon EC2 Savings Plans enable you to reduce your compute costs by committing to a consistent amount of compute usage for a 1-year or 3-year term. This term commitment results in savings of up to 66% over On-Demand costs.
    * Any usage up to the commitment is charged at the discounted plan rate (for example, $10 an hour). Any usage beyond the commitment is charged at regular On-Demand rates.
  * **Reserved Instances**
    * Reserved Instances are a billing discount applied to the use of _On-Demand_ Instances in your account. You can purchase **Standard Reserved** and **Convertible Reserved** Instances for a 1-year or 3-year term, and **Scheduled Reserved** Instances for a 1-year term. You realize greater cost savings with the 3-year option.
    * At the end of a Reserved Instance term, you can continue using the Amazon EC2 instance without interruption. However, you are charged On-Demand rates until you do one of the following:
      * Terminate the instance.
      * Purchase a new Reserved Instance that matches the instance attributes (instance type, Region, tenancy, and platform).
    * You can make payment of Reserved instances in All upfront, partial upfront or no upfront payment option.
  * **Spot Instances**
    * Spot Instances are ideal for workloads with flexible start and end times, or that can withstand interruptions. Spot Instances use unused Amazon EC2 computing capacity and offer you cost savings at up to 90% off of On-Demand prices.
    * Suppose that you have a background processing job that can start and stop as needed (such as the data processing job for a customer survey). You want to start and stop the processing job without affecting the overall operations of your business. If you make a Spot request and Amazon EC2 capacity is available, your Spot Instance launches. However, if you make a Spot request and Amazon EC2 capacity is unavailable, the request is not successful until capacity becomes available. The unavailable capacity might delay the launch of your background processing job.
    * AWS gives a 2 minute warning to complete the on going process before terminiating the spot instance if already runnning.
  * **Dedicated Hosts**
    * Dedicated Hosts are physical servers with Amazon EC2 instance capacity that is fully dedicated to your use.
    * You can use your existing per-socket, per-core, or per-VM software licenses to help maintain license compliance. You can purchase On-Demand Dedicated Hosts and Dedicated Hosts Reservations.
    * Of all the Amazon EC2 options mentioned above, Dedicated Hosts are the most expensive.

## Scaling Amazon EC2

* Scalability involves beginning with only the resources you need and designing your architecture to automatically respond to changing demand by scaling out or in. As a result, you pay for only the resources you use. You don’t have to worry about a lack of computing capacity to meet your customers’ needs.
* If you wanted the scaling process to happen automatically, which AWS service would you use? The AWS service that provides this functionality for Amazon EC2 instances is **Amazon EC2 Auto Scaling**.
* Amazon EC2 Auto Scaling enables you to automatically add or remove Amazon EC2 instances in response to changing application demand. By automatically scaling your instances in and out as needed, you are able to maintain a greater sense of application availability.
* Within Amazon EC2 Auto Scaling, you can use two approaches for auto scaling: dynamic scaling and predictive scaling.
  * Dynamic scaling responds to changing demand.
  * Predictive scaling automatically schedules the right number of Amazon EC2 instances based on predicted demand.

> **Note:** To scale faster, you can use dynamic scaling and predictive scaling together.

* Types of Scaling
  * Scale up i.e., Vertical scaling
  * Scale out i.e., Horizontal scaling (using autoscaling service)

### Example of AutoScaling

* In the cloud, computing power is a programmatic resource, so you can take a more flexible approach to the issue of scaling. By adding Amazon EC2 Auto Scaling to an application, you can add new instances to the application when necessary and terminate them when no longer needed.
* This type of elasic scaling is achieved by having different capacities configured on Auto scaling group.
  * When you create an Auto Scaling group, you can set the minimum number of Amazon EC2 instances. **The minimum capacity is the number of Amazon EC2 instances that launch immediately after you have created the Auto Scaling group**.
  * An Auto Scaling group will start by launching as many instances as are specified for **desired capacity**.
  *   The desired capacity must be greater than or equal to the minimum size of the group and less than or equal to the maximum size of the group.

      > **Note:** If you do not specify the desired number of Amazon EC2 instances in an Auto Scaling group, the desired capacity defaults to your minimum capacity.
  * The third configuration that you can set in an Auto Scaling group is the **maximum capacity**. For example, you might configure the Auto Scaling group to scale out in response to increased demand, **but only to a maximum of four Amazon EC2 instances**.
* Because Amazon EC2 Auto Scaling uses Amazon EC2 instances, you pay for only the instances you use, when you use them. You now have a cost-effective architecture that provides the best customer experience while reducing expenses.
