Trang chủ/Chương 10

Chương 10: Serverless

Tổng Quan

Serverless cho phép bạn chạy code mà không cần quản lý server. AWS quản lý hoàn toàn hạ tầng, auto-scaling, và patching.


1. AWS Lambda

1.1 Đặc điểm

  • Chạy code theo sự kiện (event-driven).
  • Hỗ trợ: Node.js, Python, Java, C#, Go, Ruby, Custom Runtime.
  • Thời gian chạy tối đa: 15 phút.
  • RAM: 128 MB → 10 GB (CPU tăng tỷ lệ theo RAM).
  • Trả tiền: Theo số lượng requests + thời gian chạy (GB-seconds).
  • Free Tier: 1 triệu requests + 400,000 GB-seconds / tháng.

1.2 Các Event Sources phổ biến

SourceTrigger type
API GatewayHTTP request → Lambda
S3Object created/deleted → Lambda
DynamoDB StreamsData change → Lambda
SQSMessage in queue → Lambda
EventBridgeScheduled/event → Lambda
CloudWatch LogsLog event → Lambda
KinesisStream data → Lambda
SNSNotification → Lambda

1.3 Lambda Concurrency

  • Reserved Concurrency: Giữ riêng concurrency cho function cụ thể.
  • Provisioned Concurrency: Khởi tạo sẵn instances → không cold start.
  • Mặc định: 1,000 concurrent executions per Region (có thể xin tăng).

1.4 Lambda Layers

  • Chia sẻ code/libraries giữa nhiều Lambda functions.
  • Tối đa 5 layers per function.

1.5 Lambda trong VPC

  • Mặc định Lambda chạy ngoài VPC của bạn.
  • Nếu cần truy cập RDS/ElastiCache trong VPC → cấu hình Lambda trong VPC.
  • Lambda trong VPC cần NAT Gateway để truy cập Internet.

2. Amazon API Gateway

2.1 Đặc điểm

  • Tạo RESTful APIs hoặc WebSocket APIs.
  • Tích hợp với Lambda, HTTP backends, AWS services.
  • Hỗ trợ: authentication, rate limiting, caching, API versioning.

2.2 Endpoint Types

TypeMô tả
Edge-OptimizedQua CloudFront Edge (mặc định, cho global clients)
RegionalCho clients trong cùng Region
PrivateChỉ truy cập từ VPC qua VPC Endpoint

2.3 Security

  • IAM Roles/Policies (cho AWS users/services).
  • Cognito User Pools (cho external users).
  • Lambda Authorizer (custom auth logic).

3. AWS Step Functions

  • Orchestrate nhiều Lambda functions thành workflow (state machine).
  • Visual workflow với các bước: task, choice, parallel, wait, error handling.
  • Use case: Order processing, approval flow, ETL pipeline.
  • Thời gian chạy tối đa: 1 năm.

4. Amazon Cognito

4.1 User Pools

  • Đăng ký/Đăng nhập cho users (username/password, social login).
  • Trả về JWT tokens cho authentication.
  • Tích hợp với API Gateway.

4.2 Identity Pools (Federated Identities)

  • Cấp temporary AWS credentials cho users.
  • Cho phép users truy cập trực tiếp AWS resources (S3, DynamoDB).
  • Hỗ trợ: Cognito User Pools, Google, Facebook, SAML.
User ──login──▶ Cognito User Pool ──JWT──▶ API Gateway ──▶ Lambda
User ──login──▶ Cognito Identity Pool ──AWS credentials──▶ S3/DynamoDB (trực tiếp)

Exam Tips 💡

  1. Lambda timeout = 15 phút. Nếu workload chạy lâu hơn → dùng ECS/EC2 hoặc Step Functions.
  2. Lambda + API Gateway là combo Serverless API chuẩn.
  3. Lambda trong VPC cần NAT Gateway để truy cập Internet.
  4. Provisioned Concurrency giải quyết cold start.
  5. Cognito User Pool = Authentication. Identity Pool = Authorization (AWS credentials).
  6. Step Functions khi cần orchestrate nhiều Lambda functions.

Câu Hỏi Ôn Tập 📝

Câu 1: Lambda function cần truy cập RDS ở Private Subnet. Cần cấu hình gì?

Xem đáp án

Cấu hình Lambda trong VPC, chỉ định Private Subnets và Security Group cho phép connect đến RDS. Lambda sẽ tạo ENI trong subnets đó.

Câu 2: Ứng dụng mobile cần cho users đăng nhập bằng Google/Facebook và truy cập trực tiếp S3 bucket cá nhân. Dùng gì?

Xem đáp án

Cognito Identity Pool (Federated Identities). Users login qua social provider → nhận temporary AWS credentials → truy cập S3 trực tiếp với IAM policy giới hạn.


⬅️ Chương 09: CloudFront | Chương 11: Containers ➡️