AWS Batch
Run batch jobs as Docker images, at any scale. You hand Batch a container + a job queue; it works out how many instances of what type to run, launches them (EC2, Spot, or Fargate), runs the jobs, and tears the capacity back down. The "no time limit, any runtime, real disk" answer when Lambda doesn't fit.
Chạy batch job dưới dạng Docker image, ở mọi quy mô. Bạn đưa cho Batch một container + một job queue; nó tự tính cần bao nhiêu instance loại gì, launch lên (EC2, Spot hoặc Fargate), chạy job xong thì thu capacity về. Đây là câu trả lời "không giới hạn thời gian, runtime nào cũng được, có disk thật" khi Lambda không đủ.
Reach for itNên dùng
- Thousands of concurrent, independent jobs — batch image / video processing, rendering, ETL fan-outHàng nghìn job độc lập chạy song song — xử lý ảnh / video hàng loạt, rendering, ETL fan-out
- Job runs longer than Lambda's 15-minute ceilingJob chạy lâu hơn trần 15 phút của Lambda
- Needs a real runtime / binary / GPU / large disk that doesn't fit Lambda's package modelCần runtime / binary / GPU / disk lớn mà package model của Lambda không kham nổi
- Cost-sensitive bulk compute → Spot inside a managed compute environmentCompute số lượng lớn, nhạy chi phí → Spot trong managed compute environment
- HPC / tightly coupled MPI workloads → Multi Node modeWorkload HPC / tightly coupled MPI → Multi Node mode
Not the right toolKhông hợp
- Short, event-driven, sub-15-min functions → Lambda (cheaper, no container to build)Function ngắn, event-driven, dưới 15 phút → Lambda (rẻ hơn, không phải build container)
- Long-running services that must stay up and take traffic → ECS / EKS servicesService chạy liên tục, phải luôn sống và nhận traffic → ECS / EKS service
- The workflow logic itself (branching, retries, human approval) → Step Functions driving Batch, not Batch aloneBản thân logic workflow (rẽ nhánh, retry, chờ duyệt) → Step Functions điều khiển Batch, không phải Batch tự làm
- Hadoop / Spark cluster semantics → EMRCần semantics cluster Hadoop / Spark → EMR
1 · Overview1 · Tổng quan
- Run batch jobs as Docker images. The unit of work is a container, pulled from ECR (or any registry).Chạy batch job dưới dạng Docker image. Đơn vị công việc là một container, pull từ ECR (hoặc registry bất kỳ).
- Two options. 1. Managed Compute Environment — dynamic provisioning of instances (EC2 & Spot Instances) inside your VPC, or AWS Fargate (full serverless offering). 2. Unmanaged Compute Environment — you bring ECS / EKS capacity.Hai lựa chọn. 1. Managed Compute Environment — tự động provision instance (EC2 & Spot Instance) trong VPC của bạn, hoặc AWS Fargate (serverless hoàn toàn). 2. Unmanaged Compute Environment — bạn tự mang capacity ECS / EKS.
- Optimal quantity and type of instance is chosen for you, based on job volume and the resource requirements each job declares.Số lượng và loại instance tối ưu được chọn giúp bạn, dựa trên khối lượng job và resource mà mỗi job khai báo.
- You just pay for the underlying resources used. Batch is a free orchestration layer on top of EC2 / Fargate.Chỉ trả tiền cho resource bên dưới. Batch là lớp điều phối miễn phí đặt trên EC2 / Fargate.
- Example: batch process of images, running thousands of concurrent jobs.Ví dụ: xử lý ảnh hàng loạt, chạy hàng nghìn job đồng thời.
- Schedule Batch jobs using Amazon EventBridge (cron or event rules).Lên lịch Batch job bằng Amazon EventBridge (cron hoặc event rule).
- Orchestrate Batch jobs using AWS Step Functions (the
SubmitJobintegration waits for the job to finish).Điều phối Batch job bằng AWS Step Functions (integrationSubmitJobchờ job chạy xong).
The four building blocksBốn thành phần
2 · Solution architecture2 · Kiến trúc giải pháp
Upload lands in S3 → an S3 event notification hits Lambda (which calls SubmitJob) or EventBridge (which triggers Batch directly) → the container runs on ECS-backed EC2, Spot or Fargate, pulls its image from ECR, reads the file back from S3, and writes metadata to DynamoDB + the processed file to S3.
File upload vào S3 → S3 event notification gọi Lambda (Lambda gọi SubmitJob) hoặc EventBridge (trigger Batch trực tiếp) → container chạy trên EC2 / Spot / Fargate do ECS quản, pull image từ ECR, đọc lại file từ S3, ghi metadata vào DynamoDB + file đã xử lý vào S3.
SubmitJob when you need logic before submitting (parse the key, decide the job definition, batch several files into one job). S3 → EventBridge → Batch target when the mapping is 1:1 and you want no code at all. EventBridge is also the cron path: "run this job every night at 02:00".
S3 → Lambda → SubmitJob khi cần logic trước khi submit (parse key, chọn job definition, gộp nhiều file vào một job). S3 → EventBridge → target Batch khi mapping 1:1 và bạn không muốn viết code. EventBridge cũng là đường cron: "chạy job này 02:00 mỗi đêm".
3 · Batch vs Lambda3 · Batch vs Lambda
The single most exam-relevant comparison on this page. If the question mentions a job that can't finish in 15 minutes, needs an arbitrary runtime or binary, or needs real disk — the answer is Batch. Đây là so sánh sát đề nhất trong trang này. Nếu câu hỏi nhắc tới job không xong trong 15 phút, cần runtime hoặc binary bất kỳ, hoặc cần disk thật — đáp án là Batch.
| Lambda | AWS Batch | |
|---|---|---|
| DurationThời lượng | Time limit (15 min max)Có giới hạn thời gian (tối đa 15 phút) | No time limitKhông giới hạn thời gian |
| RuntimeRuntime | Limited runtimes — built-in runtimes, or Docker images built for LambdaRuntime hạn chế — runtime có sẵn, hoặc Docker image build riêng cho Lambda | Any runtime, as long as it's packaged as a Docker imageRuntime nào cũng được, miễn đóng gói thành Docker image |
| DiskDisk | Limited temporary disk space (/tmp)Disk tạm hạn chế (/tmp) |
Relies on EBS / instance store for disk spaceDùng EBS / instance store làm disk |
| Compute modelMô hình compute | Serverless — nothing to seeServerless — không thấy hạ tầng | Relies on EC2 (can be managed by AWS) or AWS FargateDựa trên EC2 (AWS có thể quản giúp) hoặc AWS Fargate |
4 · Compute environments4 · Compute environment
Managed Compute EnvironmentManaged Compute Environment
- AWS Batch manages the capacity and instance types within the environment — you set min / desired / max vCPU and let it size itself.AWS Batch quản capacity và instance type trong environment — bạn set min / desired / max vCPU rồi để nó tự cân.
- You can choose EC2 On-Demand or Spot Instances.Bạn chọn EC2 On-Demand hoặc Spot Instance.
- You can choose Fargate On-Demand or Fargate Spot.Bạn chọn Fargate On-Demand hoặc Fargate Spot.
- You can set a maximum price for Spot Instances (as a % of the On-Demand price).Bạn có thể đặt giá tối đa cho Spot Instance (theo % giá On-Demand).
- Launched within your own VPC.Launch trong VPC của chính bạn.
RUNNABLE forever. This is the "my Batch jobs never start" question.
Nếu launch vào private subnet của bạn, phải đảm bảo nó truy cập được ECS service — qua NAT gateway / NAT instance, hoặc VPC endpoint cho ECS. Thiếu là instance lên nhưng không đăng ký được với ECS, job kẹt ở RUNNABLE mãi mãi. Đây chính là câu hỏi "job Batch của tôi không bao giờ chạy".
Unmanaged Compute Environment (EC2, ECS, EKS)Unmanaged Compute Environment (EC2, ECS, EKS)
- You control and manage your compute resources, provisioning and scaling. Batch only schedules jobs onto capacity you already run.Bạn tự kiểm soát và quản compute resource, provisioning và scaling. Batch chỉ schedule job lên capacity bạn đã chạy sẵn.
- Pick this when you need custom AMIs, special networking, licensing constraints, or an existing ECS / EKS cluster you must reuse.Chọn khi cần AMI tuỳ biến, networking đặc thù, ràng buộc license, hoặc phải tái dùng cluster ECS / EKS có sẵn.
How a managed environment scalesManaged environment scale ra sao
You only ever talk to the job queue. Batch mixes instance types (and sizes) to fit the queued jobs' vCPU/memory requirements, staying within the min/max vCPU you set.
Bạn chỉ làm việc với job queue. Batch tự trộn nhiều loại và size instance cho khớp yêu cầu vCPU/memory của job đang chờ, trong khoảng min/max vCPU bạn đặt.
5 · Multi Node mode5 · Multi Node mode
Multi node = a single job that occupies several EC2 instances at once. One main node coordinates, the child nodes do the work.
Multi node = một job duy nhất chiếm nhiều EC2 instance cùng lúc. Một main node điều phối, các child node làm việc.
- Multi Node: large scale, good for HPC (high performance computing).Multi Node: quy mô lớn, hợp cho HPC (high performance computing).
- Leverages multiple EC2 / ECS instances at the same time; good for tightly coupled workloads (MPI, distributed training) where nodes talk to each other constantly.Dùng nhiều EC2 / ECS instance cùng lúc; hợp workload tightly coupled (MPI, distributed training) — các node liên tục nói chuyện với nhau.
- Represents a single job, and you specify how many nodes to create for it: 1 main node and many child nodes.Là một job duy nhất, bạn khai tạo bao nhiêu node cho nó: 1 main node và nhiều child node.
- Does not work with Spot Instances. One interruption would kill the whole tightly coupled job — remember this pairing.Không dùng được với Spot Instance. Một lần bị thu hồi là chết cả job tightly coupled — nhớ cặp này.
- Works better if your EC2 launch mode uses a
clusterplacement group — same rack, same AZ, lowest network latency between nodes.Chạy tốt hơn nếu EC2 launch mode dùng placement groupcluster— cùng rack, cùng AZ, latency mạng giữa các node thấp nhất.
Exam reflexesPhản xạ khi thi
- "Job runs 40 minutes" / "needs a custom binary" / "needs 200 GB scratch" → Batch, not Lambda."Job chạy 40 phút" / "cần binary tuỳ biến" / "cần 200 GB scratch" → Batch, không phải Lambda.
- "Cheapest way to run thousands of independent jobs overnight" → managed CE on Spot + EventBridge schedule."Cách rẻ nhất để chạy hàng nghìn job độc lập qua đêm" → managed CE dùng Spot + lịch EventBridge.
- "Tightly coupled HPC across nodes" → Multi Node + cluster placement group, and no Spot."HPC tightly coupled trên nhiều node" → Multi Node + cluster placement group, và không Spot.
- "Jobs stuck in
RUNNABLEin a private subnet" → missing NAT gateway or ECS VPC endpoint."Job kẹt ởRUNNABLEtrong private subnet" → thiếu NAT gateway hoặc VPC endpoint cho ECS. - "Chain job A → job B → job C with branching and retries" → Step Functions orchestrating Batch, not job dependencies alone."Nối job A → B → C có rẽ nhánh và retry" → Step Functions điều phối Batch, đừng chỉ dựa vào job dependency.