Mục tiêu học tập
- Hiểu Lambda architecture và limits
- Nắm vững event-driven patterns
- Biết cách optimize Lambda performance
1. Lambda Architecture
2. Lambda Invocation
Invocation Types
1. SYNCHRONOUS (RequestResponse)
- Wait for function to complete
- Get result immediately
- Examples: API Gateway, ALB
2. ASYNCHRONOUS (Event)
- Fire and forget
- Automatic retry (2 times)
- Examples: S3, SNS, EventBridge
3. POLL-BASED (Event Source Mapping)
- Lambda polls source
- Batch processing
- Examples: SQS, Kinesis, DynamoDB Streams
Event Source Mapping
3. Lambda Performance
Cold Start vs Warm Start
Provisioned Concurrency
Memory & CPU Optimization
4. Lambda@Edge & CloudFront Functions
Comparison
5. Serverless Application Model (SAM)
SAM Template
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Globals:
Function:
Timeout: 30
Runtime: python3.12
Resources:
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.handler
CodeUri: ./src
Events:
Api:
Type: Api
Properties:
Path: /items
Method: GET
Policies:
- DynamoDBReadPolicy:
TableName: !Ref MyTable
6. Câu hỏi ôn tập
-
3 loại Lambda invocation là gì?
Xem đáp án
(1) Synchronous (RequestResponse) — caller đợi response, xử lý lỗi phía caller (API GW, ALB, Cognito). (2) Asynchronous (Event) — Lambda queue event, retry 2 lần nếu fail, DLQ cho unprocessed events (S3, SNS, EventBridge). (3) Event Source Mapping — Lambda poll source service (SQS, DynamoDB Streams, Kinesis, Kafka), batch processing. Error handling khác nhau per invocation type — quan trọng cho architect design.
-
Cold start xảy ra khi nào?
Xem đáp án
Cold start xảy ra khi không có warm execution environment sẵn sàng: (1) First invocation sau deploy, (2) Scale-out khi cần thêm concurrent executions, (3) Sau period of inactivity (environment bị reclaim). Cold start = download code + init runtime + run init code (
importstatements, DB connections, SDK clients) = 100ms đến vài giây. Giảm cold start: Provisioned Concurrency, tối ưu init code, dùng SnapStart (Java), lazy-load resources. -
Provisioned Concurrency giải quyết vấn đề gì?
Xem đáp án
Giải quyết cold start latency bằng cách giữ sẵn N execution environments warm và initialized — function respond ngay lập tức không có cold start delay. Phù hợp cho: latency-sensitive APIs (P99 requirements), functions với heavy init (ML models, connection pools), traffic spikes predictable (8 AM business hours). Chi phí: tính phí theo số provisioned concurrency × thời gian — kể cả khi idle. Trade-off: cost vs latency.
-
Lambda@Edge khác CloudFront Functions như thế nào?
Xem đáp án
Lambda@Edge: runs Node.js/Python, max 5-30 giây execution, có thể gọi network (DynamoDB, Secrets Manager), deployment package max 1 MB cho viewer triggers / 50 MB cho origin triggers, 4 event triggers (viewer/origin request/response). CloudFront Functions: JavaScript only, max 1 ms, NO network calls, 10 KB code limit, chỉ 2 triggers (viewer request/response). CloudFront Functions ~6× rẻ hơn, cho lightweight operations (header manipulation, URL rewrite, simple redirects). Lambda@Edge cho complex logic cần external data.
-
Event Source Mapping hoạt động như thế nào?
Xem đáp án
Event Source Mapping cho phép Lambda poll từ event sources: SQS, DynamoDB Streams, Kinesis Data Streams, Apache Kafka (MSK), self-managed Kafka. Lambda service (không phải function code) poll source và batch records → invoke Lambda function với batch. Config: BatchSize (1-10,000 records), BisectOnFunctionError (split batch khi fail), DestinationConfig (DLQ/DLS for failed records), Filter patterns. SQS: Lambda delete messages sau khi process thành công; Kinesis/DDB Streams: checkpointing tự động.
7. Bài tập thực hành
-
Lambda Power Tuning: deploy AWS Lambda Power Tuning (Step Functions state machine), chạy tuning cho một function CPU-bound với các mức memory 128MB–2048MB. Xác định điểm optimal cost/duration và đối chiếu với bảng ví dụ trong bài.
-
Event Source Mapping với SQS: tạo SQS queue + DLQ, cấu hình event source mapping BatchSize 10,
maxReceiveCount3. Gửi message lỗi có chủ đích, quan sát retry và message rơi vào DLQ; giải thích vì sao visibility timeout nên ≥ 6× function timeout. -
Provisioned Concurrency + alias: publish version, tạo alias
live, bật Provisioned Concurrency = 2 trên alias. Dùng script gọi đồng thời 10 request, so sánh P99 latency trước/sau (cold start biến mất trên 2 environment warm). -
SAM deploy: dùng template SAM ở mục 5 (
sam build && sam deploy --guided) tạo API GET /items đọc DynamoDB; sau đósam deleteđể dọn tài nguyên.
Tài liệu tham khảo chính thức
Ngày tiếp theo: S3 Advanced & Storage