</>Học Dev
Bài học

Tuần 2 - Ngày 6: AWS Lambda Basics

Tuần 2 – Ngày 6

Tuần 2 - Ngày 6: AWS Lambda Basics

Mục tiêu học tập

  • Hiểu Lambda function: runtime, handler, IAM role
  • Nắm các loại triggers (event sources)
  • Phân biệt synchronous vs asynchronous invocation
  • Hiểu cold start, provisioned concurrency, container image

1. Tổng quan Lambda

AWS Lambda = serverless compute service. Chạy code mà không quản lý server.

Đặc điểm

  • Event-driven: chạy khi có event trigger
  • Pay per execution: tính theo số lần invoke + duration + memory
  • Auto-scaling: scale từ 0 đến hàng nghìn concurrent executions
  • No infrastructure management: AWS lo mọi thứ
  • 15 minutes timeout max per execution

Use case

  • API backend (qua API Gateway)
  • File processing (S3 trigger)
  • Stream processing (Kinesis, DynamoDB Streams)
  • Scheduled tasks (EventBridge cron)
  • IoT data processing
  • Real-time data transformation

Khi KHÔNG dùng Lambda

  • Long-running tasks (> 15 min) → ECS, EC2, Batch
  • Stateful applications → EC2
  • Heavy compute / GPU → EC2 GPU, SageMaker
  • Predictable steady traffic 24/7 → có thể dùng EC2 RI rẻ hơn

2. Lambda Function Components

LambdaFunctionRuntime:Python3.12,Node.js22,...Handler:index.handlerMemory:128MB-10,240MBTimeout:1s-900s(15min)Code:.zip(50MBdirect,250MBS3)hoccontainerimage(10GB)Environmentvars:Key-valuepairsIAMRole:ExecutionroleVPCconfig:Optional(subnet+SG)Layers:Shareddependencies

Runtimes (2025)

  • Node.js 18, 20, 22
  • Python 3.10, 3.11, 3.12, 3.13
  • Java 11, 17, 21
  • Go 1.x (custom runtime)
  • .NET 6, 8
  • Ruby 3.2, 3.3
  • Custom Runtime (Bash, Rust, etc. qua Runtime API)
  • Container Image (any runtime, up to 10 GB)

Memory & CPU

  • Memory: 128 MB → 10,240 MB (10 GB), bước 1 MB
  • CPU scale tỷ lệ với memory (không cấu hình riêng)
  • Tại ~1769 MB memory = 1 vCPU full

3. Triggers (Event Sources)

200+ AWS services có thể trigger Lambda. Phổ biến:

ServiceUse case
API GatewayHTTP/REST API backend
ALBHTTP backend (alternative to API GW)
S3File upload → process (thumbnail, virus scan)
DynamoDB StreamsReact to data change
Kinesis Data StreamsReal-time stream processing
SQSProcess queue messages
SNSProcess pub/sub messages
EventBridgeCron schedule, custom events
CognitoUser pool triggers (pre-signup, post-confirmation)
CloudFront Lambda@EdgeModify request/response at edge
IoT CoreIoT messages

4. Invocation Models

4.1 Synchronous (Sync)

  • Caller chờ response
  • Errors return ngay cho caller (caller phải retry)
Examples:
- API Gateway → Lambda
- ALB → Lambda
- AWS CLI invoke
- Cognito triggers
- Lambda function URL
- Lex, Step Functions

4.2 Asynchronous (Async)

  • Caller fire-and-forget
  • Lambda queue event internally
  • Lambda tự retry: 2 retries (3 total attempts)
  • Failed events → Dead Letter Queue (DLQ) (SQS/SNS) hoặc on-failure destination
Examples:
- S3 events
- SNS notifications
- EventBridge events
- CodeCommit, CodePipeline
- CloudWatch Logs subscription

4.3 Event Source Mapping (Polling)

  • Lambda poll từ stream/queue
  • Batch processing
  • Internal Lambda machinery, không cần code
Examples:
- Kinesis Data Streams
- DynamoDB Streams
- SQS (standard và FIFO)
- Amazon MQ
- Self-managed Kafka, MSK

5. Pricing

Tính phí 2 components

1. Request count

  • $0.20 per 1M requests
  • Free tier: 1M requests/month forever free

