Skip to content
Tutorials Center
My learning

Topics

Lesson 10 of 70 ยท Compute with EC2

EC2 Instances

Understand Amazon EC2: instances, AMIs, instance type families and names, key pairs, user data and the instance lifecycle, with a runnable state machine.

Amazon EC2 (Elastic Compute Cloud) is the service that rents you virtual servers by the second. You pick an operating system image, a size, a network and a disk, press launch, and a minute later you have a machine that you can log into and use like any server. When you are done you stop or terminate it, and the billing for compute stops with it.

EC2 is the base of much of AWS. Load balancers, auto scaling, containers on EC2 and managed databases all run on instances somewhere. Learning how an instance is built and how it behaves during its life makes the later lessons easier. This lesson covers the building blocks. The next ones cover storage, pricing, access and firewalls and images and metadata.

You cannot launch real instances from this site. The CLI commands below are read-only examples. The runnable examples are Python stand-ins: an instance state machine and an instance-type name decoder.

What you choose when you launch

An instance is one virtual machine. To launch one you answer a handful of questions:

Choice What it means
AMI The Amazon Machine Image: the template with the operating system and installed software. Examples: Amazon Linux, Ubuntu, Windows Server, or an image your team built.
Instance type The hardware shape: number of vCPUs, memory, network speed and optional extras such as GPUs.
Key pair A public key placed on the instance so you can log in with the matching private key.
Network The VPC, subnet (and therefore the Availability Zone) and security groups.
Storage The root volume (usually EBS) and any extra volumes.
IAM instance profile The role the instance's software uses to call AWS APIs.
User data A script or cloud-init configuration that runs on first boot.

An instance lives in one Availability Zone. If that zone fails, the instance goes with it, so resilient designs use several instances in several zones, as you will see in Auto Scaling Groups. Networking choices are explained in VPC Fundamentals.

AMIs

An AMI is a snapshot-like template of a root volume plus launch settings. AWS publishes AMIs for common operating systems, the AWS Marketplace offers vendor images, and you can create your own from a configured instance. AMIs are regional: an image in eu-west-1 is not automatically available in us-east-1, although you can copy it.

Use an official or well-maintained AMI, keep it patched, and prefer images that are rebuilt regularly over images that were hand-configured once. In practice many teams find the latest Amazon Linux AMI through a public parameter instead of hard-coding an ID (read-only example):

Terminal
aws ssm get-parameter \
  --name /aws/service/ami-amazon-linux-latest/al2023-ami-kernel-default-x86_64 \
  --query Parameter.Value --output text

Instance types and families

An instance type name has a pattern: family, generation, optional attribute letters, a dot and a size. For example m7g.xlarge is the m family, generation 7, with the g attribute (Graviton processors), in size xlarge.

The common families:

Family Typical use Notes
t (for example t3, t4g) Small sites, dev machines, bursty loads Burstable: a low baseline of CPU that you can exceed using credits.
m General purpose Balanced CPU and memory. A good default.
c Compute optimized More CPU per GB of memory: batch jobs, encoding, web tiers that are CPU bound.
r, x Memory optimized Large caches, in-memory databases, big analytics.
i, d Storage optimized Fast local NVMe disks for databases and search.
g, p, inf, trn Accelerated computing GPUs and AI chips for graphics, training and inference.

Common attribute letters: g means AWS Graviton (Arm) processors, a means AMD, i means Intel, d adds local NVMe storage, n means enhanced networking and e means extra memory or storage. Newer generations are usually faster per dollar, so use the latest generation your software supports. Arm instances (Graviton) are often cheaper for the same work but need software built for Arm.

Sizes grow in steps. For the m, c and r families, large has 2 vCPUs and each step up doubles it: xlarge has 4, 2xlarge has 8, 4xlarge has 16. Memory per vCPU is fixed within a family: about 2 GiB for c, 4 GiB for m and 8 GiB for r. Burstable t instances break this pattern, so check the documentation for the exact numbers when it matters.

The first stand-in decodes names using these rules, which makes the naming scheme concrete. It only knows the m, c and r patterns above.

Python Decoding instance type names

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

For m7g.xlarge this gives 4 vCPU and 16 GiB, matching AWS's published numbers. Always confirm the figures for the instance you actually pick in the console or with aws ec2 describe-instance-types.

