Understanding ZenML stacks and infrastructure abstraction
A stack is the configuration of infrastructure and services that your ZenML pipelines run on. Stacks provide a powerful abstraction that allows you to develop locally and deploy to production without changing your pipeline code.
A ZenML stack is a collection of stack components that define where and how your pipelines execute. The same pipeline code can run on different stacks without modification, enabling seamless transition from development to production.
from zenml.client import Clientclient = Client()# Get active stackactive_stack = client.active_stackprint(f"Active: {active_stack.name}")# List all stacksfor stack in client.list_stacks(): print(f"Stack: {stack.name}")# Get specific stackproduction_stack = client.get_stack("production")
from zenml.client import Clientclient = Client()# Run pipeline on different stacksclient.set_active_stack("local")training_pipeline() # Runs locallyclient.set_active_stack("production")training_pipeline() # Runs on production infrastructure
Or use the CLI:
# Developmentzenml stack set localpython run_pipeline.py# Productionzenml stack set productionpython run_pipeline.py
The power of stacks is infrastructure abstraction:
from zenml import pipeline, stepimport pandas as pd@stepdef train_model(data: pd.DataFrame) -> Any: """This step works on any stack!""" model = train(data) return model@pipelinedef training_pipeline(): """This pipeline works on any stack!""" data = load_data() model = train_model(data) evaluate(model)# No code changes needed for different environments!# Just switch stacks:# Development (local)# zenml stack set local# training_pipeline()# Staging (Kubernetes + S3)# zenml stack set staging # training_pipeline()# Production (Kubernetes + S3 + Model Registry)# zenml stack set production# training_pipeline()
The same pipeline code runs on any stack. ZenML handles the infrastructure differences automatically.
from zenml.client import Clientclient = Client()# Each team member can have their own stacksstacks = client.list_stacks()for stack in stacks: print(f"Stack: {stack.name}") print(f"Owner: {stack.user.name}") print(f"Shared: {stack.is_shared}")
Each component type has multiple flavors (implementations):
# List available orchestrator flavorszenml orchestrator flavor list# Example output:# - local: Run locally# - kubernetes: Run on Kubernetes# - kubeflow: Run on Kubeflow Pipelines# - airflow: Run on Apache Airflow# - vertex: Run on Vertex AI# List artifact store flavors zenml artifact-store flavor list# Example output:# - local: Local filesystem# - s3: AWS S3# - gcs: Google Cloud Storage# - azure: Azure Blob Storage