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

Tuần 4 - Ngày 5: Orchestration — Step Functions, MWAA, Glue Workflows, EventBridge

Tuần 4 – Ngày 5

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

  • Hiểu Step Functions: Standard vs Express, error handling, Map/Distributed Map
  • Biết MWAA (Airflow managed) và khi nào chọn thay Step Functions
  • Nắm Glue Workflows và EventBridge (rules + Scheduler)
  • Chọn đúng orchestrator theo tín hiệu đề bài

1. AWS Step Functions

Khái niệm

State machine (định nghĩa ASL — JSON) điều phối các bước: gọi hơn 220 service trực tiếp (Glue, Lambda, EMR, Athena, ECS...), rẽ nhánh, song song, retry — serverless, không hạ tầng.

{Start}ValidateInput(Lambda)RunGlueJob(glue:startJobRun.sync).sync=chjobxongChoice:DQpass?yesRunCrawlerAthenaCTASSuccessnoSNSPublish(alert)Fail(mistatecóRetry/Catchriêng)

Standard vs Express

StandardExpress
Thời gian chạyTới 1 nămTới 5 phút
SemanticsExactly-once workflowAt-least-once
GiáTheo state transitionTheo số lần chạy + duration
Use caseETL pipeline, chờ job dàiEvent processing volume lớn, ngắn

Tính năng cho data pipeline

  • .sync integration: start job và chờ hoàn thành (Glue, EMR steps, Athena...) — không phải tự poll
  • Retry/Catch per state: backoff, max attempts, nhánh xử lý lỗi — điểm mạnh nhất
  • Map state: lặp song song trên mảng input; Distributed Map: fan-out tới hàng triệu item (vd mỗi object S3 một lần xử lý) với concurrency điều khiển được
  • Wait / callback (task token): chờ phê duyệt/human step

2. MWAA — Managed Workflows for Apache Airflow

  • Airflow managed: DAG viết bằng Python, kho operators khổng lồ (AWS, GCP, Snowflake, dbt, Spark...), UI theo dõi quen thuộc
  • AWS quản scheduler/worker/webserver (scale theo environment class); DAG để trên S3; hỗ trợ Airflow 2.x và 3.x (MWAA hỗ trợ Airflow 3.0 từ 10/2025)
  • Chi phí: environment chạy liên tục (trả theo giờ) — không scale-to-zero
  • Chọn khi: đã có Airflow DAGs (migrate), cần cross-platform/hybrid orchestration, đội quen Python-as-code
  • Step Functions vs MWAA: serverless + tích hợp AWS sâu + pay-per-use vs hệ sinh thái Airflow + portability

3. Glue Workflows

  • Orchestration nội bộ Glue: chuỗi crawler + job + trigger (schedule/event/conditional), truyền run properties
  • Nhìn được graph trong console; kích hoạt bởi EventBridge event (vd file S3 đến)
  • Giới hạn: chỉ điều phối crawler/job Glue — không gọi Lambda/EMR/service khác
  • Chọn khi: pipeline thuần Glue đơn giản, không muốn thêm dịch vụ

4. EventBridge — trigger và schedule

  • Rules: pattern matching event (vd s3:ObjectCreated qua EventBridge, Glue job state change FAILED → SNS)
  • EventBridge Scheduler: cron/rate managed, timezone, one-time schedule — thay cron server; kích hoạt hầu hết API AWS
  • Pipes: point-to-point nguồn (SQS, Kinesis, DynamoDB streams) → đích, kèm filter/enrichment
  • EventBridge không phải orchestrator — nó là event router/scheduler; kết hợp: EventBridge trigger → Step Functions điều phối

5. Chọn orchestrator — bảng quyết định

