Lesson 1Theory

IAM Fundamentals

IAM ใช้ควบคุม Authentication และ Authorization ของ AWS Account: ใครกำลังเรียก API, ต้องการทำ Action อะไร และทำกับ Resource ใด

Mental model Principal เรียก AWS API IAM ตอบว่า identity นั้นเป็นใคร และมีสิทธิ์ทำ action กับ resource หรือไม่
Entity choice User, Group, Role ใช้คนละงาน User คือ identity ระยะยาว, Group จัด permission, Role ใช้ temporary session
Default posture Role และ federation มาก่อน access key ลด credential ถาวร และทำให้ workload/human access audit ได้ง่ายขึ้น

IAM คืออะไร

AWS Identity and Access Management หรือ IAM เป็นบริการสำหรับควบคุม Access ไปยัง AWS Resource คุณใช้ IAM เพื่อสร้าง Identity, attach Policy และกำหนดว่า Principal สามารถทำ Action ใดได้บ้าง

IAM เป็น Global Service และมีลักษณะ Eventually Consistent ดังนั้นหลังสร้างหรือแก้ Policy อาจต้องรอสั้นๆ ก่อนการเปลี่ยนแปลงกระจายครบ อย่าออกแบบ production workflow ให้พึ่งพาการแก้ IAM แบบ real-time ระหว่าง request สำคัญ

Authentication และ Authorization

  • Authentication: พิสูจน์ว่าเป็นใคร เช่น password, MFA, access key, SSO หรือ temporary credentials
  • Authorization: ตรวจว่าหลังพิสูจน์ตัวตนแล้วมีสิทธิ์ทำ Action นั้นกับ Resource นั้นหรือไม่

IAM entities

Entityความหมายเหมาะกับ
Root UserIdentity สูงสุดของ Accountงาน Account-level ที่จำเป็นเท่านั้น
IAM UserIdentity ระยะยาวใน AccountLegacy หรือ use case ที่ใช้ Role ไม่ได้
IAM Groupรวม IAM User เพื่อ attach Policyจัด Permission ตามหน้าที่ของ User
IAM RoleIdentity ที่ถูก assume และได้ temporary credentialsWorkload, federation, cross-account, AWS service

Principal คืออะไร

Principal คือ identity ที่ส่ง request ไปยัง AWS เช่น IAM User, assumed-role session, federated user หรือ AWS service ใน Policy บางประเภท เช่น Resource-based Policy และ Trust Policy คุณต้องระบุ Principal เพื่อบอกว่าใครได้รับอนุญาต

Best practices

  • ใช้ IAM Identity Center สำหรับ human users เมื่อทำได้
  • ใช้ IAM Role สำหรับ workload แทนการฝัง Access Key
  • ไม่ใช้ Root User ทำงานประจำ และเปิด MFA เสมอ
  • แยก Permission ตามหน้าที่ ไม่ใช้ policy กว้างเกินจำเป็น

Common mistakes

  • คิดว่า IAM Group เป็น Principal แล้วนำไปใส่ Resource-based Policy
  • สร้าง IAM User ให้ application ทั้งที่ควรใช้ IAM Role
  • ให้ AdministratorAccess เป็นค่าเริ่มต้นกับทุกคน

Review questions

Before next lesson
  1. แยก authentication กับ authorization ได้
  2. บอกได้ว่า IAM Group ไม่ใช่ principal สำหรับ resource-based policy
  3. เลือก Role สำหรับ workload แทน IAM User access key ได้เมื่อเหมาะสม
  1. Authentication ต่างจาก Authorization อย่างไร?
  2. ทำไม Role เหมาะกับ workload มากกว่า IAM User?
  3. IAM Group ใช้ทำอะไร และมีข้อจำกัดอะไร?
Sources: What is IAM?