💡 Ask Tutor

Understanding Amazon EC2 (Elastic Compute Cloud)

Amazon EC2 is the foundation of AWS compute services—used by startups, enterprises, and developers globally. This chapter explores EC2 in depth: what it is, how it works, its pricing, and how to launch and manage EC2 instances.

What is Amazon EC2?

Amazon EC2 (Elastic Compute Cloud) is a web service that provides resizable, scalable compute capacity in the cloud. Think of EC2 as a virtual server (VM) that you can launch, configure, stop, restart, or terminate as needed.

📌 EC2 allows businesses to run applications without upfront hardware investment and supports Linux/Unix and Windows platforms.

EC2 Key Components

TermDescription
InstanceA virtual server in AWS
AMI (Amazon Machine Image)A template with OS + software to launch an instance
Instance TypeDefines hardware (CPU, RAM, etc.) resources
Security GroupActs as a virtual firewall
Key PairSSH authentication mechanism
Elastic IPStatic IP address for an instance
EBS VolumeStorage attached to the instance

EC2 Instance Types

AWS provides a wide variety of instance families, each optimized for different use cases.

FamilyUse CaseExamples
General PurposeBalanced CPU/RAMt2.micro, t3.medium
Compute OptimizedHigh-performance CPUc5.large, c6g.xlarge
Memory OptimizedRAM-heavy appsr5.large, x1e.xlarge
Storage OptimizedFast IOPSi3.large, d2.xlarge
GPU InstancesML, AI, Graphicsp3.2xlarge, g4dn

Step-by-Step: Launching an EC2 Instance

1. Go to EC2 Dashboard

  • Choose your region
  • Click Launch Instance

2. Select an AMI

  • Use Amazon Linux 2, Ubuntu, Windows Server, etc.

3. Choose Instance Type

  • Select t2.micro for Free Tier

4. Configure Instance

  • Default values for testing are okay
  • For production: setup subnets, roles, shutdown behavior

5. Add Storage

  • Default: 8 GB General Purpose SSD (gp2)

6. Configure Security Group

  • Allow port 22 (SSH) for Linux
  • Allow port 3389 for Windows (RDP)
  • Add HTTP/HTTPS (port 80/443) for web apps

7. Review and Launch

  • Select/Create Key Pair (.pem) for SSH
  • Click Launch

🔑 Connecting to EC2 (Linux)

Bash
ssh -i "your-key.pem" ec2-user@<Public-IP>

🔐 Always secure your .pem file, and set permissions with chmod 400 your-key.pem.

🧯 Managing EC2 Instances

You can Start, Stop, Reboot, or Terminate instances anytime from the EC2 Dashboard.

ActionEffect
StopShuts down the VM but retains data
TerminateDeletes the VM and attached storage (unless preserved)
RebootRestarts the VM
Elastic IPAssociates a public static IP

EC2 Pricing Models

ModelDescriptionIdeal For
On-DemandPay per second/minuteShort-term workloads
Reserved Instances1 or 3-year commitment for lower costPredictable usage
Spot InstancesBid for unused capacity (up to 90% off)Fault-tolerant, flexible apps
Savings PlansFlexible pricing for consistent usageCost-optimization

EC2 Auto Scaling

With Auto Scaling, AWS automatically adjusts the number of EC2 instances based on traffic load or performance metrics.

  • Create Launch Templates
  • Define Scaling Policies
  • Integrate with Elastic Load Balancer (ELB)

Security Best Practices for EC2

  • Always use SSH key pairs
  • Use IAM roles for instance-level access (e.g., access S3 securely)
  • Regularly update OS packages
  • Enable CloudWatch monitoring
  • Use Elastic IP sparingly to avoid cost
  • Open only required ports in the security group

📌 Summary

ConceptSummary
What is EC2Virtual server in AWS
AMI & Instance TypeOS template & hardware configuration
Launching Instance7-step process via Console
Connection MethodSSH or RDP
Pricing ModelsOn-Demand, Reserved, Spot, Savings Plans
Auto ScalingAutomatically handle traffic/load
Security PracticesKey pairs, IAM roles, security groups