Lesson 28

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.
Critical exam fact: You get only ONE chance to download the private key. AWS does not keep a copy. If lost, you cannot recover it and must create a new key pair.

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:

StateKey Facts
RunningFully booted, accessible, billing applies.
StoppedNo compute charges. EBS volumes persist. Instance Store data is lost. When restarted, the instance moves to a new physical host.
TerminatedCannot be recovered. Visible in the console temporarily, then deleted.
Exam tip: Stop an instance to save costs while keeping your EBS data. Terminate when you are done permanently.

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:

PillarWhat It MeansActions
1. Right SizeChoose 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 ElasticityReduce 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 ModelUse 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 ChoicesChoose 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.

1. When you launch a Linux EC2 instance and create a new key pair, how many chances do you have to download the private key file?
2. What is the primary purpose of an EC2 user data script?
3. A developer stops an EBS-backed EC2 instance on Friday to avoid weekend charges. When they start it on Monday, what is true?
4. A company assigns tags like "Project:Alpha" and "Department:Engineering" to every EC2 instance. What is the primary exam-relevant benefit?
5. A company uses m5.xlarge instances 24/7. CloudWatch shows average CPU utilization is only 15%. Which pillar of cost optimization should they apply first?
6. A company wants to automatically stop their development EC2 instances every night at 10 PM and restart them at 7 AM to save costs. Which of the Four Pillars does this practice represent?
Progress: 0/6 correct (0%). Answer all questions to see the final recommendation.
Primary Source: AWS Academy Module 6: Compute (module-6.txt) — Sections 2-3.
Last updated: June, 2026© 2026 Shahriar Ahmed ShovonCredits