The specialized families

Beyond t, m, c and r, AWS sells families built for one job. You do not need them for a web server, but you will meet them in machine learning, analytics and licensing discussions.

Family What makes it special Typical use
p (P4d, P5) Large NVIDIA GPUs with fast interconnects Training deep-learning models, HPC
g (G5, G6) Smaller GPUs Graphics, video encoding, ML inference
inf, trn AWS's own Inferentia (inference) and Trainium (training) chips, used with the Neuron SDK Cheaper ML inference and training when your framework supports them
f1 Programmable FPGAs: you load custom hardware logic Genomics, financial risk, specialized signal processing
i (I3, I4i, I3en) Very fast local NVMe SSDs NoSQL databases, search, data warehouses that need low-latency disk
x (X1e, X2gd, X2idn) Extreme memory, 16 to 32 GiB or more per vCPU In-memory databases, SAP HANA, big caches
z1d The highest sustained clock speed per core, plus local NVMe Software licensed per core, electronic design automation

Do not confuse the letter g in two places. At the start of a name (g5) it is the GPU graphics family. After the generation number (m7g, c7g) it is an attribute meaning Graviton processors.

The second stand-in is a tiny chooser that maps a workload description to a family, following the table.

Python Which family for this workload?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Launching an instance

In the console, the "Launch instance" wizard asks for each item in the table above. The same launch with the CLI looks like this (read-only; the IDs are placeholders):

Terminal
aws ec2 run-instances \
  --image-id ami-0123456789abcdef0 \
  --instance-type t3.micro \
  --key-name my-key \
  --subnet-id subnet-0abc1234 \
  --security-group-ids sg-0abc1234 \
  --iam-instance-profile Name=web-profile \
  --user-data file://bootstrap.sh \
  --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=web-1}]'

Then you can watch it come up:

Terminal
aws ec2 describe-instances --instance-ids i-0abc123 \
  --query 'Reservations[].Instances[].State.Name'

Teams rarely click through the wizard for production. They define launch templates, auto scaling groups or infrastructure as code (see CloudFormation) so that every instance is identical and reproducible.

Key pairs and user data

A key pair gives you SSH access to Linux instances: AWS puts the public key on the instance and you keep the private key (a .pem file) safe. Whoever has the private key can log in, so never commit it to Git. Newer approaches such as Session Manager avoid SSH keys entirely; see Connecting to Instances and Security Groups.

User data is a script (or a cloud-init document) that runs once, at the first boot. It installs packages and writes configuration so that a plain AMI turns into your server:

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

User data is not a secret store: anyone with access to the instance can read it. More about it is in AMIs, User Data and Instance Metadata.

The instance lifecycle

An instance moves through states, and what you pay for depends on the state.

stateDiagram-v2
    [*] --> pending: run
    pending --> running
    running --> stopping: stop
    stopping --> stopped
    stopped --> pending: start
    running --> shutting_down: terminate
    stopped --> shutting_down: terminate
    shutting_down --> terminated
    terminated --> [*]

Lifecycle of an EBS-backed instance. A reboot keeps the instance in the running state.

  • pending: AWS is placing and starting the instance.
  • running: the instance works and compute billing is active.
  • stopping / stopped: the OS has shut down. You are not billed for compute while stopped, but you still pay for the attached EBS volumes and any Elastic IP address that is not in use. A stopped instance can be started again, possibly on different hardware, and an automatically assigned public IPv4 address is released.
  • shutting-down / terminated: the instance is deleted and cannot be restarted. Its root volume is deleted by default (a setting you can change), and data on instance store disks is gone. Terminated instances stay visible in the console for a short while.
  • Rebooting restarts the operating system without leaving the running state, keeps the public IP, and preserves instance store data.

Termination protection is an option that stops accidental termination through the console or API. Hibernate (for supported types) saves memory to the root volume so the instance can resume with its RAM state.

The second stand-in models these states and rules. It rejects invalid actions, as the real API does, and advances transient states with a tick.

Python An EC2 instance state machine

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that the public IP changes after the stop and start, and that billing drops to "EBS only" while stopped. Both are things that surprise newcomers. If you need an address that never changes, you attach an Elastic IP or put a load balancer in front, and you avoid depending on the instance's own public address.

