Lần đầu deploy Lambda, mình deploy xong function rỗng mà mất 15 phút để hiểu tại sao không gọi được từ API Gateway. EC2: bạn chọn instance type, cài OS, mở port, lo security patch. Lambda: bạn chỉ viết function, AWS lo mọi thứ. Không server để quản lý, không idle cost, scale từ 0 đến hàng nghìn concurrent execution trong vài giây.

Nhưng “không server” không có nghĩa là “không vấn đề”. Cold start, timeout 15 phút, concurrency limit 1,000 — những thứ này vẫn tồn tại, chỉ khác cách giải quyết.


  flowchart LR
    Event["Event Sources<br/>API GW · S3 · SQS · EventBridge"]
    Lambda["Lambda Function<br/>handler(event, context)"]
    Role["Execution Role<br/>IAM policy"]
    Logs["CloudWatch Logs<br/>console.log → auto stream"]
    Event --> Lambda
    Role --> Lambda
    Lambda --> Logs

Memory quyết định CPU + Network + Cold Start

Lambda chỉ có 1 setting: memory (128MB - 10GB). CPU, network bandwidth, và cold start time tỉ lệ thuận:

MemoryvCPU (xấp xỉ)Cold Start (Node.js ~)Use Case
128 MB0.07~800msHello world, lightweight transform
512 MB0.29~400msAPI handler đơn giản
1024 MB0.58~300msAPI có logic, image resize
2048 MB1.16~200msData processing, PDF gen
4096 MB2.32~150msVideo, ML inference

Lưu ý: AWS cấp 1 vCPU đầy đủ tại 1,769 MB. Các giá trị vCPU trên được tính theo tỉ lệ (1769 MB = 1 vCPU). Trên thực tế, cold start time thay đổi 2-10× tùy package size, VPC config, runtime initialization — bảng trên chỉ mang tính tham khảo cho Node.js deployment tối giản không VPC.

Cold start xảy ra khi Lambda cần khởi tạo execution environment mới. Nguyên nhân: deploy mới, scale-out (thêm instance), hoặc instance cũ bị recycle sau 5-15 phút idle.

Tăng memory để giảm cold start — đôi khi rẻ hơn. Ví dụ: 256MB × 1000ms = 0.256 GB-s, trong khi 1024MB × 200ms = 0.2048 GB-s — tăng memory vừa nhanh hơn vừa rẻ hơn. Cost = memory × duration — tối ưu cho total cost, không chỉ memory.

Function anatomy + execution role

import { Handler } from "aws-lambda";

export const handler: Handler = async (event, context) => {
  console.log("Request:", context.awsRequestId);
  console.log("Remaining:", context.getRemainingTimeInMillis(), "ms");

  return { statusCode: 200, body: JSON.stringify({ ok: true }) };
};
zip -r function.zip index.ts node_modules/
aws lambda create-function --function-name hello-world \
  --runtime nodejs22.x --role arn:aws:iam::123456789012:role/lambda-basic \
  --handler index.handler --zip-file fileb://function.zip \
  --memory-size 256 --timeout 10

Execution role = IAM role function dùng để gọi AWS API. Luôn least privilege — không AdministratorAccess. Policy tối thiểu: AWSLambdaBasicExecutionRole (ghi CloudWatch Logs).


Event types: mỗi service format khác nhau

SourceEvent InterfaceKey Fields
API Gateway v2APIGatewayProxyEventV2requestContext.http.method, path, body
S3S3EventRecords[].s3.bucket.name, object.key
SQSSQSEventRecords[].body, messageId, attributes.ApproximateReceiveCount
EventBridgeEventBridgeEvent<string, any>detail-type, source, detail
DynamoDB StreamDynamoDBStreamEventRecords[].eventName (INSERT/MODIFY/REMOVE)

SAM CLI: local dev

sam init --runtime nodejs22.x
sam local invoke HelloWorldFunction --event events/test.json
sam local start-api --port 3000
sam build && sam deploy --guided

Bốn điều cần nhớ trước khi viết function đầu tiên

Lambda không loại bỏ infrastructure, nó chỉ đẩy infrastructure thành những ràng buộc vô hình. Cold start, timeout 15 phút, concurrency limit 1,000 — những thứ này vẫn tồn tại, bạn chỉ không thấy server để blame thôi.

Bài sau: Phần 11: Lambda nâng cao — VPC, concurrency, Powertools