AWS EC2 - Virtual Machine in the Cloud

Seriously, do I still need a virtual machine in the era of serverless? The answer is yes., do you want to run long processes in your stateful application., great then use a VM.

What Is EC2 ?

EC2 (Elastic Compute Cloud) gives you virtual servers on-demand. EC2 gives you full control over the OS, runtime, software stack, and more.

When to Use EC2:

  • Application hosting - Host a backend server or a microservice per instance
  • Heavy Workloads: Run batch jobs, machine learning, data processing, or computational tasks.
  • Full Control: When you need complete control over the infrastructure, such as customizing firewalls or the OS itself.

How EC2 Actually Works ?

image.png

  • Provisioning: EC2 provisions a virtual machine (VM) on a physical host in Amazon’s data centers.
  • Networking: Your instance gets a private IP (inside a Virtual Private Cloud), and optionally a public IP.
  • Storage: Each instance gets persistent storage via EBS (Elastic Block Store), which is allocated automatically. The storage size depends on the AMI (Amazon Machine Image) chosen.

Under the hood, multiple EC2 instances might run on the same server ( thank you hypervisors )

Key Concepts in EC2 (Quick Reference)

ConceptWhat it is
AMI (Amazon Machine Image)Pre-installed OS template
InstanceThe running VM.
Instance TypeHardware spec (CPU, memory) e.g., t2.micro.
Key PairYour SSH login credentials.
Security GroupActs like a firewall - controls which ports/IPs can talk to your instance.
Elastic IPStatic IP you can attach to your instance (optional but useful for production)
EBS VolumePersistent disk storage.

Choosing the Right Instance Type

image.png

FamilyUse CaseExamples
t3, t4gCheap burstable workloadsWebsites, dev servers
m6iGeneral purposeApplication servers
c7gCompute optimizedHigh-performance APIs, games
r6aMemory optimizedIn-memory databases

💸 Start with T3.micro (free tier) for tests


EC2 States You Should Know

StateMeaning
RunningVM is up and accessible.
StoppedVM is halted, not billing for compute.
TerminatedVM is deleted - irreversible.

You pay hourly when an instance is running. Stop it when not needed.


Launching and Connecting to Your First Instance

Before anything launch and connect to your VM remotely, then we can get into building blocks of EC2.

Launching an EC2 Instance

  • Go to EC2 → Launch Instance and give it a name (e.g., dev-webserver).
  • Choose an AMI - Amazon Machine Image (e.g., Ubuntu 20.04 or Amazon Linux 2).
  • Select an Instance Type (e.g., t2.micro).
  • Create a Key Pair (download .pem file).
  • Configure networking:
    • Select a VPC and Subnet.
    • Create or select a Security Group with:
      • Port 22 for SSH (your IP only)
      • Port 80/443 or custom app port (like 3000)
  • Click Launch Instance.

Connect to EC2

On Mac/Linux:

chmod 400 your-key.pem
ssh -i "your-key.pem" ec2-user@<public-ip>

On Windows: Use PuTTY or WSL

Tip: Use VS Code Remote SSH for a full coding environment on your EC2 box.


In the next article, lets learn how to deploy a nodejs app on EC2 instance.