# EC2 (Virtual Machine)

## Instance Types

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F9En5UHZbguGRApMz1lSW%2Fimage.png?alt=media\&token=0f671262-c202-46d8-82be-098ed61ba51e)

### General Purpose

* General purpose instances provide a balance of compute, memory and networking resources, and can be used for a variety of diverse workloads. These instances are ideal for applications that use these resources in equal proportions such as web servers and code repositories.

### Compute Optimized

* Compute Optimized instances are ideal for compute bound applications that benefit from high performance processors. Instances belonging to this family are well suited for batch processing workloads, media transcoding, high performance web servers, high performance computing (HPC), scientific modeling, dedicated gaming servers and ad server engines, machine learning inference and other compute intensive applications.

### Memory Optimized

* Designed to deliver fast performance for workloads that process large data sets in memory, such as hosting database

### Storage Optimized

* Designed for workloads that require high, sequential read and write access to very large data sets on local storage. They are optimized to deliver tens of thousands of low-latency, random I/O operations per second (IOPS) to applications.

## Pricing Options

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FgrM6aAyRWTgEm3bJlDAV%2Fimage.png?alt=media\&token=0f3ba13f-0b8f-4454-8006-c95179e24c79)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FeGF8Sgp2MURkmYBHpYxz%2Fimage.png?alt=media\&token=f5430fe3-7343-47ab-889a-221655926aa0)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FdR2X2hZjgrf2qkK1XIjQ%2Fimage.png?alt=media\&token=1e4c14db-6fe4-4086-91c3-1650803f09f8)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F62crmQRjGFu2j1EnoHEV%2Fimage.png?alt=media\&token=113e26f7-dd6a-4900-bfe9-1d21858b4f41)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FyDS5M6tRtA2I2dOdTQTS%2Fimage.png?alt=media\&token=31983d30-ddc2-4cf4-8c7a-f3202e327fb7)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FiXRDQgGq55cI0izlVtax%2Fimage.png?alt=media\&token=604f3d2f-78fc-4abe-9855-490f335116be)

* On Demand: Pay per second after first minute
* Reserved Instances: Reserve the instance for at least 1 year for getting discount
* Spot Instance: Instance can be lost at any time, if other works are needed for getting discount
* Dedicated Host: A physical server is fully dedicated for your use, the most expensive option

## Data Storage

### EBS Volume

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F1zD7TU2d6vDH1BDTAndh%2Fscreenshot-www.udemy.com-2023.03.24-01_57_21.png?alt=media\&token=3412a0c9-b119-4bf8-8249-a916cf79c421)

* A network drive that attach to EC2 instance
* Allow persisting data even after termination
* Can be detached and attached to other instance quickly
* Locked in a AZ, so the AZ of EC2 and EBS must be the same

### EBS Snapshots

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FnppYVqgDdCQRMiP0QKdU%2Fscreenshot-www.udemy.com-2023.03.24-01_59_46.png?alt=media\&token=57b05a2d-8629-4c1a-819b-0123369f3a5c)

* Make a backup of EBS at a point in time
* can be restored across AZ or region

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FDQHNAYVVxdwSfMWmStXH%2Fscreenshot-www.udemy.com-2023.03.24-02_01_55.png?alt=media\&token=009da793-fc25-4b7a-a397-8dfd4ccc0346)

* SSD: better i/o ops
* HDD: higher volume

### Multi-Attach

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2Fxdp0U0ZuKXbD9bR1agCf%2Fscreenshot-www.udemy.com-2023.07.18-02_21_40.png?alt=media\&token=f2ec354b-e148-4b50-ad41-0134165080f2)

* For io1/io2 class, the same volume can be attached to multiple instances
* Up to 16 instances at a time

### Instance Store

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FKpzF4ftqwjsGdkeu83Pm%2Fscreenshot-www.udemy.com-2023.03.24-02_02_44.png?alt=media\&token=cf036b0f-5cd2-4952-a117-2fba51256728)

* Advanced version of EBS
* Better I/O performance
* Lose the storage if EC2 are stopped so better for keeping temporary data

### EFS (Elastic File System)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F2z0QPbRw40qxf0Xev7ZE%2Fscreenshot-www.udemy.com-2023.03.24-02_03_52.png?alt=media\&token=ad20fa1a-fcd1-4339-8fbb-74789bc251d6)

