# EC2

## EC2 (Virtual machine)

"basic computer/server", "instance". Use it for what ever you like.

**E**lastic **C**ompute **C**loud => EC2

### Purchasing options

* On-Demand
  * most expensive, most flexible (billed by hour)
* Reserved
  * Purchase an instance for a set period of time for 1 or 3 years
  * you have a big discount
  * it does not matter whether you use it or not => you pay
* Spot
  * unused instances are sold by amazon with a discount for short time
  * spot prices fluctuate
  * you are charged by the minute
  * when you have a bid => instance is provisioned for you when the spot price      =< your bid
  *   ```
      if (spot price > bid price) 
          provisioned instance terminates automatically
      ```



### AMI (Amazon Machine images)

`AMI = EC2 + operating system + software packages + settings`&#x20;

This is a template to quickly spin up EC2 instances based on defined AMI. (analog is a docker image and docker containers)

#### AMI components

1. Root volume template
   1. operating system (e.g. Amazon linux)
   2. application software (e.g. Apache web server)
2. Launch permissions (who is allowed to launch)
3. Block device mapping
   1. EBS (hard drive mapping)

#### Available AMIs

* Community AMI
  * free to use (but it is mainly a choice of OS)
* AWS marketplace AMI
  * payed (may be with special vendor software)
* My AMI
  * create yourself

### Instance types

This is a CPU of your instance (hardware of the instance). E.g. t2.nano, t2.micro, t2.large, t2.xlarge, etc

### EBS (Elastic Block Store)

EBS is a storage volume for an EC2 instance (think of hard drive).

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M8oxJFiEzEa7uIlUY6O%2F-M8p0noVxXBfofemc6Qu%2FScreen%20Shot%202020-06-02%20at%2014.34.53.png?alt=media\&token=5686aebc-34b3-4cb7-900b-1c876cea0eda)

#### EBS Snapshot

1. A snapshot is an image of EBS volume that can be **stored as a backup** of the volume or used to **create a duplicate**.
2. A snapshot is not an active EBS volume => you can not attach/detach it to EC2 instance
3. To restore a snapshot you need to create a new EBS volume using the snapshot as its template.

#### IOPS

Input/Output operations per second. (in KiB)

Amount of data that can be written to or retrieved from EBS per second.

Each EC2 instance **must** have a root volume, which may or may not be EBS.

### Security groups

Security groups are similar to NACL, but Security groups are found on instance level.

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M8p1kTtfvgklGTPyySt%2F-M8p43dRoy70ZQIxjWr2%2FScreen%20Shot%202020-06-02%20at%2014.49.01.png?alt=media\&token=4ca26e75-09d5-4479-bff2-7ea3f280816c)

* Security groups are stateful
  * if traffic could reach an instance through SG rule => the traffic will be able to leave SG even if there is no matching outbound SG rule
* SG does not have numbers comparing to NACL rule numbers
* Up to five (5) security groups can be assigned per instance.

### IP addressing

It is providing an EC2 instance with a **public IP address**.

* private IP address
  * by default all EC2 instances have a it
  * it allows instances to communicate with each other as long as they are located in the same VPC (private network)
* public IP address
  * EC2 instances can be launched with/without it depending on VPC/subnet settings
  * Public IP addresses are required for the instance to communicate with a internet
  * default VPC and subnets are configured so that any new instance  that is provisioned has a public IP

![](https://415484505-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LxtoAXZwwOc4XGto8vb%2F-M9YNuFl9gT3t-YtiE04%2F-M9YPoo5fXVVgcCwmWKX%2FScreen%20Shot%202020-06-11%20at%2014.46.30.png?alt=media\&token=cc77f30b-7947-42f4-bb0a-c25ab1d91d8c)

##
