AWS EC2 The Complete Guide

Learn everything you need to know to get started with Amazon Elastic Compute Cloud (Amazon EC2). We'll explore what it is, how it works, how much it costs, and much more.

What is EC2?

EC2, or Elastic Cloud Compute, is one of the most popular and well-known services provided by AWS. The service provides access to virtual machines on the hardware within Amazon data centers. In simple terms, EC2 is a service that allows users to get access to computers to run the applications they desire. Through various payment methods, the user effectively "rents” a certain amount of computing power from AWS through EC2.

EC2 Key Takeaways

  • EC2 Instances are virtual machines that are provisioned via AWS - through the console, or programmatically.
  • EC2 Instances fall under 6 categories - General Purpose, Compute Optimized, Memory Optimized, Accelerated Computing, Storage Optimized, and HPC Optimized.
  • EC2 Instances are specified via AWS-defined nomenclature that dictates the number of vCPUs, Memory, storage, bandwidth, etc.
  • EC2 Instances are closely tied to volumes specified under other AWS services (Ex: EBS, EFS)
  • EC2 Instances can be paid for via various purchasing options. These options range from by the second to reserving and instance for a period of 3 years. By choosing the option that matches the requirements of the application, the user can benefit from substantial savings.
  • EC2 Instances are closely tied to Secturity Groups which dictate how traffic flows into and out of a given EC2 Instance.

Creating an EC2 Instance in AWS

In this short section, we will create an EC2 Instance on AWS.

Step 1 - Navigate to the EC2 Console

1.1 - From the AWS Console, click on “Services.”

1.2 - From the drop-down menu, click on “Compute.”

1.3 - From the side menu, click on “EC2.”

You should now be on the EC2 Console.

Figure 2.1 - AWS EC2 | Navigating to the EC2 Dashboard on AWS

Step 2 - Launch a new EC2 Instance

You can launch a new EC2 Instance directly from the Dashboard by pressing the “Launch instance” button. However, it’s typically done from the “Instances” page, that can be accessed via one of the two buttons on the Dashboard:

[Option 1] 2.1 - From the EC2 Dashboard, click on “Launch instance.”

[Option 2] 2.2 - From the EC2 Dashboard sidebar, click on “Instances.”

[Option 3] 2.3 - From the Resources panel, click on “Instances (running).”

If you’ve clicked “Launch instance,” go to Step 4. If you’ve chosen to navigate to the “Instances” page, follow the steps in Step 3 below.

Figure 2.2 - AWS EC2 | Launching a new EC2 Instance OR navigating to the “Instances” page

Step 3 - [Optional] Launch a new EC2 Instance via “Instances”

The “Instances” page will display all the running EC2 instances and give you visibility into their parameters, metrics, and options.

3.1 - From the Instances page, click on “Launch instances.”

Figure 2.3 - AWS EC2 | Launching a new EC2 Instance from the “Instances” page

Step 4 - Configure the EC2 Instance

4.1 [Optional] - Type a name for your instance.

4.2 - Select the OS for your Instance

4.3 - Select the AMI for your Instance

Note: We will cover AMIs (Amazon Machine Images) in a separate tutorial. If you’re new to AWS, we recommend that you choose “Amazon Linux” as the OS, and one of the “Free tier eligible” AMIs to get started. It’s important to note that you will be billed based on the OS & AMI you select at this stage.

4.4 - Choose an Instance type.

Note: We cover Instance types for various EC2 instances further in this tutoriall. If you’re new to AWS, we recommend that you choose “t2.micro” as the instance type as the “Free tier eligible” instance type to get started. It’s important to note that you will be billed based on the instance type you select at this stage.

4.5 - Choose a Key pair for your instance.

The key pair allows the instance to stay protected from unauthorized access. Every key pair is unique and by creating one for your instance and yourself, you can ensure that the only ones who can access the instance are the ones with the key provided by you (admin).

4.6 - Choose a volume that will be attached to your instance.

