DEV Community

Cover image for Day 10 - AWS EC2
Rahul Joshi
Rahul Joshi

Posted on

Day 10 - AWS EC2

Modern applications don’t survive on a single server anymore.

Whether you're building:

  • A fintech platform
  • SaaS application
  • E-commerce system
  • AI backend
  • DevSecOps pipeline
  • High-traffic API

You need infrastructure that can:

✅ Scale automatically
✅ Handle traffic spikes
✅ Stay highly available
✅ Recover from failures
✅ Distribute traffic intelligently

And this is exactly where AWS compute services shine.

🔗 Resources


☁️ What is EC2?

Amazon Web Services EC2 (Elastic Compute Cloud) is AWS’s virtual machine service.

Think of EC2 as:

Your own server inside AWS data centers
Enter fullscreen mode Exit fullscreen mode

Instead of buying physical hardware:

  • CPU
  • RAM
  • Storage
  • Networking

AWS gives them virtually on-demand.

EC2 Dashboard


🧠 Why EC2 Became So Popular

Before cloud computing:

❌ Companies purchased expensive servers
❌ Scaling took weeks/months
❌ Hardware failures caused downtime
❌ Infrastructure was static

EC2 changed everything.

Now you can:

✅ Launch servers in minutes
✅ Scale globally
✅ Pay only for usage
✅ Automate deployments
✅ Recover automatically

This is why almost every modern startup uses cloud infrastructure.


🏗️ EC2 Core Concepts


🖥️ 1️⃣ Instance Types

Different workloads need different hardware.

AWS provides multiple EC2 families:

Family Purpose
T-Series General purpose
C-Series Compute optimized
R-Series Memory optimized
M-Series Balanced workloads
P/G-Series GPU/AI workloads

Example:

t3.micro → small testing server
c7g.large → compute-heavy backend
r6g.large → databases
g5.xlarge → AI/ML workloads
Enter fullscreen mode Exit fullscreen mode

💾 2️⃣ AMI (Amazon Machine Image)

AMI is the blueprint of a server.

It contains:

  • Operating system
  • Installed software
  • Configuration
  • Packages

Examples:

  • Ubuntu AMI
  • Amazon Linux
  • Windows Server
  • Custom enterprise images

🔐 3️⃣ Security Groups

Security Groups act like firewalls.

Example:

Port Purpose
22 SSH
80 HTTP
443 HTTPS

Best practice:

Never allow 0.0.0.0/0 on SSH in production.
Enter fullscreen mode Exit fullscreen mode

A Quick Glimpse of the EC2 Setup Process

EC2 First Screenshot

EC2 Second

EC2 Running


🚀 Launch Templates

Now let’s move toward automation.


🤔 What is a Launch Template?

A Launch Template is a reusable EC2 configuration blueprint.

Instead of manually configuring servers every time, AWS can automatically launch instances using predefined settings.

Launch Templates include:

  • AMI
  • Instance type
  • Security Groups
  • IAM role
  • User data scripts
  • Storage config
  • Key pairs

Launch Template


🧠 Why Launch Templates Matter

Imagine scaling from:

2 servers → 200 servers
Enter fullscreen mode Exit fullscreen mode

Manually configuring each one would be impossible.

Launch Templates solve this problem.


⚙️ Real Example

Suppose you're running a fintech application.

Every server needs:

  • Ubuntu
  • Docker installed
  • Node.js runtime
  • Security monitoring agent
  • Same firewall rules

Instead of manual setup:

Create Launch Template once
→ Auto Scaling uses it automatically
Enter fullscreen mode Exit fullscreen mode

A Quick Glimpse of the Launch Template Setup Process

Launch Template Second

Launch Template Third


🛠️ User Data Scripts

One of the most powerful Launch Template features.

When EC2 starts:

AWS can automatically execute scripts.

Example:

#!/bin/bash
apt update -y
apt install docker.io -y
systemctl start docker
docker run nginx
Enter fullscreen mode Exit fullscreen mode

This enables Infrastructure as Code behavior.


📈 Auto Scaling Groups (ASG)

This is where AWS becomes truly powerful.


🤔 What is Auto Scaling?

Auto Scaling automatically:

✅ Adds servers during high traffic
✅ Removes servers during low traffic
✅ Replaces unhealthy instances
✅ Maintains application availability


🧠 Why ASG is Important

Imagine:

Your application suddenly gets viral traffic.

Without Auto Scaling:

❌ Servers crash
❌ Website slows down
❌ Revenue loss
❌ Poor customer experience

With ASG:

✅ AWS automatically launches more EC2 instances


⚡ How ASG Works

Flow:

ASG

