Users & Groups
IAM User คือบัญชีที่สร้างขึ้นสำหรับ คนหรือ service ที่ต้องการเข้าถึง AWS — ส่วน IAM Group คือกลุ่มของ Users ที่รวมกันเพื่อกำหนด permission ได้ทีเดียว
- 1 User ต่อ 1 คน (ไม่ควร share)
- User สามารถเข้าถึง AWS ได้ 2 วิธี: Management Console (password) และ CLI / SDK (Access Key)
- User ที่สร้างใหม่จะไม่มี permission ใดๆ เลยโดย default
- Root user มีสิทธิ์ทุกอย่าง — ควรเปิดใช้แค่เมื่อจำเป็น และ enable MFA ทันที
- Group มีได้แค่ Users — ไม่สามารถใส่ Group ซ้อน Group ได้ (no nested groups)
- User 1 คน สามารถอยู่ได้หลาย Group หรือไม่อยู่ Group เลยก็ได้
- ตัวอย่าง: กลุ่ม Developers, กลุ่ม Admins, กลุ่ม Finance
Group: Developers → Alice, Bob
Group: Admins → Carol
User: Dave → ไม่อยู่ group ไหน (ได้ permission เฉพาะที่ assign โดยตรง)Roles
IAM Role คือชุด permission ที่ ไม่ได้ผูกกับคน แต่ผูกกับ entity ที่ต้องการใช้งาน เหมือน "บทบาท" ที่ใครก็ได้มาสวมชั่วคราว
- ใช้สำหรับ AWS Services เช่น EC2 ที่ต้องการ access S3
- ใช้สำหรับ Cross-account access — ให้บัญชี AWS อื่นเข้าถึงได้
- ใช้สำหรับ Federated users เช่น login ด้วย Google / Active Directory
- Role ใช้ไม่มี long-term credentials — ได้ temporary security credentials แทน
Use Case: EC2 → S3
สร้าง Role ชื่อ EC2-S3-Access แล้ว attach กับ EC2 instance แทนการใส่ Access Key ลงใน code
Use Case: Lambda → DynamoDB
Lambda function รับ Role ที่มี permission อ่าน/เขียน DynamoDB โดยไม่ต้องใช้ user
Policies
Policy คือ JSON document ที่กำหนดว่า อนุญาตหรือห้าม ทำอะไรกับ resource ใดบ้าง — attach กับ User, Group หรือ Role ได้
Identity-based Policy
Attach กับ User, Group, หรือ Role — กำหนดว่า identity นั้นทำอะไรได้
Resource-based Policy
Attach กับ resource เช่น S3 bucket — กำหนดว่าใครเข้า resource นี้ได้
AWS Managed Policy
AWS สร้างให้สำเร็จรูป เช่น AdministratorAccess, ReadOnlyAccess
Customer Managed Policy
เราสร้างเองตามความต้องการ, reusable ข้าม User/Role
โครงสร้าง Policy JSON ประกอบด้วย: Version (เวอร์ชั่น policy language), Id (ระบุ policy — optional), และ Statement (1 หรือหลาย statement)
Sid
Statement ID — ตัวระบุ statement (optional) ช่วยให้อ่านง่ายขึ้น
Effect
Allow หรือ Deny — statement นี้อนุญาตหรือปฏิเสธ access
Principal
บัญชี/User/Role ที่ policy บังคับใช้ (ส่วนมากใช้ใน resource-based policy)
Action
รายการของ API call ที่ allow/deny เช่น s3:GetObject, ec2:StartInstances
Resource
รายการ resource ที่ action มีผล (เขียนเป็น ARN)
Condition
เงื่อนไขเพิ่มเติม (optional) — เช่นจำกัด IP, เวลา, MFA
{
"Version": "2012-10-17",
"Id": "S3-Account-Permissions",
"Statement": [
{
"Sid": "AllowReadObjects",
"Effect": "Allow",
"Principal": {
"AWS": ["arn:aws:iam::123456789012:root"]
},
"Action": [
"s3:GetObject",
"s3:ListBucket"
],
"Resource": "arn:aws:s3:::my-bucket/*"
}
]
}MFA (Multi-Factor Authentication)
MFA = Password ที่คุณรู้ + Security Device ที่คุณมี — ถึงแม้ password ถูกขโมยบัญชีก็ยังปลอดภัย เพราะต้องมี device ด้วย
Virtual MFA Device
แอปบนมือถือ เช่น Google Authenticator (ต่อ 1 device) หรือ Authy (รองรับ multi-device)
U2F Security Key
อุปกรณ์ USB ทางกายภาพ เช่น YubiKey by Yubico (3rd party) — รองรับ root และ IAM users หลายคนบน device เดียว
Hardware Key Fob MFA Device
อุปกรณ์พวงกุญแจ จาก Gemalto — แสดง OTP บนหน้าจอเล็กๆ
Hardware Key Fob (AWS GovCloud)
รุ่นเฉพาะสำหรับ AWS GovCloud (US) — จาก SurePassID
Password Policy
Password ที่แข็งแรง = บัญชีปลอดภัยมากขึ้น — IAM ให้ตั้ง Password Policy ระดับ account ได้
- ตั้ง minimum password length (ความยาวขั้นต่ำ)
- บังคับประเภทตัวอักษร: uppercase, lowercase, number, non-alphanumeric (special character)
- อนุญาตหรือบังคับให้ผู้ใช้เปลี่ยน password ของตัวเองได้ (allow users to change own password)
- ตั้ง password expiration — บังคับเปลี่ยน password ทุกๆ กี่วัน
- ห้ามใช้ password ซ้ำของเก่า (prevent password re-use)
Access Keys, CLI & SDK
มี 3 วิธีในการเข้าถึง AWS:
1. AWS Management Console
เข้าผ่าน web browser — ป้องกันด้วย password + MFA
2. AWS CLI
Command Line Interface — ป้องกันด้วย Access Keys
3. AWS SDK
Software Development Kit (เรียกจากโค้ด) — ป้องกันด้วย Access Keys
AWS CLI คืออะไร?
เครื่องมือ command-line สำหรับสั่งงาน AWS services ผ่าน terminal/shell — เขียน script ทำ automation ได้ เป็น open-source (github.com/aws/aws-cli)
AWS SDK คืออะไร?
ชุด library สำหรับภาษา programming (JavaScript, Python, Java, .NET, ...) — เรียก AWS API ได้จาก application code จริงๆแล้ว AWS CLI ก็สร้างบน Python SDK (boto3)
Security Tools
IAM มีเครื่องมือสำหรับตรวจสอบความปลอดภัย 2 ตัวหลัก:
IAM Credentials Report (Account-level)
รายงาน CSV ที่แสดงข้อมูล credential ของทุก user ในบัญชี — บอกว่า user ไหนเปิด MFA, password/access key ถูกใช้ล่าสุดเมื่อไร ใช้สำหรับ audit security compliance
IAM Access Advisor (User-level)
แสดงว่า service ที่ user มี permission ถูกใช้งานจริงครั้งล่าสุดเมื่อไร — ใช้ระบุ permission ที่ไม่ได้ใช้งานแล้วลบออกตาม Least Privilege
Best Practices
- อย่าใช้ root account ยกเว้นตอน setup บัญชี AWS ครั้งแรกเท่านั้น
- ห้าม share root account — สร้าง IAM user แยกต่างหาก 1 คนต่อบัญชีจริง
- เปิด MFA สำหรับ root account และ user ทุกคนที่สำคัญ
- Assign permission ผ่าน Group แทนการ assign ให้ User โดยตรง
- ใช้ Role สำหรับ AWS services แทนการเก็บ Access Key ใน code
- ตั้ง strong password policy และบังคับการเปลี่ยน password ตามรอบ (rotation)
- Review permission เป็นประจำด้วย Credentials Report & Access Advisor
- ห้าม embed Access Key ลงใน application code โดยเด็ดขาด
- Rotate Access Keys อย่างสม่ำเสมอ