There are many options when it comes to volumes; we’ll cover them in a separate tutorial. However, at this stage, you’ll be offered two main options - EBS and EFS. If you’re new to AWS, we recommend that you choose “EBS - gp3 with up to 8 GiB” as this volume falls under “Free tier eligible”. It’s important to note that you will be billed based on the volume type you select at this stage.

By clicking on “Show details,” you’ll be presented with other options for your volume: Size, Volume type, IOPS, Delete on termination, Encrypted, and Throughput. These concepts will be covered in-depth under the EBS section.

Note: you may attach multiple volumes to a single instance by pressing the “Add new volume” button.

4.7 - Review the Settings & Launch Instance

At this point, you should be able to see all the options you’ve selected in the steps above. You can revisit any of the steps prior to committing to the instance. It’s important to note that many of these settings can’t be changed on an instance that has already been launched. If you launch an instance that has the wrong configuration, you’ll most likely (not always) have to terminate the instance and re-create a new one with the correct settings.

Step 5 - Wait for the Instance to Launch

The AWS interface will bring you back to the EC2 Instances page once you launch your instance. The instance you’ve launched will take a moment to be accessible (typically, the process takes 1-2 minutes). The instance will be listed in the list presented to the user on this page.

Terminating an EC2 Instance in AWS

Once an instance has been launched, billing will commence. Although there are other ways to provision an instance and different billing structures than the “default” one, it’s important to remove any unutilized ressources from your infrastructure. In this section, we’ll briefly cover how to terminate an EC2 Instance that is currently “Running.”

Step 1 - Terminate an EC2 Instance

1.1 - From the EC2 Dashboard, click on “Instances.”

1.2 - From the list of Instances, choose the Instance(s) you want to terminate.

1.3 - From the top bar, click on “Instance state.”

1.4 - From the drop-down menu, click on “Terminanate instance.”

Step 2 - Confirm Volume Deletion

In general, a volume is tied to an instance. There are scenarios in which they aren’t, but usurs typically want to be able to acces their volumes from a certain instance. That being said, during the deletion process of an instance, it’s possible to terminate the associated volumes as well. By default, an EBS volume that was created at the same time as an EC2 instance will be terminated at the termination of that instance.

2.1 - Click on “Terminanate.”

Note that after termination, the EC2 Instance will remain in the list of instances on the “Instances” page. Verify that the “Instance state” column properly displays the state of the terminated instance as “Terminated.” after a short period of time.

EC2 Instance Options Explained

Various parameters of EC2 instances can only be modified during the creation process; some of them can be changed on a live instance. In this section, we’re going to review the options available for creating an EC2 instance.

EC2 Instance Operating System [OS]

The currently available OS options for EC2 instances are as follows:

Linux

Amazon, Linux Ubuntu, Red Hat, SUSE Linux, Debian.

[NOTE] It’s important to note that the user is given the option to select from a variety of OS versions & optimizations (Ex: Ubuntu OS allows for v22.04 LTS, v20.04 LTS, Server, SQL Server, etc.)

Windows Server

2016
2019
2022

[NOTE] It’s important to note that the user is given the option to select from a variety of Windows server versions & optimizations.

macOS

macOS Ventura
macOS Monterey
macOS Big Sur

EC2 Instance Types

As the EC2 service evolved, AWS noticed a need for specialized instances. These instance types are adapted to a specific use-case and offer an advantage over the “General purpose instances” in specific ways.

01

General Purpose instances

These instances are designed to provide a balance of compute, memory, and networking resources. They are suitable for a wide range of applications, including web servers, development environments, and small to medium-sized databases.

General Purpose instances
02

Compute optimized instances

Compute Optimized instances are designed specifically for compute-bound applications that require high-performance processors. They are optimized for compute-intensive workloads and can deliver cost-effective high performance at a low price per compute ratio.

Compute Optimized instances
03

Memory optimized instances

Memory Optimized instances are designed for workloads that demand large amounts of memory. They are optimized to deliver fast performance for workloads that process large data sets in memory.

