Core Foundations: EC2, virtual servers properly
Amazon EC2 from first principles: instance types, AMIs, storage, pricing models, IAM roles for instances, and connecting with Session Manager instead of SSH keys.
Part 3 of the Core Foundations series.
Amazon EC2 (Elastic Compute Cloud) gives you virtual machines, called instances, on demand. Even if you plan to go fully serverless, it’s worth understanding: containers on ECS and EKS, EMR clusters, and many managed services run on EC2 capacity, and their options and quotas often come back to EC2 concepts.
What makes up an instance
An AMI (Amazon Machine Image) is the template: an operating system plus any pre-installed software. Start with an AWS-provided image such as Amazon Linux 2023, or build your own.
An instance type sets the hardware. Names follow a pattern; take m7g.large:
m: the family.tis burstable,mgeneral purpose,ccompute optimised,rmemory optimised.7: the generation. Newer is usually faster and cheaper.g: attributes.gmeans an AWS Graviton (Arm) processor,iIntel,aAMD.large: the size. Each step up (xlarge,2xlarge…) roughly doubles resources and price.
Graviton instances typically offer better price-performance, and most Linux software runs on Arm without changes, so they’re worth trying first.
Storage comes in two forms:
- EBS volumes are network-attached disks that persist independently of the instance. Your root volume is usually EBS, and you can snapshot it.
- Instance store is physically attached and very fast, but its data is lost when the instance stops. Use it for caches and scratch space only.
Networking: an instance launches into a subnet in your VPC and has one or more security groups.
An IAM role, attached through an instance profile, gives software on the instance temporary credentials, so you never copy access keys onto a server. (See IAM.)
Lifecycle
- Running: you pay for compute.
- Stopped: no compute charges, but EBS volumes (and any public IPv4 addresses) are still billed. Starting it again may place it on different hardware.
- Terminated: gone. By default, the root EBS volume is deleted with it.
Treat instances as cattle, not pets: anything you’d be upset to lose should live in code (user data, an AMI build, or infrastructure as code) or in managed storage, not only on one instance’s disk.
Paying for EC2
- On-Demand: per-second billing (for most Linux instances), no commitment. Start here.
- Savings Plans: commit to a level of spend per hour for one or three years, for a significant discount across instance types.
- Spot: spare capacity at a large discount, but AWS can reclaim it with a two-minute warning. Great for batch jobs, CI runners and anything that can be interrupted.
- Reserved Instances: the older commitment model; Savings Plans are simpler for most people.
Whichever you choose, right-sizing (picking the smallest instance that does the job) usually saves more than any discount.
Connecting without SSH keys
The traditional approach is to open port 22 in the security group and manage SSH key pairs. A better default is AWS Systems Manager Session Manager:
- no inbound ports open at all,
- access controlled by IAM, not by key files,
- sessions can be logged, and
- it works for instances in private subnets (with VPC endpoints or a NAT gateway).
The instance needs the SSM Agent (pre-installed on Amazon Linux) and a role with the
AmazonSSMManagedInstanceCore managed policy.
Hands-on: a web server with no open admin ports
-
Create an IAM role for EC2 with
AmazonSSMManagedInstanceCoreattached. -
Launch an Amazon Linux 2023 instance on a small burstable type (for example
t4g.micro), with that role and a security group allowing only HTTP (80) inbound. -
Under Advanced details → User data, add a script that runs on first boot:
#!/bin/bash dnf install -y nginx echo "<h1>Hello from $(hostname)</h1>" > /usr/share/nginx/html/index.html systemctl enable --now nginx -
Browse to the instance’s public IP to see the page.
-
Choose Connect → Session Manager to get a shell, with no SSH key and no port 22.
-
Terminate the instance when you’re done.
Gotchas
- Use IMDSv2. The instance metadata service hands out the role’s credentials; require the token-based IMDSv2 (the default for new AMIs and launch templates in recent years) to protect against server-side request forgery.
- Burstable
tinstances run on CPU credits. Sustained high CPU either exhausts credits and throttles, or (in unlimited mode) costs extra. - Stopped is not free. EBS volumes, snapshots and public IPv4 addresses keep billing.
- One instance is one AZ. For availability, use an Auto Scaling group across AZs behind a load balancer.
Next: S3: storage that underpins AWS.
← Series overview