AWS Basics - Beginner's Guide to EC2
Tags: aws
Published: 2022-01-01 16:36:43

## EC2

**EC2** stands for **Elastic Compute Cloud** - Virtual machine in the cloud

> Remember to turn off services that are no longer needed to avoid unnecessary billing.

> To get an estimate of the usages in AWS, use [AWS Pricing Calculator](https://calculator.aws/#/).

### EC2 Pricing Options

#### On-demand
- Fixed rate by hour or second, no commitment
- **USE:** good for short-term, spiky and unpredictable workload

#### Reserved (RIS)
- 1 year or 3 years of commitment, discount on the hourly charge for an instance
- **Standard RIS:** cannot be changed once committed, up to 72% off on-demand price
- **Convertible RIS:** can be changed to a different RIS type of equal or greater value, up to 54% off on-demand price
- **Scheduled RIS:** allows launching an instance within the specified time frame.
- Operates at a regional level

**USE:** good for applications with
(i) steady-state or predictable usage pattern
(ii) applications that require reserved capacity

#### Spot
- Purchase unused capacity at a massive discount of up to 90%
- Bid a price for the instance you want

> Prices fluctuate based on supply and demand

- **Termination charges:** 

| Termination by EC2 | Manual termination
| - | - |
| No extra charges will be levied for partial hours usage | Charges will be levied for the complete hour in which the instance ran |

- **USE:** good for the following use cases:

(i) applications that have flexible start and end times
(ii) applications that are only feasible at very low compute prices
(iii) good for users with an urgent need for large additional computing capacity (specific job or workload and not ongoing)

#### Dedicated hosts
- Physical EC2 server dedicated for our own use - have on-demand and reserved options (up to 70% off on the on-demand price)
- Allows us to use our existing server-bound software licenses
- **Most expensive option**
- **USE:** where multi-tenancy is not an option

> **Quick note on Lambda and Fargate**

> **Lambda** is the very first **_serverless_** architecture allowing us to run our application on the cloud without worrying much about the infrastructure.

> **Fargate** allows us to run docker containers on the cloud without worrying about the underlying infrastructure.

### Launching an EC2 instance

1. Go to `EC2 Dashboard`
2. Click on `Launch instance`
3. Choose an `Amazon Machine Image (AMI)`
4. Choose an EC2 instance type
5. Configure instance details
6. Add storage (default available volume of 8 GiB as root)
7. Add tags to organise EC2 instances (optional step)
8. Configure security group to allow traffic in and out of the instance (default to SSH access with TCP protocol and port 22), but, we might need to add `HTTP` too with port `80` and source set to `Anywhere`, `0.0.0.0/0, ::0` which means anyone with any IP address will be able to access the server 
9. Select an existing key pair or create a new key pair
10. Download the key pair
11. Launch instance

### Connect to an EC2 instance

> A **key pair** can be either created or added from a list of existing key pairs when launching a new instance.

> A **key pair** consists of a public key that AWS stores, and a private key file that you store. Together, they allow you to connect to your instance securely.

To connect to an EC2 instance and run a web server:

```bash
# To allow read only permission
chmod 400 MyNewKeyPair.pem
ssh -i "MyNewKeyPair.pem" ec2-user@<public_ip_address_ec2_instance>
sudo su
yum update -y
  
# Install Apache
yum install httpd -y
  
# Start server
systemctl start httpd
# Whenever system reboots, it is going to start httpd
systemctl enable httpd
# To check the status
systemctl status httpd

# To start the server automatically when EC2 instance boots,
chkconfig httpd on
  
# Check server status
service httpd status
```

Root directory of our web server (default directory that gets created when we install the web server): `cd /var/www/html`

To connect to the website, use the public IP address.

> Primer on chmod: [`chmodcommand`](https://chmodcommand.com/chmod-400/)

### EC2 Instance Types

> Underlying hardware (compute memory, storage capabilities, etc.) for the selected instance type

| Family | Specialty | Use case |
| - | - | - |
| F1 | Field Programmable Gate Array | Genomics research, financial analytics, real-time video processing, big data etc |
| I3 | High Speed Storage | NoSQL DBs, Data Warehousing etc |
| G3 | Graphics Intensive | Video Encoding/ 3D Application Streaming |
| H1 | High Disk Throughput | MapReduce-based workloads, distributed file systems such as HDFS and MapR-FS |
| T2 | Lowest Cost, General Purpose | Web Servers/Small DBs |
| D2 | Dense Storage | Fileservers/Data Warehousing/Hadoop |
| R4 | Memory Optimized | Memory Intensive Apps/DBs |
| M5 | General Purpose | Application Servers |
| C5 | Compute Optimized | CPU Intensive Apps/DBs |
| P3 | Graphics/General Purpose GPU | Machine Learning, Bit Coin Mining etc |
| X1 | Memory Optimized | SAP HANA/Apache Spark etc |