Creating your first workflow [WIP]

In this guide we will create a simple workflow to return the sum of the square of all the elements in the list. following are the tasks we will be creating in the workflow

  1. Generate a list of numbers.
  2. Calculate the square of each number in the list.
  3. Sum the squared numbers.

Prerequisites

Before you proceed with the guide, make sure you have the following:

  • Truefoundry CLI: Set up and configure the TrueFoundry CLI tool on your local machine by following the Setup for CLI guide.
  • Workspace: To deploy your workflow, you'll need a workspace. If you don't have one, you can create it using this guide: Creating a Workspace or seek assistance from your cluster administrator.

Creating the Workflow

Create a workflow.py where we will write the code for our workflow and then create deploy.py file where we will store the truefoundry configuration for deploying workflow.

Your directory structure will then appear as follows:

.
├── workflow.py
└── deploy.py

workflow.py

from truefoundry.workflow import task, workflow, PythonTaskConfig, TaskPythonBuild
from truefoundry.deploy import Resources


cpu_task_config = PythonTaskConfig(
    # `TaskPythonBuild` helps specify the details of your Python Code.
    # These details will be used to templatize a DockerFile to build your Docker Image
    image=TaskPythonBuild(
        python_version="3.9",
        # PIp packages to install.`truefoundry[workflow]` is a mandatory dependency
        pip_packages=["truefoundry[workflow]"],
    ),
    resources=Resources(cpu_request=0.45),
    service_account="<service-account>",
)


# Task 1: Generate a list of numbers
@task(task_config=cpu_task_config)
def generate_numbers(n: int) -> list[int]:
    return list(range(1, n + 1))


# Task 2: Calculate the square of each number in the list
@task(task_config=cpu_task_config)
def square_numbers(numbers: list[int]) -> list[int]:
    return [x**2 for x in numbers]


# Task 3: Sum the squared numbers
@task(task_config=cpu_task_config)
def sum_squares(squared_numbers: list[int]) -> int:
    return sum(squared_numbers)


# Workflow: Orchestrate the tasks
@workflow
def simple_pipeline(n: int) -> int:
    numbers = generate_numbers(n=n)
    squared_numbers = square_numbers(numbers=numbers)
    result = sum_squares(squared_numbers=squared_numbers)
    return result


# Runs the pipeline locally
if __name__ == "__main__":
    result = simple_pipeline(n=5)
    print(f"The sum of squares from 1 to 5 is: {result}")

To learn more about the task and workflow, click on this link.

deploy.py

Now in deploy.py file write the below code to configure the deployment.

from truefoundry.deploy import Workflow, LocalSource


workflow = Workflow(
    name="testing-pipeline",  # your workflow application name
    workflow_file_path="workflow.py",
)
workflow.deploy(workspace_fqn="<workspace-fqn>")

Now run the below command in the terminal to deploy your workflow, replace <workfspace-fqn> with the workspace fqn which you can find on the ui.

python deploy.py

Run the above command from the same directory containing the deploy.py file.

View your deployed workflow

  • You can view the deployed workflow by going to the workflow tab on dashboard.