Skip to Content

AWS Batch

Fully managed batch processing at any scale on EC2, Spot and Fargate, its building blocks, and when to use Batch instead of Lambda.

What it is

A batch job has a start and an end: render a video, process yesterday's sales files, run a genome analysis. That's the opposite of a web server, which runs continuously. Companies often have thousands of such jobs, and running them means managing a scheduler, a queue and a fleet of servers.

AWS Batch does that as a managed service: fully managed batch processing at any scale, up to hundreds of thousands of jobs.

  • Jobs are submitted or scheduled; Batch does the rest.
  • Batch launches EC2 instances or Spot Instances (or uses Fargate) dynamically, sized to the jobs waiting, and removes them when the queue is empty.
  • Jobs are defined as Docker images and run on Amazon ECS or EKS.
  • There's no extra charge for Batch: billing covers only the EC2, Spot or Fargate capacity the jobs use. With Spot, that's a big saving.

How it works

  • Job definition: a template for a job: the container image, vCPUs, memory, command, IAM role, retry policy.
  • Job queue: where submitted jobs wait. Queues have priorities.
  • Compute environment: the capacity that runs the jobs: managed (Batch picks and scales the instances, On-Demand or Spot, or uses Fargate) or unmanaged (the customer runs the instances).
  • Scheduler: decides which job runs next, and where.

Batch can also run array jobs (the same job thousands of times on different inputs) and multi-node parallel jobs that span several instances for tightly coupled HPC work.

A simple example

A file uploaded to S3 triggers an EventBridge rule that submits a job; AWS Batch runs it on EC2, Spot or Fargate and saves the result to S3 A file uploaded to S3 triggers an EventBridge rule that submits a job; AWS Batch runs it on EC2, Spot or Fargate and saves the result to S3

An event-driven image-processing pipeline.
  1. A user uploads an image to an S3 bucket.
  2. An EventBridge rule reacts to the upload and submits a Batch job (Batch jobs can also run on a schedule).
  3. AWS Batch puts the job in its queue and starts capacity in the compute environment: On-Demand, Spot, or Fargate.
  4. The job's container processes the image and writes the result to another S3 bucket. When the queue is empty, the capacity scales back to zero.

Batch vs Lambda

Both run code without any servers to manage, but they suit different jobs:

  • AWS Lambda
    • time limit per invocation;
    • a limited set of runtimes (or a container image built for Lambda);
    • limited temporary disk space (/tmp);
    • serverless: nothing to scale or patch, billed per millisecond.
  • AWS Batch
    • no time limit: jobs can run for hours or days;
    • any runtime, as long as it's packaged as a Docker image;
    • disk space from EBS or instance store, so jobs can use large local storage;
    • runs on EC2 (managed by AWS) or Fargate, and can use GPUs.

Rule of thumb: short, event-driven tasks → Lambda. Long, heavy or GPU jobs → Batch.

Updated since the slides

Lambda's maximum timeout is now 30 minutes (it was 15 minutes for years, and many practice questions still say 15). Memory and /tmp both go up to 10,240 MB. Batch can also now use ECS Managed Instances as capacity and queue SageMaker training jobs.

Real-world examples

  • A pharmaceutical company screens millions of molecules with array jobs on Spot Instances overnight.
  • A media company transcodes every uploaded video with a Batch job that runs for 40 minutes on a GPU instance, too long for Lambda.
  • A bank runs end-of-day risk calculations as a queue of jobs with priorities.

Exam tips

  • "Run batch jobs at scale", "hundreds of thousands of jobs", "managed job scheduling" → AWS Batch.
  • "Job runs longer than Lambda allows", "needs a custom runtime or GPU", "needs lots of disk" → Batch, not Lambda.
  • "Reduce the cost of batch processing" → Batch with Spot compute environments.
  • Batch jobs are Docker images, running on ECS/EKS with EC2, Spot or Fargate capacity.

Common traps

  • "Batch is a serverless function service." It runs containers on EC2 or Fargate capacity, which is billed.
  • "Use Lambda for a 3-hour job." Lambda's timeout is far shorter.

Hands-on lab: run a job on Fargate

Goal: create a Fargate compute environment, a job queue and a job definition, and run a container job.

Costs money

Fargate bills per second for the job's vCPU and memory. A job of 1 vCPU and 2 GB that runs for a minute costs well under a cent. Batch itself is free.

Start the wizard

Open AWS Batch in us-east-1 and choose Wizard (or Get started). Orchestration type Fargate.

Compute environment and queue

Name the compute environment saa-batch-ce, maximum vCPUs 4, the default VPC and its subnets. Name the job queue saa-batch-queue.

Job definition

Name saa-hello, image public.ecr.aws/amazonlinux/amazonlinux:latest, command echo Hello from AWS Batch, 1 vCPU, 2 GB memory. Turn Assign public IP on, so the job can pull the image from the internet. Let the wizard create the execution role.

Submit and watch

Submit a job named hello-1 to saa-batch-queue. In Jobs, watch it go Submitted → Runnable → Starting → Running → Succeeded. Open the job and follow the log stream link: CloudWatch Logs shows Hello from AWS Batch.

Expected result: a container job that ran on capacity Batch started for it and then released.

Clean up: disable and delete the job queue, then disable and delete the compute environment, and deregister the job definition. Delete the CloudWatch log group /aws/batch/job if you like (log storage is tiny).

AWS documentation

On this page

Open notes

Spotted a mistake?

These notes are open source. Suggest a fix, or star the repo if they helped you.

Suggest an edit

For AI agents: llms.txt · llms-full.txt