EC2 Launch and Cost Optimization
Key pairs, user data, tags, instance lifecycle, and the Four Pillars of Cost Optimization
1. Key Pairs
Amazon EC2 uses public-key cryptography to secure instance access. When you launch an instance, AWS stores the public key and gives you a one-time download of the private key file.
- Linux: Use the private key with SSH to connect.
- Windows: Use the private key to decrypt the administrator password, then connect via RDP.
2. User Data Scripts
At launch, you can pass a user data script that runs automatically on the first boot. It handles installation and configuration tasks with root/admin privileges — for example, applying patches or installing software.
3. Instance Lifecycle
Every EC2 instance moves through states from launch to termination:
| State | Key Facts |
|---|---|
| Running | Fully booted, accessible, billing applies. |
| Stopped | No compute charges. EBS volumes persist. Instance Store data is lost. When restarted, the instance moves to a new physical host. |
| Terminated | Cannot be recovered. Visible in the console temporarily, then deleted. |
4. Tags
A tag is a key-value label you assign to AWS resources. The most important exam use is cost allocation: activate tags in the Billing console, and AWS groups your costs by tag — by department, project, or environment — in cost reports.
Tags also enable filtering resources in the console, automation via scripts, and access control in IAM policies. Develop a consistent tagging strategy early; it is difficult to retroactively tag resources.
5. The Four Pillars of Cost Optimization
AWS identifies four drivers for optimizing EC2 costs. These are tested across both the Compute and Billing domains:
| Pillar | What It Means | Actions |
|---|---|---|
| 1. Right Size | Choose the cheapest instance that meets performance needs. | Review CPU/RAM/storage utilization with CloudWatch. Downsize over-provisioned instances. Right-size first, then reserve for maximum savings. |
| 2. Increase Elasticity | Reduce idle capacity by matching resources to demand. | Stop non-production instances outside business hours (saves up to 65%). Use Auto Scaling for production. Target 20-30% On-Demand or Spot. |
| 3. Optimal Pricing Model | Use the right mix of purchase types for your usage patterns. | On-Demand + Spot for variable workloads. Reserved Instances or Savings Plans for predictable steady-state. Consider if Lambda can replace EC2 entirely. |
| 4. Optimize Storage Choices | Choose the least expensive storage that meets performance needs. | Resize over-provisioned EBS volumes. Change volume types (st1 costs ~50% less than gp2). Delete unused snapshots. Use S3 with lifecycle policies instead of EBS when possible. |
EC2 Launch and Cost Optimization Quiz
Select one answer per question. You will receive immediate feedback.