AWS Identity and Access Management
Ai được làm gì
Quản lý danh tính (user, role) và chính sách (policy) quyết định ai được gọi API nào trên tài nguyên nào.
- Chi phí$ Rẻ
- Độ khó●●●●●
- Để không có tốn tiền?Không
- AWS quản lý
- Có trong đềCLF · SAA · DVA · SOA
Hình dungNhư hệ thống thẻ từ toà nhà: mỗi thẻ mở được những cửa nhất định.
IAM đứng ở đâu trong hệ thống?
Một ví dụ thật, từng bước
- Công ty mới mở tài khoản AWS; ai cũng đang dùng chung tài khoản root, rất nguy hiểm.
- Bạn bật MFA cho root, cất root đi, rồi tạo nhóm Developers chỉ có quyền với EC2, S3 và Lambda.
- Ứng dụng chạy trên EC2 cần đọc S3: bạn gắn IAM role cho máy thay vì ghi access key vào file cấu hình.
- AWS tự cấp khoá tạm thời cho role và đổi định kỳ, nên không có khoá cố định nào bị lộ trong code.
- Một policy có Deny xoá bucket production: kể cả có quyền khác cho phép, lệnh xoá vẫn bị chặn.
Khi nào nên dùng
✓ Nên dùng khi
Luôn có: mọi lời gọi tới AWS đều qua IAM.
✗ Không nên dùng khi
Đăng nhập cho người dùng cuối của ứng dụng → Cognito.
Ví dụ hay gặp
- Role cho EC2/Lambda/ECS gọi AWS API mà không cần access key
- Phân quyền tối thiểu (least privilege) cho người và ứng dụng
- Truy cập chéo tài khoản bằng AssumeRole
IAM, IAM Identity Center hay Cognito?
| Dành cho | Quyền trong một tài khoản AWS (người, dịch vụ) | Nhân viên đăng nhập một lần vào nhiều tài khoản | Người dùng cuối của ứng dụng |
|---|---|---|---|
| Danh tính | User, group, role | Người dùng từ thư mục công ty | User pool, đăng nhập mạng xã hội |
| Thông tin đăng nhập | Mật khẩu, access key, role tạm thời | Quyền tạm thời theo permission set | Token JWT |
| Phí | Miễn phí | Miễn phí | Theo số người dùng hoạt động |
Tốn bao nhiêu tiền?
- Cách tính: Miễn phí.
- Miễn phí: Miễn phí.
Không tốn tiền trực tiếp, nhưng quyền quá rộng có thể gây hoá đơn khổng lồ khi bị lộ khoá (bị dùng để đào coin). Dùng role thay cho access key dài hạn.
Dịch vụ AWS (EC2, Lambda) cần quyền → IAM role, không bao giờ để access key trong code. Deny rõ ràng luôn thắng Allow. Cho tài khoản khác truy cập → role cho phép assume chéo tài khoản. Đăng nhập người dùng app → Cognito, không phải IAM.
Thử sức với câu hỏi thi thật
1A company is planning to run a group of Amazon EC2 instances that connect to an Amazon Aurora database. The company has built an AWS CloudFormation template to deploy the EC2 instances and the Aurora DB cluster. The company wants to allow the instances to authenticate to the database in a secure way. The company does not want to maintain static database credentials. Which solution meets these requirements with the LEAST operational effort?
- ACreate a database user with a user name and password. Add parameters for the database user name and password to the CloudFormation template. Pass the parameters to the EC2 instances when the instances are launched.
- BCreate a database user with a user name and password. Store the user name and password in AWS Systems Manager Parameter Store. Configure the EC2 instances to retrieve the database credentials from Parameter Store.
- CConfigure the DB cluster to use IAM database authentication. Create a database user to use with IAM authentication. Associate a role with the EC2 instances to allow applications on the instances to access the database.
- DConfigure the DB cluster to use IAM database authentication with an IAM user. Create a database user that has a name that matches the IAM user. Associate the IAM user with the EC2 instances to allow applications on the instances to access the database.
Xem đáp án
Đáp án đúng: C
Bật IAM database authentication cho Aurora và gắn IAM role cho EC2: ứng dụng lấy token tạm thời, không phải lưu mật khẩu cố định. Dùng IAM user gắn vào EC2 là sai; EC2 nhận quyền qua role.
2An administrator is using Amazon CloudFormation to deploy a three tier web application that consists of a web tier and application tier that will utilize Amazon DynamoDB for storage. When creating the CloudFormation template, which of the following would allow the application instance access to the DynamoDB tables without exposing API credentials?
- ACreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and associate the Role to the application instances by referencing an instance profile.
- BUse the Parameter section in the Cloud Formation template to have the user input Access and Secret Keys from an already created IAM user that has the permissions required to read and write from the required DynamoDB table.
- CCreate an Identity and Access Management Role that has the required permissions to read and write from the required DynamoDB table and reference the Role in the instance profile property of the application instance.
- DCreate an identity and Access Management user in the CloudFormation template that has permissions to read and write from the required DynamoDB table, use the GetAtt function to retrieve the Access and secret keys and pass them to the application instance through user-data.
Xem đáp án
Đáp án đúng: C
Tạo IAM role có quyền đọc ghi bảng DynamoDB và tham chiếu qua thuộc tính instance profile của instance trong template. Không đưa access key vào tham số hay sinh access key trong template.
Câu hỏi thường gặp
IAM user, group và role khác nhau thế nào?
User là danh tính lâu dài có mật khẩu hoặc access key. Group gom user để gán quyền chung. Role không có mật khẩu cố định mà được "đảm nhận" để nhận quyền tạm thời, dùng cho dịch vụ AWS (như EC2, Lambda), cho tài khoản khác hoặc cho người đăng nhập qua SSO.
Có nên dùng tài khoản root hằng ngày không?
Không. Root có toàn quyền và không bị giới hạn bởi policy. Chỉ dùng root cho vài việc bắt buộc, bật MFA, không tạo access key cho root, và dùng user hoặc SSO với quyền tối thiểu cho công việc hằng ngày.
IAM có tính phí không?
Không. IAM miễn phí; bạn chỉ trả cho tài nguyên mà các danh tính đó sử dụng.
Đề thi AWS hay hỏi gì về IAM?
Nguyên tắc quyền tối thiểu, cách đánh giá policy (Deny rõ ràng luôn thắng), dùng role cho EC2 hay Lambda thay vì để access key trong code, truy cập chéo tài khoản bằng role, MFA, và permission boundary.