Mastering Agentic AI with Java: Live Course
AWSCompute with EC2

Introduction to EC2

EC2 - Elastic Compute Cloud - is the service most people picture when they think "the cloud." It's a computer you rent by the second: pick how powerful, pick the operating system, and a minute later you can SSH in and it's yours. No hardware, no data centre, no waiting.

If S3 is the cloud's hard drive, EC2 is its CPU.

What an EC2 instance actually is

An instance is a virtual server running on Amazon's physical hardware. One big physical machine in a data centre is sliced - by a hypervisor - into many isolated virtual servers, and you rent one slice. To you it behaves exactly like a dedicated machine: it has its own OS, memory, disk, and network.

   One physical server in an AWS data centre
   ┌───────────────────────────────────────────┐
   │   Hypervisor (carves it into slices)        │
   │  ┌─────────┐ ┌─────────┐ ┌─────────┐        │
   │  │Instance │ │Instance │ │Instance │  ...   │
   │  │  yours  │ │ someone │ │ someone │        │
   │  │         │ │  else's │ │  else's │        │
   │  └─────────┘ └─────────┘ └─────────┘        │
   └───────────────────────────────────────────┘

The "Elastic" in the name is the point: you can launch one instance or a hundred, keep them for an hour or a year, and resize them as needs change.

The choices you make when launching

Launching an instance is mostly answering five questions. Every EC2 launch wizard is these in order:

ChoiceWhat it decidesExample
AMIThe OS + pre-installed softwareAmazon Linux 2023, Ubuntu, Windows Server
Instance typeCPU, RAM, network capacityt2.micro (1 vCPU, 1 GB) → m5.large → huge
Key pairHow you'll log in securelyAn SSH key you download once
Network & security groupWhere it lives and what traffic is allowedA VPC + a firewall rule for SSH/HTTP
StorageThe disk(s) attachedAn EBS root volume, e.g. 8 GB

AMI - Amazon Machine Image

An AMI is a template for the instance's disk: the operating system and anything pre-baked into it. Choosing Ubuntu 22.04 vs Amazon Linux 2023 here is choosing what your server boots into. You can also create your own AMI from a configured instance, so the next launch already has your software installed.

Instance type - the t/m/c/r families

Instance types are grouped into families tuned for different jobs. The letter tells you the focus:

FamilyOptimized forUse it for
t (e.g. t3.micro)Burstable, general purpose, cheapDev, small web apps, learning
m (e.g. m5.large)Balanced CPU/memoryGeneral production workloads
c (e.g. c5.large)Compute (CPU-heavy)Batch processing, gaming servers
r (e.g. r5.large)Memory-heavyIn-memory caches, big databases

The number is the generation (newer = faster/cheaper), and the suffix (micro, large, xlarge) is the size. For this whole course, t2.micro or t3.micro is what you want - it's free-tier eligible.

Don't overthink the instance type while learning. Start with a free-tier t2.micro. If it's too small for something later, you can stop the instance, change the type, and start it again - resizing takes a couple of minutes, not a purchase order.

The key pair: how you log in

When you launch an instance you attach a key pair - a public key AWS keeps on the server and a private key (.pem file) you download. You connect with:

ssh -i my-key.pem ec2-user@<public-ip>

The private key is the only way in via SSH, and AWS does not keep a copy.

Download the .pem file when you create the key pair and store it safely - you cannot download it again. Lose it and you lose SSH access to instances that use it. Also chmod 400 my-key.pem on Linux/Mac, or SSH will refuse to use a world-readable key.

Pricing models: how you pay for compute

You don't have to pay the same way for every instance. Matching the pricing model to the workload is where real money is saved:

ModelWhat it isBest for
On-DemandPay per second, no commitmentShort-term, unpredictable, learning
Reserved / Savings PlansCommit 1-3 years for a big discountSteady, always-on production
SpotBid on spare capacity, up to ~90% off, can be reclaimedFault-tolerant batch jobs
Dedicated HostsA whole physical server for youLicensing/compliance needs

For learning, you're on On-Demand and the free tier - which gives 750 instance-hours/month of a micro instance for the first 12 months.

The instance lifecycle

An instance moves through states, and the difference between stop and terminate is one beginners get wrong painfully:

   launch ──► pending ──► running ──┬──► stop ──► stopped ──► start ──► running

                                    └──► terminate ──► terminated  (gone forever)
ActionWhat happensBilling
StopShuts down; EBS disk persists; can start againNo compute charge; small EBS storage charge
RebootRestarts; keeps everything, keeps its IPCharged normally
TerminateDeletes the instance; root volume usually deleted tooStops entirely; not recoverable

Stop ≠ Terminate. Stop is "pause and come back later" - your data survives. Terminate is "destroy it" - by default the root disk is wiped with it. When you're done experimenting for the day, stop the instance to avoid charges; terminate it only when you truly don't need it.

Launching your first instance - the flow

Open EC2 and click Launch Instance

Make sure your region (top-right) is the one you want.

Name it, pick an AMI

Give it a Name tag. Choose Amazon Linux 2023 or Ubuntu (free-tier eligible).

Pick t2.micro and a key pair

Free-tier instance type; create or select an SSH key pair and save the .pem.

Configure network & security group

Leave the default VPC; create a security group allowing SSH (port 22) from your IP. (Next page covers this in depth.)

Launch, then connect

Wait for "running," copy the public IP, and SSH in.

You now have a real Linux server in the cloud. The next pages make it useful and safe - starting with the firewall in front of it: security groups.

How is this guide?

Last updated on