2. Duration

  • Tính theo GB-seconds (memory × duration)
  • $0.0000166667 per GB-second
  • Free tier: 400,000 GB-seconds/month (forever free)

Ví dụ

  • Function 512 MB chạy 1 second 1M times/month
  • = 1M × (512/1024) × 1 = 500,000 GB-seconds
  • Vì < 400,000 free, billable = 100,000 GB-seconds = ~$1.67
  • Cộng request cost: 1M = $0.20 (sau free tier)
  • Total ~$1.87/month

Free tier Lambda rất hào phóng — đa số side project chạy miễn phí.

6. Cold Start vs Warm Start

Cold Start

  • Lambda runtime mới được khởi tạo
  • Download code, init runtime, run init code
  • Lag: 100ms - 5s (Java/.NET chậm hơn Node/Python)
  • Xảy ra khi: lần đầu invoke, sau 5-15 phút idle, scale up

Warm Start

  • Reuse container đã có sẵn
  • Latency rất thấp (< 10ms typically)

Cách giảm Cold Start

Provisioned Concurrency

  • Pre-warm N executions, luôn sẵn sàng
  • Eliminate cold start cho first N concurrent invocations
  • Trả thêm phí $/GB-hour cho provisioned capacity

Lambda SnapStart (Java)

  • Snapshot pre-initialized Lambda execution environment
  • Cold start giảm từ ~3s xuống ~200ms cho Java
  • Free (no extra cost)

Best practices

  • Init outside handler (DB connection, SDK clients)
  • Minimize package size
  • Avoid VPC nếu không cần (VPC + ENI thêm cold start)
  • Dùng Node.js / Python (cold start nhỏ hơn Java)

7. Concurrency

Reserved Concurrency

  • Đặt giới hạn cho function
  • Lambda không vượt giới hạn này
  • 2 mục đích:
    • Cap: function "đói" không ảnh hưởng function khác (account limit shared)
    • Throttle protection: bảo vệ downstream (DB không bị quá tải)

Provisioned Concurrency

  • Pre-warmed executions
  • Không cold start
  • Có thể combine với Reserved Concurrency

Account-level Concurrency Limit

  • Default 1,000 concurrent executions per account per region
  • Có thể request tăng qua Service Quotas

Burst Concurrency

  • Khi cần scale lên nhanh, Lambda burst:
    • 3000 (Mỹ East/West, Europe)
    • 1000-3000 (regions khác)
  • Sau đó scale 500 concurrent/min

8. Layers

Định nghĩa

Layer = package chứa shared code/libraries dùng chung cho nhiều functions.

Use cases

  • Shared SDK (boto3 custom version)
  • Common utilities
  • Native binaries (FFmpeg, ImageMagick)
  • Custom runtime (chạy Rust, Bash...)

Limits

  • 5 layers per function
  • Total unzipped size (function + layers): 250 MB

Workflow

# Tạo layer
zip -r layer.zip python/  # boto3 in python/ folder
aws lambda publish-layer-version \
  --layer-name shared-libs \
  --zip-file fileb://layer.zip \
  --compatible-runtimes python3.12

# Attach layer
aws lambda update-function-configuration \
  --function-name my-function \
  --layers arn:aws:lambda:us-east-1:111111111111:layer:shared-libs:1

9. Container Image Support

Đặc điểm

  • Đóng gói Lambda dưới dạng Docker image (up to 10 GB)
  • Push lên ECR (Elastic Container Registry)
  • Lambda pull image khi cold start

Dockerfile Example

FROM public.ecr.aws/lambda/python:3.12
COPY app.py ${LAMBDA_TASK_ROOT}/
COPY requirements.txt .
RUN pip install -r requirements.txt
CMD ["app.handler"]

Use cases

  • Heavy dependencies (ML model, native libraries)
  • CI/CD đã quen Docker workflow
  • Container image > 250 MB

10. Lambda Networking

Default (no VPC)

  • Lambda chạy trong AWS-managed VPC
  • Có internet access mặc định
  • Nhanh cold start
  • KHÔNG access VPC private resources (RDS private, ElastiCache)

VPC Configuration

  • Chỉ định subnet + SG
  • Lambda tạo ENI trong subnet (Hyperplane ENI — shared, fast)
  • Cần NAT Gateway trong VPC để Lambda access internet (vì subnet là private)
  • Cold start slower lần đầu (đã giảm nhiều từ 2019)

