AWS EC2
Tags: devops, aws, cloud, beginners
Published: 2023-04-09 17:29:31

## What is EC2? 

Elastic compute cloud (EC2). It's a virtual machine in AWS. Develop and deploy application faster without worrying about the hardware investments. We can choose the Instance Type and family and attach the security groups to handle the inbound and outbound traffics. We can create the EC2 instance by choosing the AMI (Amazon Machine Image), there are many AMI's available. We can choose anything based on our requirements and attach the security policies or group for the security purpose.

## Create or launch an EC2 Instance

Steps to launch an instance. Go to the Instance tab and find the launch instance button to configure the instance.
Name the instance
- Select the AMI type and instance types, family
- Create the key-pair and download the .pem or .ppk file to access the instance in SSH.
- Launch the instance and go to the instance list and you can find your instance using name.

## Instance Name

![Instance Naming](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pmz2899ejzeua37e1ubh.png)

## Select the instance family

![Instance Family](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8cyc7e7a6s3w6k8sehns.png)

## Edit the network details

![Network Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hysk9spjlk01dz3zkgvo.png)

## Creating Key-Value Pair for accessing the instance using SSH

![For accessing SSH we need to create key pair](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4bh5qeycu9oci0afumzp.png)

After Instance was successfully launched, we can see the following pages

## List of available instances

![List of Instances](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qhs14yum1lrac320dkv7.png)

## Instance details

![Instance Details](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tjd6pfqgr1rfpcwfobg5.png)

## User data for an instance

We can add the user data to the instance. If we add the user data script, it ll run only once when the server is getting launched.

User data should be in shellscript. We can add manually or import from a file and we cannot be able to modify the user data when the instance is in Start stage. We need to stop the instance and modify the user data and restart the instance again.

> Command to access ec2 instance using SSH

```
ssh -i .pem/location Username@publicIPV4

Username should be ec2-user
```

> Command to upload files to ec2 instance using SSH

```
scp -i .pem/location fileLocation username@publicIPV4:~/.
```

SSH Summary table

| **SSH Summary** | **SSH**| **PuTTY** | **EC2 Connect**|
| --------- |:---------:|:---------:|:---------:|
| MacOS | Y | N | Y |
| Linux | Y | N | Y |
| Windows >= 10 | Y | N | N |
| Windows < 10 | N | Y | Y |


## Purchasing options

Several options we can buy EC2 instances

## On Demand

- Short workloads, Predictable pricing, Pay by Second
 - Linux or Windows - Pay per second
 - All other platforms - Pay per hour
- Highest cost by no upfront payment
- No Long-term commitment

> Recommend for short-term and unintereppted workloads or services where you can't predict the application behaviours.
## Reserved Instances

- 72 % discount when compared to OnDemand
- We can reserve specific instances i.e, Instance Type, Region, OS, Tenancy
- Reservation period 1 year with minimal discount and 3 year with maximum discount
- Reserved instance scope like regional or zonal

Recommended for steady state workloads like databases. We can buy and sell the instance at marketplace if not needed.

** Payment options
- No Upfront (+)
- Partial Upfront (++)
- All Upfront (+++)

> Convertible reserve instances like we can change the EC2 instance type, family, OS, scope and tenency.

## Savings plan

- Discount on long term
- Commit to a certain type of usages, like we set $10 / hour.
- Usage beyond EC2 savings plans is billed at OnDemand price.
- Locked to specific instance family and region
- Flexible on Instance Size and OS, that means we can change the instance size and OS.

## Spot Instances

- 90% discount compared with OnDemand
- Instances that you can "lose" at any point of time if the maximun price is less than the current spot price.
- Most cost effective instances in AWS useful for workloads that are resilent to failure.
  - Batch Jobs
  - Data Analysis
  - Image Processing
  - Distributed workloads
  - Flexible start and stop workloads

## Useful links
- [Instance Types](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instance-types.html)
- [Instance lifecycles](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-lifecycle.html)
- [Troubleshooting](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-troubleshoot.html)