04

Storage optimized instances

Storage Optimized instances are designed for workloads that require high, sequential read and write access to very large datasets on local storage. They are optimized to deliver high I/O performance.

05

Accelerated computing instances

Accelerated Computing instances are designed to perform advanced computation tasks using hardware accelerators, or co-processors. These instances are optimized for graphics processing units (GPUs) or field programmable gate arrays (FPGAs) to provide high levels of floating-point performance, as well as graphics processing.

06

High Performance Computing [HPC] optimized instances

HPC Optimized instances are designed specifically for high-performance computing (HPC) applications. These instances offer the highest level of performance and are optimized for compute-intensive, memory-intensive, and network-intensive workloads.

EC2 Instance Sizes

The instance size will dictate multiple parameters of the EC2 instance - number of vCPUs, Memory, Instance Storage types allowed, Network Bandwidth, and EBS Bandwidth. Below’s a general list of sizes you’ll encounter when provisioning an EC2 instance. Note that not all sizes will be available for all instance types.

  • medium
  • large
  • xlarge
  • 2xlarge
  • 4xlarge
  • 8xlarge
  • 12xlarge
  • 16xlarge
  • Metal

EC2 Nomenclature - Putting it all together

Based on the sections above, we can put together multiple variations / flavours of EC2 instances. In this section, let’s take a look at a few examples:

c6i.8xlarge

  • vCPU count - 32
  • Memory (GiB) - 64
  • Instance Storage (GB) - EBS-Only
  • Network Bandwidth (Gbps) - 12.5
  • EBS Bandwidth (Gbps) - 10

g5.xlarge

  • GPU count - 1
  • GPU Memory (GiB) - 24
  • vCPU count - 4
  • Memory (GiB) - 16
  • Instance Storage (GB) - 1 x 250 NVMe SSD
  • Network Bandwidth (Gbps) - Up to 10
  • EBS Bandwidth (Gbps) - Up to 3.5

m5.metal

  • vCPU count - 96
  • Memory (GiB) - 384
  • Instance Storage (GB) - EBS-Only
  • Network Bandwidth (Gbps) - 25
  • EBS Bandwidth (Gbps) - 19,000

EC2 Purchasing / Reservation Options

AWS offers flexible options of purchasing and deploying EC2 instances that cater to different application types, industries, etc. An organization may significantly benefit from committing to the right option based on their needs, loads, users, etc. In this section, we’ll explore these options, provide some of the constrains, and give you a starting point in selecting the right approach for you.

On-Demand Instances

On-Demand instances are typically used for short-term workloads, come with predictable pricing, and are billed by the second.

  • Billing starts as soon as the instance is created.
  • Billing ends as soon as the instance is terminated.

Reserved Instances

Reserved instances provide an opportunity to lock-in a lower price on a committed contract. In other words, the user is choosing to prepay for a certain period of time which results in a discount that increases with the length of the commitment. Furthermore, the user can choose the terms of their payment - No Upfront, Partial Upfront, and All Upfront. Paying upfront results in higher discounts.

  • Instances can be reserved for a period of 1 or 3 years.
  • Billing starts as soon as the contract is confirmed.
  • Billing ends at the end of the contract period.

Special - Convertible Reserved Instance

  • An option for the user to change the EC2 instance type, family, Operating System, Scope, and Tenancy.

Savings Plans

The user chooses to commit to a certain amount of usage instead of a specific instance. Based on the contract, the user is able to lock in a limited amount of usage for a specific timeframe.

  • Usage can be reserved for a period of 1 or 3 years.
  • Billing starts as soon as the contract is confirmed.
  • Billing ends at the end of the contract period or if the usage limit has been exceeded for the billing cycle.

Spot Instances

Spot instances provide the most aggressive discounts for EC2. The user will place a maximum bid they’re willing to pay for an instance. If, at any point in time, the current price of an instance exceeds the bid of the user, he will lose that instance. In other words, if there’s less demand, the user may benefit from up to a 90% discount, but bares the risk of losing that instance at any point in time.

  • Billing starts as soon as the contract is confirmed.
  • Billing ends as soon as the current bid is higher than what the user has bid.
  • The instance is terminated immediately.

