EC2: Virtual Servers in the Cloud
EC2 (Elastic Compute Cloud) rents virtual machines, called instances, by the second. It's the most fundamental AWS compute service, and the one most other services are conceptually built on top of.
Launching an instance
Every EC2 instance starts from an AMI (Amazon Machine Image), a template that includes an operating system and optionally pre-installed software. AWS provides official AMIs (Amazon Linux, Ubuntu, Windows Server), and you can create your own from a configured instance to reuse later.
Key choices when launching an instance:
- Instance type (e.g.
t3.micro,m5.large), determines vCPUs, memory, and network performance. The letter is the family (general purpose, compute-optimized, memory-optimized...), the number is the generation, the suffix is the size. - Key pair, an SSH key you use to log in, AWS never stores your private key.
- Security group, a virtual firewall controlling what traffic can reach the instance.
Security groups
Inbound rules:
- Port 22 (SSH) from 203.0.113.4/32 (your IP only)
- Port 443 (HTTPS) from 0.0.0.0/0 (anyone)
Outbound rules:
- All traffic to 0.0.0.0/0
Security groups are stateful: if you allow inbound traffic on a port, the matching response traffic is automatically allowed out, you don't need a separate outbound rule for replies. They're also allow-only, you can't write an explicit "deny" rule, only choose what to allow.
Elastic IPs
A regular EC2 instance's public IP changes if you stop and start it. An Elastic IP is a static public IP you allocate to your account and attach to an instance, giving you a stable address that survives stop/start cycles (though it costs a small amount if it's allocated but not attached to a running instance, to discourage hoarding unused addresses).
TIP
For anything long-running and public-facing, put a load balancer in front of your EC2 instances instead of relying on a single instance's Elastic IP, that way you can add/replace instances without ever changing the address your users hit.