Amazon EC2 (Elastic Compute Cloud) rents you virtual machines, called instances, in AWS data centers. You pick the CPU, memory, storage and network you need, start a server in minutes, and pay only while it runs. EC2 is the base layer under many other AWS services, so knowing how instances, tenancy and pricing work helps you size systems and control the bill.
What an instance is#
An instance is a virtual machine on a physical host that AWS runs. You get full control of the guest operating system (Linux or Windows): you install packages, edit config and run any process. AWS owns the hardware, the hypervisor and the data center.
Every launch asks you the same handful of questions. Most of them get their own note later, so here's what each one is:
- AMI: the machine image with the OS and software (see AMIs).
- Instance type: the hardware shape, such as
m7g.large. - Network: a VPC subnet, which also fixes the Availability Zone.
- Security groups: virtual firewalls that allow inbound and outbound traffic by port, protocol and source.
- Key pair: AWS stores the public key on the instance. You keep the private key and use it to log in over SSH.
- Storage: an EBS root volume, plus optional data volumes.
- User data: an optional boot script (see Instance Metadata and User Data).
You manage all of this through the console, the AWS CLI, the SDKs or infrastructure-as-code tools. Tags (key-value labels such as team=payments) help you filter resources and split the bill.
Instance families and sizes#
Of those launch choices, the instance type is the one that sets what the machine can do. AWS groups instance types into families. Each family tunes the balance of CPU, memory, storage and network for one kind of workload.
| Family | Optimized for | Typical workloads | Example letters |
|---|---|---|---|
| General purpose | Balanced CPU and memory | Web servers, small databases, dev | M, T |
| Compute optimized | High CPU per GB of memory | Batch jobs, encoding, game servers | C |
| Memory optimized | Large memory | In-memory caches, analytics, big databases | R, X |
| Accelerated computing | GPUs or ML chips | ML training and inference, graphics | P, G, Inf, Trn |
| Storage optimized | Fast local disks | NoSQL databases, data warehouses | I, D |
Reading an instance type name#
Once you know the families, the type names stop looking random. A name such as m7g.large packs four facts:
- Family letter (
m): general purpose. - Generation (
7): higher is newer and usually gives better price-performance. - Attributes (
g): here, an AWS Graviton (Arm) processor. Other common letters arei(Intel),a(AMD) andd(local NVMe instance store). - Size (
large): sizes double in steps, such aslarge,xlarge,2xlarge.
One family needs a closer look, because it behaves differently under load. Burstable T instances (for example t3 and t4g) earn CPU credits while idle and spend them during short bursts. They are cheap for spiky, low-average workloads. A busy service that runs hot all day drains its credits, so it suits a fixed-performance family better.
Tenancy: shared, Dedicated Instances and Dedicated Hosts#
The instance type sets the shape of the machine. Tenancy sets who else shares the physical server under it. By default your instances run on shared tenancy: one physical host can run instances from many AWS accounts. The hypervisor isolates them. Some licenses and compliance rules need more physical separation, so EC2 offers two dedicated options.
| Dedicated Instances | Dedicated Hosts | |
|---|---|---|
| Hardware | Runs on hosts used only by your account | A whole physical server assigned to you |
| Sharing | Can share a host with your other instances | Only the instances you place on it |
| Host visibility | None: AWS picks the host | You see sockets and cores and control placement |
| Main reason | Isolation from other customers | Bring-your-own licenses tied to sockets or cores |
Choose Dedicated Hosts when a software license (for example some Windows Server or SQL Server licenses) counts physical cores or sockets. Reusing existing licenses can save money and help you pass audits. Choose Dedicated Instances when you need isolation from other accounts but do not care which host you get.
Dedicated is a tenancy choice, not a separate discount plan. You still pay for it On-Demand or with a commitment.
Pricing models#
How you buy capacity often changes the bill more than which instance type you pick. EC2 bills common Linux and Windows instances per second, with a one-minute minimum. A stopped instance has no instance charge, but its EBS volumes still cost money.
| Model | You commit to | Discount | Can AWS take it back? | Good for |
|---|---|---|---|---|
| On-Demand | Nothing | None | No | Spiky, short or unknown workloads; dev and test |
| Savings Plans | A $/hour spend for 1 or 3 years | Large | No | Steady baseline usage |
| Reserved Instances | A specific instance setup for 1 or 3 years | Large | No | Steady usage with a fixed shape |
| Spot | Nothing | Largest | Yes, with a 2-minute notice | Fault-tolerant, flexible jobs |
The sections below walk through each row of the table.
On-Demand#
You pay a fixed rate for the time an instance runs, with no upfront payment and no contract. On-Demand is the default and the most flexible option. It is also the most expensive per hour, so it fits the part of your load you cannot predict.
Savings Plans and Reserved Instances#
Both trade a 1-year or 3-year commitment for a lower rate. You can pay all upfront, part upfront or nothing upfront; more upfront gives a bigger discount.
- Savings Plans commit you to a dollar amount of compute per hour. Compute Savings Plans apply across instance families, Regions, and also to Fargate and Lambda. EC2 Instance Savings Plans lock to one family in one Region for a deeper discount.
- Reserved Instances (RIs) commit you to an instance type, platform and Region. Standard RIs give the deeper discount; Convertible RIs let you exchange for other types. A zonal RI also reserves capacity in one Availability Zone.
AWS now recommends Savings Plans for most new commitments because they are easier to apply across changing fleets. If you need guaranteed capacity in one zone without a long commitment, use an On-Demand Capacity Reservation.
Spot Instances#
Spot is the biggest discount, and the only model where AWS can take your instance away. It sells spare EC2 capacity cheaply and reclaims it when it needs the capacity back.
- There is no bidding. Since 2017 the Spot price moves slowly with long-term supply and demand. You pay the current Spot price. You can set an optional maximum price, but it defaults to the On-Demand price.
- Interruptions come with a 2-minute notice. EC2 publishes the notice in instance metadata and as an EventBridge event. Your code should watch for it, finish or checkpoint work, and drain connections.
- Interruption behavior can be terminate (the default), stop or hibernate.
- Spread across many instance types and zones. A diverse request is less likely to lose all capacity at once.
Spot suits stateless web workers behind a load balancer, batch and data processing, CI runners and ML training with checkpoints. It does not suit a single database primary or any job that cannot restart.
A common mix#
You don't have to pick just one model. Most production fleets combine them:
- Cover the steady baseline with Savings Plans.
- Run predictable peaks on On-Demand.
- Add Spot for burst capacity and batch work.
An Auto Scaling group can mix On-Demand and Spot in one group.
Instance lifecycle and public IP addresses#
Pricing depends on whether an instance is running, so it helps to know the states it moves through. They also explain what happens to your disks and addresses.
| State | Meaning | Instance charges? |
|---|---|---|
pending |
Booting | No |
running |
Up and serving | Yes |
stopping / stopped |
Shut down; EBS volumes kept | No (EBS still billed) |
shutting-down / terminated |
Being deleted / gone for good | No |
A reboot restarts the OS on the same host and keeps everything. A stop and start usually moves the instance to new hardware, which has two effects:
- Instance store data is lost. Local NVMe disks belong to the host, not the instance.
- The auto-assigned public IPv4 address changes. EC2 releases it on stop and assigns a new one on start. The private IP address stays the same.
The auto-assigned public IP does not change while the instance keeps running. It goes away for good on terminate. The table compares each kind of address across the three actions.
| Action | Auto-assigned public IPv4 | Elastic IP | Private IP |
|---|---|---|---|
| Reboot | Kept | Kept | Kept |
| Stop, then start | Replaced | Kept | Kept |
| Terminate | Released | Disassociated (stays in your account) | Released |
An Elastic IP, the column that stays put, is a static public IPv4 address that you own until you release it. You can move it between instances, which helps when clients or firewall rules need a fixed address. AWS charges for public IPv4 addresses, including Elastic IPs, so release the ones you do not use. For most web services, a load balancer DNS name is a better fixed entry point than an Elastic IP on one server.
Trade-offs and pitfalls#
Most of the ideas above show up again as mistakes. These are the ones that cost teams the most money or data.
- Oversized instances waste the most money. Start smaller, measure CPU and memory, then resize.
- Commitments are hard to undo. Commit only to the usage you are sure of, and review it as the fleet changes.
- Spot without interruption handling loses work. Make jobs idempotent and checkpoint progress.
- Data on instance store disappears on stop or hardware failure. Keep anything you need on EBS, S3 or a database.
- Hard-coding a public IP breaks on the next stop and start. Use DNS, a load balancer or an Elastic IP.
Key takeaways#
- An EC2 instance is a VM you control; the instance type fixes its CPU, memory, storage and network shape.
- Read a type like
m7g.largeas family, generation, attributes and size. Pick the family that matches the bottleneck. - Dedicated Hosts give you a whole server for core-based licenses; Dedicated Instances only isolate you from other accounts.
- Use On-Demand for the unpredictable part, Savings Plans or RIs for the steady baseline, and Spot for interruptible work.
- Spot has no bidding; AWS reclaims it with a 2-minute notice.
- Stop and start replaces the auto-assigned public IP and wipes instance store; reboot keeps both.