EC2

What is EC2?

  • It’s just a computer in the cloud that you can rent from Amazon.

  • Like a laptop you can open anytime without owning the hardware.

  • Elastic = you can make it big or small, and turn it on or off when you want.

Key Parts

  1. Instance – The actual β€œcloud computer” you run.

  2. AMI (Amazon Machine Image) – The β€œtemplate” of your computer (like choosing Windows, Linux, or preloaded software).

  3. Instance Type – The size and power (tiny like a phone or huge like a supercomputer).

  4. EBS (Elastic Block Store) – Your hard drive for EC2.

  5. Security Groups – The firewall that decides who can talk to your EC2.

  6. Key Pair – Your secret key to log in (like the house key).

  7. Elastic IP – Your public phone number for the EC2.

How EC2 Works (Step-by-Step)

  1. You launch an EC2 from the AWS console or CLI.

  2. Choose:

    • AMI (OS)

    • Instance type (power)

    • Storage size (EBS)

    • Network (VPC, subnet, ENI)

  3. Attach a Security Group to allow/deny access.

  4. Download your Key Pair (to connect via SSH or RDP).

  5. Start the instance β†’ it’s live and running.

EC2 Networking

  • Public Subnet β†’ EC2 has public IP (directly reachable from internet).

  • Private Subnet β†’ EC2 only reachable inside VPC (needs NAT to go to internet).

  • ENI (Elastic Network Interface) β†’ Like EC2’s network card.

  • Elastic IP β†’ Permanent public IP you can attach to EC2.

EC2 Storage Options

  • EBS – Persistent storage (keeps data after stop/start).

  • Instance Store – Temporary storage (data gone when instance stops).

  • EFS – Shared file system (multiple EC2s can use it).

Scaling & Availability

  • Auto Scaling β†’ Automatically add/remove EC2s based on traffic.

  • Load Balancers (ALB/NLB) β†’ Spread traffic between EC2s.

  • Multi-AZ β†’ Run EC2s in different Availability Zones for high availability.

EC2 Pricing Models

  1. On-Demand – Pay by the hour/second (flexible).

  2. Reserved Instances – Commit 1–3 years, cheaper.

  3. Spot Instances – Cheap but can be interrupted anytime.

  4. Savings Plans – Flexible commitment for discounts.

EC2 Security

  • Use IAM roles instead of hardcoding keys.

  • Keep EC2 in private subnets if not needed on the internet.

  • Enable SSM Session Manager for secure login without SSH keys.

  • Patch regularly and monitor with Amazon Inspector + GuardDuty.

  • Encrypt data with EBS encryption and TLS for communication.

Key Points:

  • Always choose the right instance family:

    • T-series β†’ General use

    • M-series β†’ Balanced

    • C-series β†’ Compute-heavy

    • R-series β†’ Memory-heavy

    • G/P-series β†’ Graphics/ML

  • Tag instances for tracking costs.

  • Use CloudWatch to monitor CPU, memory, and network.

  • Take EBS snapshots for backup.

Instance Metadata

Instance metadata is data about your instance that you can use to configure or manage the running instance. Instance metadata is divided into categories, for example, host name, events, and security groups.

Last updated