How do you build a reproducible ML pipeline with DVC and AWS S3?

Build a reproducible spam-detection pipeline with DVC stages, versioned artifacts, and AWS S3 remote storage.

A small spam classifier has three steps: normalize training text, learn a word rule, then evaluate it. You can run three Python scripts by hand. But when one input changes, which scripts need to run again?

A pipeline makes that relationship explicit. DVC records each stage's command, dependencies and outputs, then uses those declarations to decide what is out of date.

Make the files tell the story

Use a fresh directory with Python and DVC installed. The following tiny classifier is for inspecting pipeline behavior, not for filtering real mail.

Save these training examples as raw.json:

Save separate evaluation examples as test.json:

The preparation script, prepare.py, turns each text into lowercase tokens while preserving its label:

The fitting script, fit.py, remembers words seen in spam but not in non-spam training examples. That learned set is our model:

Finally, evaluate.py predicts spam when any learned spam word occurs and writes the fraction of correct test predictions:

The rule misclassifies “win the project.” A word seen in spam can appear in an ordinary message. This visible error will stay in the evaluation; pipeline automation should not hide it.

Declare what each command reads and writes

Run these commands from the directory containing the five files:

-d declares a dependency, -o a cached output, and -M a metric file kept outside the DVC data cache. Stage definitions go into dvc.yaml; dvc.lock records the dependency/output state of the reproduced pipeline.

StageDeclared inputsOutput
prepareprepare.py, raw.jsontrain.json
fitfit.py, train.jsonmodel.json
evaluateevaluate.py, model.json, test.jsonmetrics.json

The first reproduction creates the outputs and reports accuracy 2/3. A second unchanged dvc repro skips the work. DVC compares declared inputs and output state; it does not discover every file or environment setting a script might secretly use.

Add only a comment to prepare.py, then run dvc repro again. The script is a declared dependency, so preparation is out of date. Must fitting and evaluation also run?

Keep the scripts, small input fixtures, dvc.yaml, dvc.lock, metric file and DVC configuration under Git. Large real inputs can themselves be tracked by DVC. The fitted model and preprocessing output are cached data artifacts, rather than files to add directly to Git.

Where S3 fits

The pipeline runs on your machine or runner. Amazon S3 stores objects; it does not run these Python stages.

A local remote lets you practice the storage step without a cloud account:

For an existing S3 bucket, the remote URL would instead look like s3://YOUR_BUCKET/dvc-store. DVC's S3 storage guide explains the S3 backend and authentication. Use appropriate role or temporary credentials supplied by your environment, with access to the required bucket/prefix; credentials do not belong in committed pipeline files.

Inspect one dependency: DVC's repro reference explains stage selection and caching. Try this in the practice directory:

  1. Back up the configuration with cp dvc.yaml dvc.yaml.backup.
  2. In dvc.yaml, find stages → prepare → deps and remove only its prepare.py entry. Keep the script file and the python prepare.py command.
  3. Run dvc repro once after changing the declaration. Then add another comment to prepare.py and run dvc repro again.

The second run can skip preparation: the edited script is no longer a declared dependency. DVC still runs the command when needed, but it does not infer that the command's script should be tracked. Restore the configuration with mv dvc.yaml.backup dvc.yaml and run dvc repro to restore its recorded state.

  • A DVC stage declares a command, dependencies and outputs; include the script itself as a dependency.
  • dvc repro runs out-of-date stages or reuses available cached results based on declared state.
  • Unchanged downstream input can let later stages skip even after an upstream script reruns.
  • dvc.yaml defines the pipeline; dvc.lock records reproduced dependency/output state.
  • S3 stores data and model objects; it does not execute the training pipeline. Keep credentials outside committed configuration.