Best practice VPC

  • Lambda trong private subnet
  • Route 0.0.0.0/0 → NAT Gateway (cho internet)
  • Dùng VPC Endpoints cho AWS services (S3, DynamoDB) để tránh data transfer cost

11. Lambda Destinations (Async)

Khi async invocation fail (sau 2 retries) hoặc success

Có thể gửi event đến destination:

  • SQS queue
  • SNS topic
  • Lambda function khác
  • EventBridge bus

vs DLQ

  • Destinations hỗ trợ cả success + failure, nhiều target types
  • DLQ chỉ failure, chỉ SQS/SNS
  • AWS recommend Destinations thay DLQ

12. Lambda Function URL

Định nghĩa

Function URL = dedicated HTTP(S) endpoint cho Lambda function, không cần API Gateway.

Đặc điểm

  • Format: https://<url-id>.lambda-url.<region>.on.aws/
  • Auth: NONE (public) hoặc AWS_IAM
  • CORS support built-in
  • Không có rate limiting, không có custom domain

Khi nào dùng Function URL vs API Gateway?

  • Function URL: Đơn giản, low-cost, ít features
  • API Gateway: Cần rate limit, custom domain, authorization complex (Cognito, Lambda authorizer), throttling, caching

Câu hỏi ôn tập

  1. Lambda timeout tối đa là bao lâu?

    Xem đáp án

    15 phút (900 giây). Default timeout là 3 giây — cần tăng nếu function cần lâu hơn. Workload vượt 15 phút cần dùng dịch vụ khác: EC2, Fargate, hoặc Step Functions để orchestrate nhiều Lambda invocations. Memory range: 128 MB đến 10,240 MB (10 GB).

  2. CPU của Lambda được scale theo gì?

    Xem đáp án

    CPU scale tỷ lệ thuận với memory. Bạn không cấu hình CPU trực tiếp — chỉ cấu hình memory. Tại 1,769 MB, function có 1 full vCPU. Tại 3,538 MB có 2 vCPUs, v.v. Nếu function CPU-bound và cần nhanh hơn, tăng memory (dù không cần RAM) để tăng CPU allocation — chi phí tăng nhưng execution time giảm, tổng chi phí có thể không đổi hoặc rẻ hơn.

  3. Khi async invoke fail, Lambda retry mấy lần?

    Xem đáp án

    2 lần retry (tổng 3 lần attempt). Sau lần đầu fail: retry lần 1 sau 1 phút, retry lần 2 sau 2 phút. Sau 3 lần đều fail, event bị discarded (hoặc gửi đến DLQ/EventBridge nếu cấu hình). Synchronous invoke: không retry tự động — caller nhận lỗi và tự quyết định retry. SQS trigger: retry theo SQS visibility timeout và maxReceiveCount.

  4. Provisioned Concurrency giải quyết vấn đề gì?

    Xem đáp án

    Giải quyết cold start latency. Khi Lambda function chưa có warm instance, lần invocation đầu phải khởi tạo execution environment (download code, init runtime, run init code) — mất 100ms-1s+. Provisioned Concurrency giữ sẵn N instances warm và initialized, sẵn sàng phục vụ requests ngay lập tức. Chi phí: tính phí cho cả thời gian "provisioned" dù không có request — trade-off cost vs latency.

  5. Khi nào nên dùng container image thay vì zip cho Lambda?

    Xem đáp án

    Dùng container image khi: (1) Deployment package lớn hơn 250 MB (zip limit) — container image hỗ trợ đến 10 GB, (2) Cần custom runtime, system libraries, hay binaries không có trong standard Lambda runtimes, (3) Team đã quen với Docker workflow và muốn reuse Dockerfile, (4) Cần test locally với behavior giống production bằng Lambda Runtime Interface Emulator (RIE).

Bài tập thực hành

  • Tạo Lambda function Python "hello world" với memory 256 MB
  • Tạo trigger từ S3 bucket → Lambda log filename khi có file upload
  • Setup API Gateway → Lambda (REST API endpoint trả JSON)
  • Enable Provisioned Concurrency = 1, observe cold start before/after
  • Tạo Layer chứa requests library, attach vào function
  • Setup EventBridge schedule cron(0/5 * * * ? *) → Lambda chạy mỗi 5 phút

Tài liệu tham khảo chính thức


Tiếp theo: Quiz Tuần 2