This is how modern internet infrastructure scales.


🏗️ ASG Components

Component Purpose
Launch Template Defines instance config
Desired Capacity Number of running instances
Minimum Capacity Lowest server count
Maximum Capacity Highest scaling limit

Example:

Min = 2
Desired = 2
Max = 10
Enter fullscreen mode Exit fullscreen mode

🔥 Scaling Policies

AWS supports multiple scaling strategies.


📊 Dynamic Scaling

Scale based on metrics:

  • CPU utilization
  • Memory
  • Request count

Example:

CPU > 70%
→ Add 2 instances
Enter fullscreen mode Exit fullscreen mode

⏰ Scheduled Scaling

Useful for predictable traffic.

Example:

Scale up every Friday evening
Enter fullscreen mode Exit fullscreen mode

🎯 Target Tracking

AWS automatically tries to maintain a metric target.

Example:

Keep CPU around 50%
Enter fullscreen mode Exit fullscreen mode

This is one of the most commonly used production strategies.


❤️ Health Checks

ASG continuously checks server health.

If a server becomes unhealthy:

Terminate instance
        ↓
Launch replacement automatically
Enter fullscreen mode Exit fullscreen mode

This improves fault tolerance.


🌐 Load Balancing

Now we need a way to distribute traffic.

This is where Load Balancers enter.


⚖️ What is a Load Balancer?

A Load Balancer distributes incoming traffic across multiple servers.

Without it:

All traffic → One server
Enter fullscreen mode Exit fullscreen mode

With it:

Traffic → Multiple servers evenly
Enter fullscreen mode Exit fullscreen mode

Benefits:

✅ High availability
✅ Better performance
✅ Fault tolerance
✅ Scalability

Load Balancer


🚦 Types of AWS Load Balancers

AWS mainly provides:

Type Layer Best For
ALB Layer 7 HTTP/HTTPS
NLB Layer 4 TCP/UDP ultra-high performance

🌍 Application Load Balancer (ALB)

ALB works at:

OSI Layer 7 (Application Layer)
Enter fullscreen mode Exit fullscreen mode

It understands:

  • URLs
  • HTTP headers
  • Hostnames
  • Paths

🧠 ALB Features


🔀 Path-Based Routing

Example:

/api → Backend API servers
/images → Image servers
/admin → Admin application
Enter fullscreen mode Exit fullscreen mode

Single ALB can route traffic intelligently.


🌐 Host-Based Routing

Example:

api.company.com
blog.company.com
admin.company.com
Enter fullscreen mode Exit fullscreen mode

ALB routes traffic based on hostname.


🔒 HTTPS Termination

ALB can handle SSL/TLS certificates directly.

This reduces backend server complexity.


🍪 Sticky Sessions

ALB can keep users connected to the same server session.

Useful for legacy applications.


⚡ Network Load Balancer (NLB)

NLB works at:

OSI Layer 4 (Transport Layer)
Enter fullscreen mode Exit fullscreen mode

It is built for:

✅ Ultra-high performance
✅ Millions of requests/sec
✅ Low latency
✅ TCP/UDP traffic


🧠 When to Use NLB

NLB is perfect for:

  • Gaming servers
  • Financial transaction systems
  • Real-time applications
  • VoIP
  • TCP services

⚔️ Load Balancer

ALB Vs NLB


🏗️ Real Production Possible Architecture

Typical scalable AWS architecture:

Possible AWS

This is the foundation of most modern cloud-native applications.


🔥 High Availability Architecture

Production systems usually deploy across multiple AZs.

Example:

multi az architecture

If one Availability Zone fails:

✅ Traffic automatically shifts


💰 Cost Optimization

cost optimization

🔐 Security Best Practices


✅ Use IAM Roles Instead of Access Keys

Never hardcode credentials inside servers.


✅ Restrict Security Groups

Allow only required ports.


✅ Enable Monitoring

Use:

  • CloudWatch
  • VPC Flow Logs
  • GuardDuty

✅ Patch Regularly

Outdated instances become security risks.


🧠 Final Thoughts

EC2 is the core foundation of AWS infrastructure engineering.

If Kubernetes is the future of orchestration…

Then EC2 is still the foundation underneath most cloud environments.

Understanding these concepts deeply will help you in:

  • DevOps
  • Cloud Engineering
  • Site Reliability Engineering
  • DevSecOps
  • Platform Engineering
  • Fintech infrastructure

Because modern infrastructure is not just about launching servers anymore.

It’s about:

Automation
Scalability
Resilience
Availability
Security
Enter fullscreen mode Exit fullscreen mode

And AWS provides all of them at massive scale.

Top comments (0)