EC2 Fundamentals
Amazon EC2 คือ virtual server บน AWS ที่คุณควบคุม OS, runtime, application, network และ storage ได้ละเอียดกว่า managed compute หลายแบบ
Amazon EC2 คืออะไร
Amazon Elastic Compute Cloud หรือ Amazon EC2 ให้ on-demand scalable computing capacity ใน AWS Cloud คุณ launch virtual servers ได้ตามต้องการ เลือก AMI, Instance Type, VPC/Subnet, Security Group, Storage และ IAM Role เอง
EC2 launch decisions
AMI -> Instance Type -> VPC/Subnet -> Security Group -> Storage -> IAM Role -> User Data -> Tags
AMI และ Instance Type
AMI คือ template ที่มี OS และ configuration พื้นฐานสำหรับสร้าง instance ส่วน Instance Type กำหนด CPU, memory, networking, storage capability และบาง family มี GPU หรือ local NVMe storage
| Family | จุดเด่น | ตัวอย่างงาน |
|---|---|---|
t | Burstable general purpose | dev, website ขนาดเล็ก |
m | สมดุล CPU / Memory | application server ทั่วไป |
c | Compute optimized | batch, CPU-heavy API |
r | Memory optimized | cache, database ที่ใช้ RAM สูง |
g, p | GPU / accelerated | ML, rendering, video processing |
Pricing models
- On-Demand: จ่ายตามการใช้งาน ไม่ผูกสัญญา เหมาะกับ Lab และ workload ที่ยังไม่แน่นอน
- Savings Plans / Reserved Instances: ลด cost เมื่อมี usage ต่อเนื่องและคาดการณ์ได้
- Spot Instances: ใช้ unused capacity ราคาถูกกว่า แต่ถูก interrupt ได้ เหมาะกับงานทน interruption
User Data, Instance Metadata และ Instance Profile
User Data คือ script ที่รันตอน boot เพื่อ bootstrap instance เช่นติดตั้ง Nginx หรือ deploy config Instance Metadata ให้ instance อ่านข้อมูลของตัวเอง เช่น instance ID หรือ IAM role credentials ผ่าน metadata endpoint Instance Profile คือ container ที่ทำให้ EC2 ใช้ IAM Role ได้
Access: SSH vs Session Manager
Key Pair ใช้ SSH เข้า Linux instance ได้ แต่ต้องเปิด inbound port และดูแล private key Systems Manager Session Manager ช่วยเชื่อมต่อ instance ผ่าน IAM โดยไม่ต้องเปิด inbound SSH port, ไม่ต้องมี bastion host และสามารถเก็บ session logs ได้
Common mistakes
- เปิด SSH จาก
0.0.0.0/0แทนการใช้ Session Manager หรือจำกัด source - เลือก instance ใหญ่เกินโดยไม่ดู CloudWatch metrics
- ฝัง Secret ใน User Data หรือ AMI
- ใช้ IAM user access key บน instance แทน Instance Profile
Review questions
- อธิบาย EC2 launch decisions ตั้งแต่ AMI ถึง tags ได้
- เลือก instance family เบื้องต้นตาม workload ได้
- รู้ว่าควรใช้ instance profile และ Session Manager แทน access key/inbound SSH เมื่อทำได้
- AMI ต่างจาก EC2 Instance อย่างไร?
- Instance Type ส่งผลต่อ resource ใดบ้าง?
- Session Manager ช่วยลดความเสี่ยงของ inbound SSH อย่างไร?