Quick start
This walk-through runs a DAG with a single Airflow task,
RunWorkRequirement,
on a YellowDog worker pool that is already running. The Airflow task submits
a work requirement containing one YellowDog task (a docker task that runs
the hello-world container), then waits until the work requirement
completes. To provision the pool from the DAG as well, see Provision, run, shut down.
Before you start
You need:
A working Airflow 3.1+ installation on Python 3.12–3.14 (for a local trial,
airflow standaloneis enough).A YellowDog Application key ID and secret. Create an Application in the YellowDog Portal and give it access to the namespace you will use.
A YellowDog worker pool that is up (status
IDLEorRUNNING), whose workers support thedockertask type and carry a worker tag you know. You can create one in the YellowDog Portal, or provision one from Airflow withProvisionWorkerPool(see Operators, and the full example DAG described at the end of this page).
If these terms are new, read Concepts first.
1. Install the provider
In the Python environment that runs Airflow:
pip install --upgrade yellowdog-airflow-provider
Restart the Airflow components so they pick up the new connection type.
2. Create the connection
Operators and sensors use the connection ID yellowdog_default unless told
otherwise, so create a connection with that ID.
In the Airflow UI: go to Admin → Connections, add a connection, and fill in:
Field |
Value |
|---|---|
Connection Id |
|
Connection Type |
|
Platform API URL |
Leave empty to use |
Application Key ID |
Your Application’s key ID |
Application Key Secret |
Your Application’s key secret |
Or with the CLI:
$ airflow connections add yellowdog_default \
--conn-type yellowdog \
--conn-login '<KEY_ID>' \
--conn-password '<KEY_SECRET>'
Other ways to create connections (environment variables, secrets backends) are covered in Connections.
3. Test the connection
Airflow disables connection testing by default. To enable it, set
test_connection = Enabled in the [core] section of airflow.cfg, or
set the environment variable AIRFLOW__CORE__TEST_CONNECTION=Enabled, and
restart Airflow. Then either press Test on the connection’s page in the
UI, or run:
$ airflow connections test yellowdog_default
On success the UI shows Successfully connected to
https://api.yellowdog.ai and the CLI prints Connection success!. On
failure both show the error from YellowDog (for example, a 401 for a wrong
key or secret); see Troubleshooting.
4. Add the DAG
Save the following as yd-quickstart.py in your Airflow DAGs folder. (It is
also in the provider’s GitHub repository, as
yellowdog_provider/example_dags/yd-quickstart.py.)
"""Quickstart: run one task on an existing YellowDog worker pool. Needs the Airflow
Connection `yellowdog_default` and a running pool whose workers carry `worker_tag`."""
from airflow.sdk import DAG, Context
from airflow.sdk.definitions.param import ParamsDict
from jinja2 import Environment
from yellowdog_client.model import RunSpecification, Task, TaskGroup, WorkRequirement
from yellowdog_provider.operators.yellowdog_operators import RunWorkRequirement
from yellowdog_provider.utils.run_stamp import MAX_NAME_LENGTH, sanitise_name
# Airflow calls these at run time (parameter names must be exactly these).
def make_work_requirement(context: Context, jinja_env: Environment) -> WorkRequirement:
assert "params" in context and "run_id" in context
# Make a valid YellowDog name: a-z, 0-9, "_" and "-", up to 60 characters,
# starting with a letter and ending with a letter or digit
name = sanitise_name(f"quickstart-{context['run_id']}", MAX_NAME_LENGTH)
return WorkRequirement(namespace=context["params"]["namespace"], name=name)
def make_groups_and_tasks(
context: Context, jinja_env: Environment
) -> list[tuple[TaskGroup, list[Task]]]:
assert "params" in context
tag = context["params"]["worker_tag"]
spec = RunSpecification(taskTypes=["docker"], workerTags=[tag])
task = Task(name="task-1", taskType="docker", arguments=["hello-world"])
return [(TaskGroup(name="group-1", runSpecification=spec), [task])]
dag: DAG = DAG(
dag_id="yellowdog-quickstart",
# worker_tag must match the tag of an existing, running worker pool
params=ParamsDict({"namespace": "airflow-quickstart", "worker_tag": "my-tag"}),
)
with dag:
# Submit a work requirement and wait until it completes. If it fails or is
# cancelled instead, the task (and so the DAG run) fails at once, and the
# work requirement is cancelled if the task times out or is killed.
RunWorkRequirement(
task_id="run",
work_requirement=make_work_requirement,
task_groups_and_tasks=make_groups_and_tasks,
poke_interval=30,
timeout=30 * 60,
)
What it does:
make_work_requirementandmake_groups_and_tasksare callables. Airflow calls them when theruntask runs, passing the run’s context, so they can use the run’s params andrun_id(see Templating and callables). The work requirement’s name includes therun_id, so every run creates a uniquely named work requirement.The task group’s run specification asks for the
dockertask type and your worker tag, so only your pool’s workers will claim it.run(RunWorkRequirement) creates the work requirement, its task group and task, then checks the work requirement’s status every 30 seconds, for up to 30 minutes, until it isCOMPLETED. If the work requirement reachesFAILEDorCANCELLEDinstead, the task fails at once withYellowDogStateError(see failure states). If the wait times out or the task is killed, the work requirement is cancelled.
5. Trigger a run
New DAGs are paused by default. Unpause the DAG and trigger it, supplying your pool’s worker tag (and, if you need a different one, the namespace):
$ airflow dags unpause yellowdog-quickstart
$ airflow dags trigger yellowdog-quickstart \
--conf '{"worker_tag": "<YOUR_WORKER_TAG>", "namespace": "airflow-quickstart"}'
Or, in the UI, unpause yellowdog-quickstart, press Trigger, and edit
the params in the form.
What to expect
runlogs the work requirement’s namespace and name and, a few seconds after it starts, its ID (Work requirement '<namespace>/<name>' (ID '...'): submitted). The new work requirement is visible in the YellowDog Portal, and once the task has finished the task’s YellowDog work requirement link opens it (see Connections).runlogs the status on each check (Current status is 'RUNNING' ... waiting for 30 seconds) until the task has run. On a pool that is already up,hello-worldfinishes quickly, so the task usually succeeds within a few checks.When the work requirement completes,
runlogsReached status 'COMPLETED'and succeeds, and so does the DAG run.If the work fails or is cancelled,
runfails withYellowDogStateError(for exampleWork requirement 'airflow-quickstart/quickstart-...' (ID '...') reached status 'FAILED', which is a failure state (target states: 'COMPLETED')), and the DAG run fails. Look at the task’s details in the YellowDog Portal to see why. To wait for the work to finish whatever the outcome, listFAILEDandCANCELLEDintarget_statestoo.
If run keeps reporting RUNNING, no worker has claimed the task: check
that your pool is up, supports the docker task type, and carries exactly
the worker tag you passed. See Troubleshooting for other problems.
Next steps
Provision, run, shut down shows the usual three-task DAG (provision a pool, run work on it, shut it down) and explains deferral, retries and clean-up.
Templating and callables explains callables and XCom-to-ID patterns.
The full example DAG in the provider’s GitHub repository, yd-provider-test.py, exercises the workflow operators: it provisions its own worker pool (a setup task), runs two work requirements on it, and shuts it down (a teardown task). Its behaviour is driven by DAG params (connection ID, namespace, compute requirement template, image family, task type and arguments, timeout and poke interval, whether to defer, maximum worker pool nodes, userdata), whose defaults must be changed to match items in your YellowDog account.