Back to Library
aws

EC2 - Elastic Compute Cloud

EC2 provides resizable virtual servers in the cloud. You choose instance type, OS, and pay per second of runtime.

awscomputeec2scaling

What is AWS EC2 (Elastic Compute Cloud)?

EC2 (Elastic Compute Cloud) is AWS's virtual machine service. Each EC2 instance is a virtual server running on AWS hardware. You select the instance type (defining CPU, RAM, network, storage), the AMI (Amazon Machine Image - your OS + software), and the region.

Instance Types

Instance types follow the naming pattern family.size - e.g., t3.medium, m6i.xlarge, c7g.2xlarge.

FamilyOptimised ForExample Use
t (burstable)Low baseline CPU with burstDev/test, low-traffic web
m (general)Balanced CPU/RAMApplication servers
c (compute)High CPUEncoding, ML inference, HPC
r (memory)High RAMIn-memory DBs, analytics
i (storage)High IOPS NVMeDatabases, caches
p/g (GPU)GPU computeML training, rendering
x (extra memory)Extreme RAM (up to 24 TB)SAP HANA, in-memory analytics

Sizes within a family: nano -> micro -> small -> medium -> large -> xlarge -> 2xlarge -> ... -> 48xlarge.

Purchasing Options

OptionDescriptionSavings vs On-Demand
On-DemandPay per second, no commitmentBaseline (0%)
Reserved (1 or 3 yr)Commit to instance family + regionUp to 72%
Savings PlansFlexible reserved - commit to $/hr spendUp to 66%
SpotSpare AWS capacity, interruptible (2 min notice)Up to 90%
Dedicated HostPhysical server for your useFor licensing compliance

Spot interruption: AWS reclaims spot capacity with 2 minutes notice. Use for stateless, fault-tolerant workloads - batch jobs, CI runners, ML training checkpoints.

Storage

  • Instance store: Ephemeral NVMe directly on the host. Extremely fast. Lost when instance stops.
  • EBS (Elastic Block Store): Persistent network-attached volumes. Survives stop/start. Multiple volume types (gp3, io2, st1, sc1).
  • EFS: Shared NFS file system mountable across many instances.

AMIs (Amazon Machine Images)

AMIs snapshot the root volume + launch config. You can use AWS-provided AMIs (Amazon Linux 2/2023, Ubuntu, Windows), third-party marketplace AMIs, or custom AMIs you create from existing instances (for pre-baked environments).

Auto Scaling Groups (ASG)

ASG manages a fleet of EC2 instances:

  • Define min/desired/max instance count
  • Launch template specifies instance type, AMI, user data
  • Scaling policies: target tracking (maintain 60% CPU), step scaling, scheduled
  • Health checks: EC2 status checks or ELB health checks replace unhealthy instances

ASG is the primary EC2 horizontal scaling mechanism.

Placement Groups

TypeBehaviourUse For
ClusterAll instances in same rackLow-latency HPC, 10Gbps between instances
SpreadInstances on separate hardwareCritical instances needing fault isolation
PartitionGroups in separate racksDistributed systems (Kafka, Cassandra)

Key Networking

  • Each instance gets a private IP within its VPC subnet
  • Public IP (if configured) is ephemeral - changes on stop/start. Use Elastic IP for a static public address.
  • Security groups = stateful firewall at the instance level (allow rules only)
  • NACLs = stateless firewall at the subnet level (allow + deny rules)

Interview Angle

EC2 questions often focus on cost optimisation (on-demand vs reserved vs spot), instance family selection (right-size for the workload), and scaling patterns (ASG + ALB). For stateless services, spot + ASG gives up to 90% savings. For databases, reserved or savings plans. Always mention that t3 instances have CPU credits - sustained high CPU workloads should use m or c family instead.

Recommended Resources