Amazon EC2 (Elastic Compute Cloud) lets you run on demand virtual servers in the cloud. This is the bread and butter for infrastructure types wherever you might be spinning up a test environment or running production workloads. Amazon EC2 gives you control and flexibility as well as the integration of other AWS services, without having to work with physical hardware.


EC2 Instance Types – Choosing the Right Tool for the Job

EC2 offers several instance families. Each one is tailored for a specific type of workload. If you've ever sized a server, this section should feel familiar.

General Purpose

  • T3 / T4g: Great for small workloads like dev environments or lightly loaded web servers. T4g runs on Graviton2 (ARM-based), which cuts costs — I use these for all personal projects.
  • M6i / M7g: Balanced CPU and RAM. These are your go-to choices for backend services, databases, and small clusters.

Compute Optimized

  • C6g / C7i: If you’re running CI pipelines, game servers, or web apps that need CPU cycles more than RAM, this is where you want to be.

Memory Optimized

  • R6i / X2idn: I’ve used these for Redis, Elasticsearch, and big-memory data workloads. If you’re caching or doing in-memory analytics, start here.

Storage Optimized

  • I4i / D3en: These shine for heavy read/write operations. I’ve deployed Cassandra and Splunk on I4i instances — it’s like strapping SSDs directly to your CPU.

Accelerated Computing

  • P4 / Inf2: You’ll see these in ML model training and video processing pipelines. P4 is GPU-based and seriously fast — but expensive.


How You Pay: EC2 Pricing Models

This is where things get interesting and dangerous if you're not careful.

On-Demand

Spin up, pay per second. Good for dev/test, or when you need flexibility.

Reserved Instances

Used in production? Lock in a 1 or 3-year term for huge savings (up to 75%). I use this for long-lived services.

Spot Instances

Spare capacity, dirt cheap. But AWS can reclaim the instance at any time. Ideal for batch jobs, image processing, and CI runners.

Savings Plans

Think of it as a billing-side discount. It works across instance types and families.


Core Features That Actually Matter

AMIs (Amazon Machine Images)

An AMI is a snapshot of your system — OS, packages, and configs. Roll your own after setting up a golden image. It saves time on future launches.

EBS (Elastic Block Store)

Think of this like attaching a virtual hard drive. Most of my Linux servers boot from gp3 volumes. For DBs, use io2 for high IOPS.

Elastic IP

A static public IP. Useful when DNS stability matters like reverse proxy setups or failover.

User Data

Bootstrap your instance. I’ve installed NGINX, pulled Git repos, and configured cronjobs all from user data scripts.

#!/bin/bash

apt update

apt install -y nginx

systemctl enable nginx


Key Pairs

Used for SSH access. Keep your private key safe. Never expose it not even in GitHub gists.

Security Groups

Virtual firewalls at the instance level in AWS. They are stateful, control inbound and outbound traffic, and are used to allow only specific ports and sources. Treat them like iptables, keeping rules minimal and restrictive by default.


Networking & Placement Strategy

VPC (Virtual Private Cloud)

All EC2s live in a VPC. You control subnets, routes, NAT gateways, and more. I always isolate public and private traffic.

ELB (Elastic Load Balancer)

Distributes traffic. Use Application Load Balancer for HTTP(S), Network Load Balancer for TCP/UDP. Crucial for multi-AZ redundancy.

Auto Scaling Groups

Tie this to a CloudWatch alarm. When traffic spikes, it adds instances. When things calm down, it scales back. I set min/max boundaries carefully.

Placement Groups

  • Cluster: Low latency between nodes — used in HPC or tightly coupled systems.
  • Spread: Instances spread across hardware to reduce failure domain.
  • Partition: Logical separation for large-scale deployments.


ENIs (Elastic Network Interfaces)

Attach multiple IPs or interfaces to a single EC2 instance. I’ve used these in failover setups.


Monitoring & Logs – Don’t Fly Blind

CloudWatch Metrics

Enable monitoring when you launch. Watch CPU, disk, network. Use dashboards. I alert on CPU > 80% for 5 mins.

CloudWatch Alarms

Trigger scaling events or notifications. Link to SNS for email/SMS/Slack alerts.

CloudTrail

Track every API call. Useful for audits or debugging who did what.

EC2 Instance Connect

Browser-based SSH. Handy if you lose your SSH key or need temporary access.


EC2 Automation & Management

Launch Templates

Reusable templates for launching EC2s. Version-controlled — I always use these in autoscaling setups.

Auto Recovery

If a hardware failure happens, AWS tries to recover your instance.

Stop vs Terminate

  • Stop: Keeps data, stops billing for instance (not EBS).
  • Terminate: Deletes everything unless it’s EBS-backed.


Systems Manager (SSM)

No need for SSH. Run commands, install updates, or patch fleets of EC2s from the console.


Example: Deploy a Web App

aws ec2 run-instances \

  --image-id ami-0abcdef1234567890 \

  --instance-type t3.micro \

  --key-name mykey \

  --security-groups web-sg \

  --user-data file://setup-nginx.sh


Inside setup-nginx.sh:

#!/bin/bash

apt update

apt install -y nginx

systemctl enable nginx && systemctl start nginx

Security group:

Inbound: TCP 22, 80 from 0.0.0.0/0

Outbound: All allowed


Conclusion

EC2 is an essential service from AWS. It's powerful and flexible, and it's also easy to misconfigure or misfire in terms of over-usage. At a high level, there are basics to stick with: picking the right EC2 instance type, automating where possible, securing EC2, and monitoring usage. If configured correctly, EC2 runs everything from test environments to large scale production worldwide reliably.