Placement groups, Elastic IPs and stop, hibernate or terminate

Three more choices come up as soon as you run more than one instance.

Placement groups

By default AWS decides where your instances run. A placement group lets you influence that, trading one property against another:

Strategy What it does Use it for Limits
Cluster Packs instances close together in one Availability Zone, on the same high-bandwidth network segment HPC, tightly coupled jobs, anything that needs the lowest latency between instances One zone, so a zone failure affects all of them. Use one instance type, and launch all instances together to avoid capacity errors.
Spread Puts each instance on distinct hardware A small number of critical instances that must not fail together (for example a few primary nodes) At most 7 running instances per Availability Zone per group
Partition Splits instances into partitions, each on its own set of racks, with no shared hardware between partitions Large distributed systems such as Hadoop, Cassandra and Kafka that are rack-aware Up to 7 partitions per zone. You can see which partition an instance is in.

Create a group and launch into it (read-only):

Terminal
aws ec2 create-placement-group --group-name hpc-nodes --strategy cluster
aws ec2 run-instances --image-id ami-0abc1234 --instance-type c7g.4xlarge \
  --count 4 --placement GroupName=hpc-nodes

The rule of thumb: cluster for speed, spread or partition for safety.

Elastic IP and the other public addresses

An instance can have a public IPv4 address that AWS assigns automatically. It is released when you stop the instance, so it changes after each stop and start. An Elastic IP is a static public IPv4 address that belongs to your account until you release it. You attach it to an instance or network interface, and you can remap it to another instance in seconds, which is a simple failover trick. Keep two things in mind:

  • AWS now charges for every public IPv4 address, in use or idle, so release Elastic IPs you do not need. See Cost Management.
  • For a web application a load balancer plus a DNS name is better than a pinned address. See Elastic Load Balancing and Route 53.

Stop, hibernate or terminate?

Stop Hibernate Terminate
What happens The OS shuts down, the instance can be started again RAM is saved to the root volume, then it stops; start resumes processes where they were The instance is deleted
Compute billing Stopped Stopped Stopped (it is gone)
EBS billing Continues Continues, and the root volume must hold the RAM contents Root volume deleted by default
Memory (RAM) Lost Preserved Lost
Instance store data Lost Lost Lost
Public IP Released (unless Elastic IP) Released (unless Elastic IP) Gone
Requirements EBS-backed instance Supported instance types, encrypted EBS root volume, RAM within the supported limit None (termination protection can block it)

Hibernation helps for long-running warm-up processes, such as an application that takes ten minutes to load a cache. It does not work for instance-store-backed instances, and a hibernating instance can only stay in that state for a limited number of days.

Python Choose the right instance action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good habits

  • Name and tag every instance (Name, Owner, Environment) so you can find it and see where the cost comes from.
  • Use roles instead of access keys on instances.
  • Treat servers as replaceable: configure through user data or images, not by hand, and you can rebuild at any time.
  • Stop or terminate what you do not use. Forgotten instances are a classic source of surprise bills, and Cost Management shows how to catch them.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does the AMI contain?

    An Amazon Machine Image is the template an instance is started from. AMIs are regional and can be copied between regions.
  2. 2. In the name m7g.xlarge, what does the letter g stand for?

    The family is m, the generation is 7, the attribute g means Graviton processors, and xlarge is the size.
  3. 3. What do you pay for while an EBS-backed instance is in the stopped state?

    Stopping ends compute billing, but the volumes still hold your data and are billed.
  4. 4. Which statement about a terminated instance is true?

    Terminate is final. Stop and start is the reversible action.
  5. 5. When does user data normally run?

    Cloud-init runs user data at first boot by default, which is why it is used for initial setup rather than for ongoing changes.

Summary

  • An EC2 instance is a virtual machine launched from an AMI, with an instance type, network, storage, key pair, role and optional user data.
  • Instance type names combine family, generation, attributes and size. The t, m, c, r, i and accelerated families cover most needs.
  • Instances live in one Availability Zone, and AMIs are regional.
  • Stopped instances do not bill compute but still bill storage; terminated instances are gone for good. A stop and start can change an auto-assigned public IP.
  • Use launch templates and images instead of manual setup so servers are replaceable.

Next: EBS Volumes, Snapshots and Instance Store