Tín hiệu đề bàiChọn
Serverless, retry/branching, gọi nhiều AWS services, chờ job dàiStep Functions (Standard)
"Existing Airflow DAGs", "Python-based workflows", đa nền tảngMWAA
Pipeline chỉ gồm Glue crawlers + jobsGlue Workflows
Chạy X theo lịch cron đơn giảnEventBridge Scheduler (+ đích là job/SFN)
Xử lý hàng triệu file S3 song song có kiểm soátStep Functions Distributed Map
Event volume cao, mỗi workflow < 5 phútStep Functions Express

Kiến trúc mẫu end-to-end

(fileđến)(nightly02:00)S3EventBridgeruleEventBridgeSchedulerStepFunctions(Standard)GluejobDQcheckChoicepassCrawlerdbt/CTASfailSNS+Fail

Câu hỏi ôn tập

  1. Pipeline: Glue job A → nếu thành công chạy job B và C song song → cuối cùng crawler; cần retry từng bước và alert khi fail. Orchestrator gọn nhất?

    Xem đáp án

    Step Functions Standard: Task states với glue:startJobRun.sync (chờ job xong), Parallel state cho B+C, Retry/Catch per state, nhánh Catch → SNS. Toàn bộ serverless, không code điều phối. Glue Workflows cũng làm được (thuần Glue) nhưng thiếu alert/tích hợp ngoài Glue linh hoạt; MWAA thừa nặng cho một pipeline.

  2. Công ty di chuyển từ on-prem, đã có 200 Airflow DAGs. Giải pháp orchestration trên AWS ít công chuyển đổi nhất?

    Xem đáp án

    MWAA — Airflow managed, DAG Python gần như giữ nguyên (đưa lên S3, chỉnh connection/operator AWS nếu cần). Viết lại 200 DAG sang Step Functions ASL là chi phí lớn không cần thiết — sai với "minimal migration effort".

  3. Workflow cần chờ Glue job chạy 3 giờ rồi tiếp tục. Standard hay Express? Vì sao?

    Xem đáp án

    Standard — chạy tới 1 năm, tính tiền theo state transition nên chờ lâu không tốn thêm; dùng .sync để đợi Glue job. Express tối đa 5 phút — không thể chờ job 3 giờ. Express dành cho event processing ngắn, volume lớn.

  4. Cần xử lý 2 triệu object S3, mỗi object một lần transform, kiểm soát concurrency và retry per-item. Giải pháp?

    Xem đáp án

    Step Functions Distributed Map: liệt kê object từ S3 (hoặc manifest/CSV), fan-out mỗi item vào một child execution (Lambda/ECS...), đặt MaxConcurrency, tolerated failure, retry per item, kết quả tổng hợp về S3. Đây chính là use case Distributed Map sinh ra để thay việc tự viết queue + worker.

  5. Muốn khi Glue job FAILED thì tự động gửi Slack/SNS và tạo ticket — không sửa job. Cách làm?

    Xem đáp án

    EventBridge rule bắt event Glue Job State Change với state: FAILED → target SNS (hoặc Lambda gọi API ticket). Glue phát event lên EventBridge sẵn — không cần sửa job hay thêm polling. Pattern "react to service events" là đất của EventBridge.

  6. Sự khác nhau giữa EventBridge Scheduler và schedule trong Glue trigger / cron của MWAA?

    Xem đáp án

    EventBridge Scheduler là dịch vụ lịch độc lập, universal: cron/rate/one-time, timezone-aware, gọi được hầu hết API AWS (kể cả Step Functions, Glue, Lambda) với retry/DLQ — dùng khi cần "cái đồng hồ" chung. Glue trigger schedule chỉ khởi động job/workflow Glue; Airflow schedule gắn với DAG trong MWAA. Chọn theo hệ đang dùng; câu "replace cron server" → EventBridge Scheduler.

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

  • Tạo state machine: Lambda validate → Glue job (.sync) → Choice → SNS fail branch; chạy thử cả nhánh lỗi
  • Tạo EventBridge rule bắt Glue Job State Change FAILED → SNS email
  • Tạo EventBridge Scheduler cron 02:00 gọi state machine trên
  • Đọc Distributed Map

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


Tiếp theo: Quiz Tuần 4