* Managed network file system that can mounted in many EC2
* Work with EC2s with multiple A-Z
* Highly available and scalable&#x20;

## Placement Groups

### Cluster

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2Fa7zlw9rl9MsPY2BXiUPI%2Fscreenshot-www.udemy.com-2023.03.23-17_13_24.png?alt=media\&token=3534fbf6-4b59-44e6-855c-3479f2f39509)

* Put virtual machines into the same hardware to achieve low latency between them

### Spread

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FzAS4xGtD7zoz8psh6w1V%2Fscreenshot-www.udemy.com-2023.03.23-17_16_45.png?alt=media\&token=d63f63d8-6319-4620-a95d-0903076d540a)

* Spread different virtual machines into different hardwares

### Partition

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2Fwr0OCe7SSXldgDX7YF6V%2Fscreenshot-www.udemy.com-2023.03.23-17_20_46.png?alt=media\&token=29519c1e-5775-42f1-82d5-bea809600936)

* Split the virtual machines into different partitions
* Each partition belongs to different hardwares
* Virtual machines in the same partition belongs to same hardware

## Load Balancing

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F4B9yrD3HVyANh8pif9t8%2Fscreenshot-www.udemy.com-2023.03.24-14_58_33.png?alt=media\&token=bb2b99e8-b08d-4425-b2b2-864d432aa171)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FuAK9AkJfXRyUSLdT88ZB%2Fscreenshot-www.udemy.com-2023.03.24-14_59_37.png?alt=media\&token=834c0f80-a909-476b-8bab-67e16ce2560e)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FlSwIoW1XHsv6x1zpv0s9%2Fscreenshot-www.udemy.com-2023.03.24-15_00_22.png?alt=media\&token=53cf5101-d489-46d2-ba11-fe75f091f1bf)

* Provide high availbility across the zones
* Expose single of access to application
* Separate the public traffic from private traffic

### Application Load Balancer

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FLYtKlD8kJGHfn9QSXvH4%2Fscreenshot-www.udemy.com-2023.03.24-15_06_25.png?alt=media\&token=d40e1e3d-a26d-48e0-83a5-da9b7db4a4b4)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FfyzA7DrMGLqEOAJYYNoD%2Fscreenshot-www.udemy.com-2023.03.24-15_07_09.png?alt=media\&token=f1d1c152-5854-4484-9897-cc2596718636)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FyVd7dTo9rKQsufwjt3cI%2Fscreenshot-www.udemy.com-2023.03.24-15_08_11.png?alt=media\&token=5ad582e2-42b7-4dcc-8516-dc4dfd1a74df)

* Layer 7 of load balancer
* Based on the route, query params

### Network Load Balancer

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F6LTI3pHxDpGNJ5l7kVpQ%2Fscreenshot-www.udemy.com-2023.03.24-15_45_39.png?alt=media\&token=14d9b89f-dee1-4665-bac1-bb9dfee9285f)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FFJjpAl600ff7l2VPnrzC%2Fscreenshot-www.udemy.com-2023.03.24-15_46_09.png?alt=media\&token=58a346f0-7a26-44e8-b994-8317b9def0ee)

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FgERFA4BVDsykL4gsJEw5%2Fscreenshot-www.udemy.com-2023.03.24-15_46_46.png?alt=media\&token=d9004d2a-5f83-4d48-bd87-ab72ed003406)

* Layer 4 of load balancer
* Based on port number

### Gateway Load Balancer

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FuaT11cWXdGNEuf8fKd5Y%2Fscreenshot-www.udemy.com-2023.03.27-18_41_04.png?alt=media\&token=dea78434-4757-42ad-abbf-45e4528a838b)

### Sticky Sessions

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2FXRONX4o6S9BWl4XVwAhP%2Fscreenshot-www.udemy.com-2023.03.27-18_46_09.png?alt=media\&token=12bfbce9-e667-447f-aaa7-93ee386144ba)

* User with the cookie will always go to the same instances

### Cross-Zone Load Balancing

![](https://1374779285-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MFW3x2aqEO8GF2kr3VU%2Fuploads%2F7nvVJCEnora13CxsGLwK%2Fscreenshot-www.udemy.com-2023.03.27-18_49_31.png?alt=media\&token=c1b2d633-b95e-4a38-a905-358fd2c8ecb8)

* Evenly disturb the traffic across all instances in all AZ
