Automate your machine learning workflow tasks using Elyra and Apache Airflow
Tags: jupyter, kubernetes, airflow, machinelearning
Published: 2021-03-18 14:19:44

In this blog post I’ll introduce a GUI-driven approach to creating Apache Airflow workflows that implement machine learning pipelines using Jupyter notebooks and Python scripts.

[Apache Airflow](https://airflow.apache.org/) is an open source workflow management platform that allows for _programmatic_ creation, scheduling, and monitoring of workflows. In Apache Airflow a workflow (or pipeline) is called a [Directed Acyclic Graph (DAG)](https://airflow.apache.org/docs/apache-airflow/stable/concepts.html#dags) that comprises of one or more related [tasks](https://airflow.apache.org/docs/apache-airflow/stable/concepts.html#tasks). A task represents a unit of work (such as the execution of a Python script) and is implemented by an operator. Airflow comes with built-in operators, such as the [`PythonOperator`](https://airflow.apache.org/docs/apache-airflow/stable/_api/airflow/operators/python/index.html#airflow.operators.python.PythonOperator), and can be extended using [provider packages](https://airflow.apache.org/docs/apache-airflow-providers/index.html). 

Let's say you want to use an Apache Airflow deployment on Kubernetes to periodically execute a set of notebooks or scripts that load and process data in preparation for machine learning model training. 

![notebook-script-pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhh0mfp6enza3m8gud9i.png)
 

Without going into details how, you could implement this workflow using the generic [`PythonVirtualEnvOperator`](https://airflow.apache.org/docs/apache-airflow/stable/howto/operator/python.html) to run the Python script and the special purpose  [`PapermillOperator`](https://airflow.apache.org/docs/apache-airflow-providers-papermill/stable/operators.html) to run the notebook. If the latter doesn't meet your needs, you'd have to implement its functionality yourself by developing code that performs custom pre-processing, uses the [papermill](https://papermill.readthedocs.io/en/latest/) Python package to run the notebook, and performs custom post-processing.

 An easier way to create a pipeline from scripts and notebooks is to use [Elyra](https://github.com/elyra-ai/elyra)'s Visual Pipeline Editor. This editor lets you assemble pipelines by dragging and dropping supported files onto the canvas and defining their dependencies.

![pipeline-in-pipeline-editor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a7774l8lm63qqwlqxu8y.png)

Once you've assembled the pipeline and are ready to run it, the editor takes care of generating the Apache Airflow DAG code on the fly, eliminating the need for any coding.

> If you are new to the Elyra open source project, take a look at the [overview documentation](https://elyra.readthedocs.io/en/latest/getting_started/overview.html).

The pipeline editor can also generate code that runs the pipeline in JupyterLab or on [Kubeflow Pipelines](https://dev.to/ibmdeveloper/automate-your-machine-learning-workflow-tasks-using-elyra-and-kubeflow-pipelines-5d6n) for greater flexibility.

![notebook-pipeline-local-and-remote](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ef2cus8klhprjv8c5d2k.png)
 
> Local execution is primarily intended for use during development or if no Kubeflow Pipelines or Apache Airflow deployment is available. 

This article outlines how you would go about creating a pipeline from a set of files and running it on Apache Airflow. We have published a [set of tutorials](https://elyra.readthedocs.io/en/latest/getting_started/tutorials.html#tutorials) that provide step-by-step instructions for each one of the supported runtime environments.

### Prerequisites

- JupyterLab 3.0 with the [Elyra extensions v2.1 (or later) installed](https://elyra.readthedocs.io/en/latest/getting_started/installation.html)
- Apache Airflow v1.10.8 or later but not including v2
  - Must be configured to use the Kubernetes Executor with git-sync enabled
  - [Must be enabled for Elyra](https://elyra.readthedocs.io/en/latest/recipes/configure-airflow-as-a-runtime.html) 

### Creating a pipeline

Pipelines are created in Elyra with the Visual Pipeline Editor by 
 - adding Python scripts or notebooks
 - configuring their execution properties
 - connecting the files to define dependencies 

![assemble-pipeline](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf0nf2259n3puu6hxfnw.gif)
 

Each pipeline node represents a task in the DAG and is executed in Apache Airflow with the help of a custom [`NotebookOp` operator](https://github.com/elyra-ai/airflow-notebook). The operator also performs pre- and post processing operations, that, for example, make it possible to share data between multiple tasks using shared cloud storage.

Node properties define the container image that the operator will run in, optional CPU, GPU, or RAM resource requests, file dependencies, environment variables, and output files. Output files are files that need to be preserved after the node was processed. For example, a notebook that trains a model might want to save model files for later consumption, such as deployment.

![configure node properties](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzn1zm2kt3iqqs5p6uf9.png)
 
A pipeline that comprises only of file nodes (nodes that execute a Python script or Jupyter notebook) can be run as-is locally in JupyterLab, or remotely in Apache Airflow or Kubeflow Pipelines.

> Future releases of Elyra might provide support for node types that are specific to a runtime platform. Pipelines that include such nodes can take advantage of platform specific features but won't be portable.

A pipeline definition does not include any target environment
information, such as the host name of the Apache Airflow webserver. This information is encapsulated in runtime [environment] configurations.

### Creating a runtime configuration

In Elyra [runtime configurations](https://elyra.readthedocs.io/en/latest/user_guide/runtime-conf.html) store metadata that describes the target environment where pipelines are executed. A runtime configuration for Apache Airflow includes 
 - connectivity information for the Airflow web server, 
 - details about the GitHub repository, where DAGs are stored,
 - connectivity information for the cloud storage service, which Elyra uses to store pipeline-run specific artifacts 

![apache-airflow-runtime-configuration](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gprle8e1oti5ybqh1bw0.png) 

> Elyra supports repositories on github.com and GitHub Enterprise. Note that some of the runtime configuration information is embedded in the generated DAGs to provide the `NotebookOp` operator access to the configured cloud storage. Therefore you should always use a  private repository to store DAGs that were produced by Elyra.

### Running pipelines on Apache Airflow

Once you created a pipeline and a runtime configuration for your Apache Airflow cluster, you are ready to run the pipeline.

![submit pipeline for execution](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2wgmu8xlnqej2v6zfaoz.png)
 
When you submit a pipeline for execution from the Visual Pipeline Editor, Elyra performs the following pre-processing steps:
- package the input artifacts (files and dependencies) for each task in a compressed archive
- upload the archives to the cloud storage bucket referenced in the runtime configuration
- generate a DAG, comprising of one task for each notebook or Python script
- upload DAG to the GitHub repository that Apache Airflow is monitoring

The uploaded DAG is pre-configured to run only once.

> Within limits you can customize the generated DAG by exporting the pipeline instead of running it. The main difference between running and exporting a pipeline for Apache Airflow is that the latter does not upload the generated DAG file to the GitHub repository.

### Monitoring a pipeline run on Apache Airflow

How soon a DAG is executed after it was uploaded to the repository  depends on the git-sync refresh time interval setting and the scheduler in your Apache Airflow configuration.

![listing of DAGs in the Airflow GUI](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k5rymi8p72pf7g8lr3wc.png)
 
![Monitor task status](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddc2m4yfmdrwm92haxvz.png)
 
Each notebook or Python script in the pipeline is executed as a task using Elyra's [`NotebookOp` operator](https://github.com/elyra-ai/airflow-notebook), in the order defined by it's dependencies.

Once a task has been processed, its outputs can be downloaded from the associated cloud storage bucket. Outputs include the completed notebooks, an HTML version of each notebook, a log file for each Python script, and files that were declared as output files.

![browse pipeline artifacts on cloud storage](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9htbxmlp8q4x7i2rjvi6.png)

### Getting started

If you have already have access to an v1.10 Apache Airflow cluster you can start running pipelines in minutes:
 - [Prepare your cluster for use by Elyra](https://elyra.readthedocs.io/en/latest/recipes/configure-airflow-as-a-runtime.html).
 - [Install Elyra from PyPI](https://elyra.readthedocs.io/en/latest/getting_started/installation.html#pip) or [pull the ready-to-use container image](https://elyra.readthedocs.io/en/latest/getting_started/installation.html#pulling-elyra-container-images).
 - [Step through the tutorial](https://elyra.readthedocs.io/en/latest/getting_started/tutorials.html#running-notebook-pipelines-on-apache-airflow) to learn how to create a pipeline, configure a pipeline, and run/export it.

If you are interested in running pipelines on Apache Airflow on the Red Hat OpenShift Container Platform, take a look at [Open Data Hub](https://opendatahub.io/). Open Data Hub is an open source project (just like Elyra) that should include everything you need to start running machine learning workloads in a Kubernetes environment.

### Using Watson Studio services in pipelines

We've introduced pipelines in Elyra to make it easy to run notebooks or scripts as batch jobs, and thus automate common repetitive tasks. 

The [recently launched beta of IBM Watson Studio Orchestration Flow](https://yairschiff.medium.com/automating-the-ai-lifecycle-with-ibm-watson-studio-orchestration-flow-4450f1d725d6) takes this a step further. The flow orchestrator integrates with various data and AI services in Watson Studio, enabling users to ingest data, or train, test and deploy machine learning models at scale. 

The orchestration flow editor, which is shown in the screen capture below, is based on the [Elyra canvas](https://github.com/elyra-ai/canvas) open source project.

![Watson Studio Orchestration flow](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e97vjo77dfn87kdukruh.png)
 
#### Questions and feedback

We'd love to hear from you! [You can reach us in the community chatroom, the discussion forum, or by joining our weekly community call.](https://elyra.readthedocs.io/en/latest/getting_started/getting-help.html)