Amazon EC2 Basics

Virtual servers in the cloud: instances, AMIs, instance types, and how the hypervisor shares hardware.

What is it?

Amazon EC2 (Elastic Compute Cloud) rents you virtual servers called instances. You pick the operating system, size and network placement. You manage everything from the OS up.

  • AMI (Amazon Machine Image): a template with an operating system and optional software. Launching an instance means 'start a server from this image'.
  • Instance type: the hardware shape, written like m7g.large: family and generation (m7g) plus size (large).
  • Key pair / Systems Manager: ways to log in securely.
  • User data: a script that runs on first boot to set up the server.

Instance families group types by what they are good at:

  • General purpose (M, T): balanced CPU/memory, the usual starting point. T types are burstable. Also includes Mac instances for macOS.
  • Compute optimized (C): batch processing, game servers, high-performance web.
  • Memory optimized (R, X): in-memory databases and large caches.
  • Storage optimized (I, D): high local disk throughput, data warehouses.
  • Accelerated computing (P, G, Inf, Trn): GPUs and ML chips for training, inference, graphics.

For CLF-C02, know these five categories, not every letter.

Many instances share one physical host: multi-tenancy. A hypervisor (AWS uses the Nitro system) splits the host's CPU, memory and storage and keeps each tenant isolated.

EC2 is often called the backbone of AWS because many services (RDS, for example) run on EC2 behind the scenes. For a simple virtual server with a bundled plan, say a WordPress site with a domain, Amazon Lightsail is the easy version of EC2.

The AWS Nitro System underpins all modern instance types. It pairs dedicated hardware with a very light hypervisor:

  • Nitro Cards take networking, EBS storage and instance storage work off the host CPU.
  • Nitro Security Chip on the motherboard protects the hardware.
  • Nitro Hypervisor only allocates CPU and memory, giving near bare-metal performance.
  • Nitro Enclaves create an isolated area inside an instance for very sensitive data (beyond CLF-C02 depth).
  • Bare metal sizes (ending in .metal) have no hypervisor; your software runs straight on the hardware.

Sizes and vCPUs: an instance type is family + size. Sizes go nano, micro, small, medium, large, xlarge, 2xlarge and up. Each step roughly doubles vCPU, memory and price. A vCPU is a share of the host's processors, not a whole physical CPU. Adding another instance of the same size (scaling out, ideally in another AZ) is often better for availability than moving to a bigger one.

Processors and accelerators

  • Chips are x86 (Intel or AMD) or Arm, such as AWS Graviton. Graviton usually gives better price-performance and energy efficiency, if your software supports Arm.
  • AWS also designs Inferentia (ML inference) and Trainium (ML training) chips.
  • GPUs have thousands of small cores for parallel work like graphics and ML training. The P and G families offer NVIDIA GPUs.

AMIs in more depth

  • AMI IDs are Region-specific: the same image has a different ID in each Region.
  • Sources: Quick Start (AWS-supported, such as Amazon Linux 2023, Ubuntu, Red Hat, Windows), AWS Marketplace (vendor images, often paid), community AMIs, and your own custom AMIs made from a configured instance (Create image) to relaunch the same server later.
  • Launch templates save launch settings (AMI, type, key pair, security groups, IAM instance profile, tags, user data) with versions. They replace the deprecated launch configurations.

What you set at launch: AMI, instance type, network (VPC, subnet, public IP), IAM role (attached via an instance profile), storage (EBS volumes; turn on encryption, it costs nothing extra), tags, a security group, a key pair and optional user data. Security group names cannot be changed later. Open only the ports you need (for example HTTP 80 for a public website).

Connecting to an instance

  • SSH with a key pair: AWS gives you the private key (.pem) once. Lock it down with chmod 400. The default user on Amazon Linux is ec2-user.
  • EC2 Instance Connect: a shell in the browser, no key file to manage.
  • Systems Manager Session Manager: no open inbound port, no key pair, and sessions are logged. Needs the SSM agent and an instance profile with the AmazonSSMManagedInstanceCore policy.
  • Windows instances use RDP. You decrypt the administrator password with the key pair's private key.

Lifecycle details

  • Wait for both status checks (system and instance) to pass before using a new instance.
  • A reboot keeps the public IP. A stop and start gives a new public IPv4 address.
  • An Elastic IP is a static public IPv4 address you allocate to your account and attach to an instance, so its address survives stop/start.
  • AWS charges for all public IPv4 addresses. Terminating an instance does not release its Elastic IP, so release ones you no longer need.
  • Launches can sometimes fail with insufficient capacity, because an AZ has a limited number of servers of each type.

Tenancy

  • Shared: the default, multi-tenant.
  • Dedicated Instance: hardware used only by your account, still virtualized, billed per instance, no host visibility.
  • Dedicated Host: a whole physical server you control, billed per host, showing sockets and cores, with instance placement and affinity. The choice for bring-your-own-license software licensed per socket or core, such as Microsoft SQL Server.

Windows on AWS goes beyond EC2: license-included Windows Server AMIs, SQL Server on RDS, AWS Directory Service (managed Microsoft Active Directory), Amazon FSx for Windows File Server, AWS License Manager and Amazon WorkSpaces Windows desktops.

Explain like I'm 10

A physical server is an apartment building. The hypervisor is the building manager who splits it into flats with locked doors: each tenant has their own space and cannot peek next door. An AMI is a furnished floor plan you can copy into any new flat.