Dedicated Hosts

This option provides access to a physical server aimed at compliance requirements. This option has two sub-options - On-demand and Reserved.

On-demand

  • Billing starts as soon as the instance is created.
  • Billing ends as soon as the instance is terminated.

Reserved

  • Billing starts as soon as the contract is confirmed.
  • Billing ends at the end of the contract period.

Choosing The Correct Option

As discussed at the start of this section, making the right choice can result in a lot of savings. Choosing the incorrect option may result in system downtime. Therefore, it’s important to understand the differences between these options and the application at hand.

Example 01

You’re asked to architect the most cost-effective structure for a data analysis application that does not require real-time response.

Solution 01

Spot Instances are ideal for workloads that are resilient to failure. In other words, if you can launch applications at any point in time and benefit from the highest discount, they’ll offer the most cost-effective solution that fits the application.

Example 02

You’re asked to architect the most cost-effective structure for a database that requires resiliency and access 24/7.

Solution 02

If the company is planning to push this database instance in production for a foreseeable future, the most cost effective solution would be a reserved instance. If the project is experimental and the business need is to decommission the application at the end of a development cycle (ex: 2 weeks), you may select the on-demand option.

EC2 Security Groups [SG]

During the creation process of an EC2 instance, the user is required to specify the security group(s) the instance belongs to. Security Groups control how traffic is allowed into / from EC2 instances and other AWS resources. It’s important to note that Security Groups only contain allow rules; in other words, you can’t explicitly restrict traffic. Security Groups will reference an instance via IP addresses or other Security Groups (Ex: a local SG can inherit the rules of a global SG).

At a fundamental level, a Security Group will allow of block traffic from various sources into the EC2 instance. The figure below illustrates a basic example in which a PC with a specific IP address can access the EC2 instance while the PC with a different IP address is blocked.

Security Group Specifics

  • Security Groups can be attached to multiple instances. In other words, you can specify and apply a set of traffic rules to many instances. As you update the rules of this Security Group, they’ll be applied across the instances to which they’re associated.
  • Security Groups are specified by region. In other words, you’ll have to create a separate set of Security Groups in a different region if you need the same set of rules.
  • Security Groups are external to EC2 instances. In other words, if a Security Group blocks traffic, the EC2 instance won’t have any record of the packet or payload.
  • Default SG Configuration - All inbound traffic is blocked and all outbound traffic is authorized.

Most Important Security Group Protocols

Although this isn’t a networking tutorial, it’s important to note that the user will need to configure an array of ports that are standard to the industry. We encourage you to take the time and understand IP addresses, Ports, Subnet Masks, and other concepts from other sources. Here’s a list of most utilized ports you should be familiar with:

  • Port 21 | FTP (File Transfer Protocol) - Upload files into a file share
  • Port 22 | SSH (Secure Shell) - Login into a Linux instance
  • Port 22 | SFTP (Secure File Transfer Protocol) - Upload files via SSH
  • Port 80 | HTTP (Hypertext Transfer Protocol) - Unsecured website access
  • Port 443 | HTTPS (Secure Hypertext Transfer Protocol) - Secured website access
  • Port 3389 | RDP (Remote Desktop Protocol) - Login into a Windows instance

Hands-On EC2 Training

In order to deploy software, configure, and monitor an EC2 instance, you’ll need to connect to it. Multiple options are available to users that need to connect to EC2 instances. Depending on your requirements and OS, select one of the following tutorials that will walk you through the steps of connecting.

EC2 - Access via Instance Connect
Read the tutorial
Understanding & Creating AWS AMIs
Read the tutorial
EC2 - Access via SSH - Mac OS
[Coming Soon]
EC2 - Access via SSH - Windows
[Coming Soon]

EC2 Knowledge Check