Introduction to AWS: EC2, related services, and AWS through the CLI
Tags: aws, cloud, beginners
Published: 2021-07-26 00:00:00

---
title: "Introduction to AWS: EC2, related services, and AWS through the CLI"
date: "2021-07-26"
categories: 
  - "aws"
tags:
  - "aws"
  - "cloud"
  - "beginners"
series: [Introduction to AWS]
---

### The related services are Elastic Block Store, Load Balancing, and Auto-Scaling

![cupofcode_blog_main_picture](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_intro_to_aws_ec2-768x432.png)

In [previous blog posts](https://cupofcode.blog/aws-iam-cloudfront-s3/), I mentioned that AWS services are dependent on each other, which makes it difficult to decide in which order to present them.

The big star of AWS, the one that all tutorials mention, is EC2, so we’ll start there. This will be fun!

Let’s see a funny meme and then jump right into it!

![cupofcode_blog_ec2_meme1](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_ec2_meme1.png)

* * *

## EC2—Elastic Compute Cloud

### Instance Types, Pricing Models, and a Hands-on example

![cupofcode_blog_ec2](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_ec2-1024x576.png)

_Elastic Compute Cloud (EC2)_ is a virtual server in the cloud. Amazon EC2 reduces the time required to obtain and boot new server instances to just minutes, allowing you to quickly scale capacity, both up and down, as your computing requirements change.

A common mistake is to think that EC2 is serverless. Actually, EC2 is a compute-based service, which means it’s a server! So it’s not serverless. 

Note that you should always design for failure and have one EC2 instance in each AZ. Remember what AZ is? We talked about it in [the first blog post](https://cupofcode.blog/cloud-computing-and-aws/).

### EC2 Instance Types

Amazon EC2 provides a wide selection of instance types optimized to fit different purposes. Instance types comprise varying combinations of CPU, memory, storage, and networking capacity and give you the flexibility to choose the appropriate mix of resources for your applications.

The instance types divide into 5 groups: 

1. General-purpose
2. Compute Optimized
3. Memory Optimised
4. Accelerated computing
5. Storage Optimised

![cupofcode_blog_ec2_meme2](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_ec2_meme2.jpg)

Let’s look at the meme above as an example:

Here are the specs of the two types, which are both under general purpose:
```
Type     | vCPU*  | Mem(GiB) | Storage  | Network 
m4 large |   2    |    8     | EBS-Only | Moderate  
t2 large |   1    |    8     | EBS-Only | Low to Moderate
```
Well, it looks like m4 large is better, so why would the guy look at t2 large? Apparently, it’s because of its burstable compute units, which are more suitable for his project. Click [here](https://www.apptio.com/blog/comparing-aws-t2-large-and-m4-large-ec2-instances/) if you want to read more about it, and click [here](https://aws.amazon.com/ec2/instance-types/?trkCampaign=acq_paid_search_brand&sc_channel=PS&sc_campaign=acquisition_IE&sc_publisher=Google&sc_category=Cloud%20Computing&sc_country=IE&sc_geo=EMEA&sc_outcome=acq&sc_detail=ec2%20instance%20types&sc_content=%7Bad%20group%7D&sc_matchtype=e&sc_segment=489284072828&sc_medium=ACQ-P|PS-GO|Brand|Desktop|SU|Cloud%20Computing|EC2|IE|EN|Sitelink&s_kwcid=AL!4422!3!489284072828!e!!g!!ec2%20instance%20types&ef_id=Cj0KCQjwsqmEBhDiARIsANV8H3YCRUMwrDF3z3LD0oEfZNpxRkS273vEbMlGT0GBhFbc3pnAO3PZRhQaApdaEALw_wcB:G:s&s_kwcid=AL!4422!3!489284072828!e!!g!!ec2%20instance%20types) if you want to read more about EC2 instance type.

Now, let’s talk money!

### EC2 Has Four Pricing Models

1. **On-demand:** This allows you to pay a fixed rate by the hour (or by the second) with no commitment.
2. **Reserved:** Provides you with a capacity reservation, and offers a significant discount on the hourly charge for an instance (like booking a hotel room a year in advance). Contract terms are 1 year or 3 years terms.
3. **Spot:** Enables you to bid whatever price you want for instance capacity, providing for even greater savings if your applications have flexible start and end times.
4. **Dedicated hosts:** A physical EC2 server dedicated for your use. Dedicated hosts can help you reduce costs by allowing you to use your existing server-bound software licenses.

Let’s dive deep and see when we should use which:

#### 1\. On-Demand pricing is useful for:

- Users that want the low cost and flexibility of Amazon EC2 without any up-front payment or long-term commitment.
- Apps with short-term, spiky, or unpredictable workloads that can’t be interrupted.
- Apps being developed or tested on Amazon EC2 for the first time.

#### 2\. Reserved pricing is useful for:

- Apps with steady-state or predictable usage.
- Apps that require reserved capacity.
- Users who are able to name upfront payments to reduce their total computing costs even further.

#### Reserved pricing types:

1. **Standard** Reserved Instances: These offer up to 75% off on-demand instances. The more you pay upfront and the longer the contract, the greater the discount.
2. **Convertible** Reserved Instances: These offer up to 75% off on-demand capability to change the attributes of the RIas long as the exchange results in the creation of Reserved Instances are of equal or greater value.
3. **Scheduled** Reserved Instances: These are available to launch within the time windows you reserve. This option allows you to match your capacity reservation to a predictable recurring schedule that only requires a fraction of a day, a week, or a month.

#### 3\. Spot pricing is useful for:

- Apps that have flexible start and end times
- Apps that are only feasible at very low compute prices
- Users with urgent computing needs for large amounts of additional capacity
- If the spot instance is terminated by EC2, you will not be charged for a partial hour of usage. However, if you terminate the instance yourself, you will be charged for any hour in which the instance ran.

#### 4\. Dedicated Hosts pricing is useful for:

- Regulatory requirements that may not support multi-tenant virtualization
- Licensing which doesn’t support multi-tenancy or cloud deployments. Tip: any question that talks about special licensing requirements — talks about dedicated hosts!
- Can be purchased On-Demand (hourly)
- Can be purchased as a Reservation for up to 70% off the On-Demand price

![cupofcode_blog_ec2_pricing](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_ec2_pricing-768x432.png)

### EC2 Hands-on: Creating an EC2 instance and connecting to it

When you want to launch a new EC2 instance, you’ll need to configure some things:

- Region: because this is a per region service.
- Amazon Machine Image (AMI), which provides the information required to launch an instance.
- Instance type: The t2.micro is under the free tier
- add storage — EBS — We will talk about this in more depth soon.
- Security Group — A **security group** acts as a virtual firewall for your **EC2** instances to control incoming and outgoing traffic. _0.0.0.0/0_ everything, _W.X.Y.Z/32_ allows only 1 IP address.  
    Incoming and outgoing traffic is controlled by ports. **Ports are the way computers communicate:** as eyes see, ears hear, so it works with ports: Linux through SSH (port 22), Microsoft through Remote Desktop Protocol (protocol 3389), HTTP through port 80, and HTTPS through port 443.

After you click _launch_, it will create a key pair, which allows us to log into our EC2 machine. After downloading the key pair file `MyPrivateKey.pem` , you need to change permissions on it with:

`chmod 400 MyPrivateKey.pem`

Now you can connect to the ec2 instance through the CLI by typing:

`ssh ec2-user@34.229.236.39 -i MyPrivateKey.pem`

Don’t worry! We will talk about connection through the CLI soon!

**Important note!** If you get a timeout you should check the Security Group permissions!

* * *

## EBS and Storage Options Comparison

![cupofcode_blog_ebs](https://cupofcode.blog/wp-content/uploads/2021/07/intro-to-aws-part-3-7-768x432.png)

Previously, in the hands-on example of the EC2, there was this part:

> _Add storage—EBS—We will talk about this in more depth soon._

Soon is here!

But before we go over the AWS TLA (three letters acronyms), let’s start with the simplest storage option: Instance Storage.

### Instance Storage

[Some Amazon Elastic Compute Cloud (Amazon EC2) instance types](https://aws.amazon.com/ec2/instance-types/) come with a form of directly attached, block-device storage known as the [instance store](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/InstanceStorage.html). The instance store is ideal for temporary storage, because the data stored in instance store volumes is not persistent through instance stops, terminations, or hardware failures.

### EBS: A Virtual Disk in The Cloud

Inside the EC2 service, you will see a tab saying _Elastic Block Store._

EBS allows you to create storage volumes and attach them to EC2 instances. Once attached, you can create a file system on top of these volumes, run a DB, or use them in any other way you would use a block service. EBS volumes are placed in a specific AZ, where they are **automatically replicated** to protect you from the failure of a single component.

#### EBS Storage Types

SSD:

- General Purpose SSD (GP2) — balances prove and performance for a wide variety of workloads.
- Provisioned IOPS SSD (IO1) — Highest-performance SSD volume for mission-critical low-latency or high-throughput workloads.

Magnetic:

- Throughput Optimised HDD (ST1) — Low-cost HDD volume designed for frequently accessed, throughput-intensive workloads.
- Cold HDD (SC1) — Lowest cost HDD volume designed for frequently accessed workloads (File Servers).
- Magnetic — previous generation.

### S3 vs EBS vs EFS Comparison

Well, before we compare them and see their differences, let’s open those three letters acronyms.

**S3 = Simple Storage Service**

We learned about S3 in a [previous blog post](https://cupofcode.blog/aws-iam-cloudfront-s3/), so here is a very short summary:

S3 provides developers and IT teams with **secure, durable, highly scalable object storage**. Amazon S3 is easy to use, with a simple web services interface to store and retrieve any amount of data from anywhere on the web.

- S3 is a safe place to store your files.
- It is object-based storage.
- The data is spread across multiple devices and facilities.

**EFS= Elastic File Storage**

Great for file servers. Multiple ec2 instances can access the same store.

EFS is a **file storage service** for EC2 instances.

It’s easy to use and provides a simple interface that allows you to create and configure file systems quickly and easily.

**S3 vs EBS vs EFS**

1. **S3** is used for storing flat files (objects), like pictures, documents, videos, etc. You do not install an operating system or DB for it.
2. **EBS** is a virtual disk that can be attached to EC2. The size of the disk can be changed, but it’s not done automatically.
3. **EFS** is a virtual disk that can be attached to EC2, and the size of the disk is elastic (scaling up and down depending on usage).

* * *

## Elastic Load Balancer

![cupofcode_blog_load_balancers](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_load_balancers-768x432.png)

Elastic Load Balancing automatically distributes incoming application traffic across multiple targets, such as Amazon EC2 instances, containers, IP addresses, Lambda functions, and virtual appliances. It can handle the varying load of your application traffic in a single Availability Zone or across multiple Availability Zones.

When you click on `Create Load Balancer` you will see there are 3 types (well, now there are 4, including _Gateway Load Balancer_, but it’s not mentioned in any course):

- **Application LB (ALB)**— Application-aware, which means it can see into [_Layer 7_](https://en.wikipedia.org/wiki/OSI_model#Layer_7:_Application_Layer), and make intelligent routing decisions.
- **Network LB (NLB)** — For when you need ultra-high performance and static IP addresses.
- **Classic** — V1, slowly getting phased out. Good for tests and development, or for keeping the costs low.

The Load Balancer configuration contains:

- Security Groups, which we mentioned before.
- Target Groups, which are the groups of EC2 instances it balances.
- Health Checks, with which your Load Balancer assures the instances are healthy and replace them if not.

* * *

## Auto Scaling Groups

![cupofcode_blog_auto_scaling](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_auto_scaling-768x432.png)

Let’s start with the problem and then see the solution. Considering that:

- ALB is by default redundant so spread across multiple AZs.
- Let’s say we have a DB. as you will learn in the next blog post, we have our primary DB and Secondary DB in another AZ.
- we have one EC2 instance.

So our problem is that the EC2 instance is our single point of failure.

The solution is Auto scaling groups. ASG is a group of EC2 instances that scale in/out based on the load, or on health.  
Based on load means keeping many instances when there is much traffic and removing instances when there isn’t much traffic.  
Based on health means that if one of your EC2 instances is down (= doesn’t pass health check) — your ASG will create a new one to replace it.

In the AWS console, under `Auto Scaling` there are 2 tabs: `Launch Configurations` and `Auto Scaling Groups`. you **can’t** create an ASG without a Launch config. Launch config uses AMI (amazon machine image), which is a screenshot of the EC2.

* * *

## Using The Command Line

We had a sneak peek for it in the EC2 hands-on, with:

`ssh ec2-user@34.229.236.39 -i MyPrivateKey.pem`

And now we’ll understand how it works!

Interacting with AWS can be done in 3 ways: AWS Console, [CLI](https://www.w3schools.com/whatis/whatis_cli.asp), and SDKs. **_SDKs_** (Software Development Kits) are a way to communicate with AWS through code, and it’s out of the scope for this blog post.

Connecting to the EC2 instance through the AWS console is pretty straight forward: _ec2->instances->click on it -> connect_.  
But how do you connect through the CLI? This is where we revisit the IAM service from the previous blog post!

### How To Enable Communication Through CLI?

1. AWS Console-> IAM
2. Create a user and give them administrator access.
3. Go to users-> click on your user-> security credentials -> access key -> download.  
    You only get access to download the access key **once**, so if you lost it, you can deactivate and delete it, and then create a new one.
4. Type in the CLI: 

`ssh ec2-user@34.229.236.39 -i MyPrivateKey.pem`

**_MyPrivateKey.pem_** is the security credentials file you downloaded!

### Configure Communication:

AWS communication through the CLI: `aws <SERVICE>`

for example: `aws s3 mb s3://cupofcodeblog` to create a new bucket

But if you try that, you’ll get the error:

make\_bucket failed: s3://cupofcodeblog unable to locate credentials

That’s because you need to configure this EC2 instance to talk to s3, and there are two ways to do that:

1. Give it **credentials** of the administrator IAM user
2. Use **roles**

#### 1\. Configure Credentials:

Type `aws configure` and give it the _access key ID_ and _secret access key_:
```
> aws configure
Access key ID = username
secret access key = password
default region name
default output format = enter.
```
And now, when we try:

`aws s3 mb s3://cupofcodeblog`

We made a bucket! And we can see it when we type `aws s3 ls`.

#### 2\. Using Roles

1\. Create a s3AdminAccess role: Go to AWS Console -> IAM -> Roles. While configuring, you choose that the service that will use this role is EC2.

2\. Attach the role to the specific EC2 instance: Go to instances -> tick the one -> actions -> instance settings -> attach or replace IAM role

Now you can ssh into the instance and create a bucket without any problem.

#### Why Are Roles Better?

Roles are a much safer option than saving credentials on the ec2 instance. With Roles, if someone hacks your ec2 instance —  they’ll still have administrative access from this ec2 instance into S3, **but** they won’t have full admin access to the AWS environment. That means that when we delete the hacked instance — they lost access to the AWS env.

Roles are also easier to manage, simply by adding or modifying policies.

Important note: Roles, like IAM Users, are global and not regional.

* * *

That’s it! Wasn’t so hard, was it?

![cupofcode_blog_ec2_meme3](https://cupofcode.blog/wp-content/uploads/2021/07/cupofcode_blog_ec2_meme3-768x414.jpg)

Today we learned about EC2, with all its types and pricing models. We also learned about four storage options and the differences between them. We learned about load balancers and auto-scaling groups and finished with communication through the CLI.

* * *

If you read this, you’ve reached the end of the blog post, and I would love to hear your thoughts! Was I helpful? Do you also use AWS at work? Here are the ways to contact me:  
Facebook: [https://www.facebook.com/cupofcode.blog/](https://www.facebook.com/cupofcode.blog/)  
Instagram: [https://www.instagram.com/cupofcode.blog/](https://www.instagram.com/cupofcode.blog/)  
Email: cupofcode.blog@gmail.com

![[https://cupofcode.blog/](https://cupofcode.blog/)](https://cdn-images-1.medium.com/max/3170/0*vLdjjgtBfeBxj_f8.png)