Examples

Launch an instance

aws ec2 run-instances \
  --image-id ami-0123456789abcdef0 \
  --instance-type t3.micro \
  --count 1 \
  --key-name my-key \
  --security-group-ids sg-0abc1234 \
  --subnet-id subnet-0abc1234 \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'

aws ec2 describe-instances \
  --filters Name=tag:Name,Values=web-1 \
  --query "Reservations[].Instances[].[InstanceId,State.Name]" --output table

aws ec2 terminate-instances --instance-ids i-0123456789abcdef0

Replace the placeholder IDs with ones from your account. Terminate when done so billing stops.

User data bootstrap script

#!/bin/bash
dnf install -y nginx
echo "hello from $(hostname)" > /usr/share/nginx/html/index.html
systemctl enable --now nginx

Runs once at first boot, so a fresh instance configures itself.

How it works

At launch, EC2 picks a physical host in the AZ that fits the instance type, attaches a root volume built from the AMI, and starts the VM. Instance states: pending, running, stopping/stopped and terminated. You pay for compute while running. Stopped instances stop compute billing, but attached EBS volumes still cost money.

Storage is either an EBS network volume (lives on independently) or an instance store (physically attached, lost when the instance stops or terminates). The instance sits in a VPC subnet, with traffic filtered by security groups.

   AMI (OS + software)
        |
        v  launch
 +---------- Physical host (one AZ) -----------+
 |   Hypervisor (Nitro)                        |
 |   +---------+  +---------+  +---------+     |
 |   | Inst. A |  | Inst. B |  | Inst. C |     |
 |   +---------+  +---------+  +---------+     |
 |    (each may belong to a different customer)|
 +---------------------------------------------+

Why does it exist?

Lots of software expects a normal server: a certain OS, installed packages, long-running processes. EC2 gives you that control with the speed and billing of the cloud. Useful when you cannot, or do not want to, rewrite for managed services.

When to use it

Use EC2 for lift-and-shift apps, software that needs OS-level control, custom networking or licensing, long-running servers, and special hardware like GPUs.

When not to use it

For short event-driven code, Lambda is simpler. For containers, ECS/EKS with Fargate removes server management. If a managed database fits, use RDS instead of running one yourself on EC2.

Common mistakes

  • Leaving instances running after experiments.

  • Opening SSH to the entire internet rather than restricting the source.

  • Storing important data on instance store volumes.

  • Oversizing 'just in case' instead of right-sizing from metrics.

  • Treating servers as pets that cannot be rebuilt; bake an AMI or use user data so you can replace them.

  • Copying an AMI ID from one Region and using it in another - AMI IDs are Region-specific.

  • Relying on a public IP that changes after stop/start; use an Elastic IP or DNS if the address must stay fixed.

  • Leaving unused Elastic IPs allocated - public IPv4 addresses are charged and terminating the instance does not release them.

  • Forgetting to open the port in the security group, then wondering why the web server is unreachable.

  • Storing access keys on an instance instead of attaching an IAM role through an instance profile.

  • Picking an Arm (Graviton) instance for software that only ships for x86.

Practice exercises

  1. Easy:

    Decode the instance type c7g.xlarge: what do the letter, number, extra letter and size tell you?

  2. Easy:

    Pick an instance family for (a) a large in-memory cache, (b) video encoding, (c) a small blog.

  3. Medium:

    Write user data that installs a web server and serves a page showing the instance's hostname.

  4. Easy:

    List three ways to get a shell on a Linux instance and say which needs no open inbound port.

  5. Medium:

    Create a launch template for a web server (AMI, type, security group, user data) and explain why it beats typing the settings each time.

  6. Hard:

    Explain what is lost, kept, and billed when you stop vs terminate an instance with one EBS root volume and one instance-store volume.

Interview questions

What is an AMI?

A template with an operating system and optional software used to launch EC2 instances.

What does multi-tenancy mean for EC2?

Multiple customers' instances share one physical host, kept isolated by the hypervisor.

Which instance family suits memory-intensive workloads?

Memory optimized (R and X families).

Exam-style: A company needs full control of the OS for a legacy application. Which compute service fits best?

Amazon EC2.

Exam-style: Who is responsible for patching the operating system on an EC2 instance?

The customer; AWS manages the underlying host and hypervisor.

Exam-style: Which service offers simple, bundled virtual servers for users new to the cloud? (A) EC2 (B) Lightsail (C) Lambda (D) Outposts

B. Amazon Lightsail.

Exam-style: Which feature keeps an instance's public IPv4 address the same after stop and start?

An Elastic IP address.

Exam-style: A company must use existing per-core software licences and see the physical sockets and cores. Which tenancy? (A) Shared (B) Dedicated Instance (C) Dedicated Host (D) Spot

C. Dedicated Host.

What is the AWS Nitro System?

The hardware and lightweight hypervisor platform behind modern EC2 instances: Nitro Cards offload networking and storage, the Nitro Security Chip protects hardware, and the Nitro Hypervisor allocates CPU and memory.

Which connection method needs no SSH key and no open inbound port, and logs sessions?

AWS Systems Manager Session Manager.

What is a launch template?

A saved, versioned set of instance launch settings (AMI, type, security groups, role, user data) used by people, Auto Scaling and other services; it replaces launch configurations.

Exam-style: Which AWS-designed processor gives Arm-based general-purpose instances with good price-performance?

AWS Graviton.