As described in the Deploying Flows to Work Pools and Workers guide, there are two ways to deploy flows to work pools: using a prefect.yaml file or using the .deploy() method.
In both cases, you can override job variables on a work pool for a given deployment.
While exactly which job variables are available to be overridden depends on the type of work pool you're using at a given time, this guide will explore some common patterns for overriding job variables in both deployment methods.
Job variables are infrastructure related values that are configurable on a work pool, which may be relevant to how your flow run executes on your infrastructure. Job variables can be overridden on a per-deployment or per-flow run basis, allowing you to dynamically change infrastructure from the work pools defaults depending on your needs.
Let's use env - the only job variable that is configurable for all work pool types - as an example.
When you create or edit a work pool, you can specify a set of environment variables that will be set in the runtime environment of the flow run.
For example, you might want a certain deployment to have the following environment variables available:
Rather than hardcoding these values into your work pool in the UI and making them available to all deployments associated with that work pool, you can override these values on a per-deployment basis.
importosfromprefectimportflow,task@taskdefdo_something_important(not_so_secret_value:str)->None:print(f"Doing something important with {not_so_secret_value}!")@flow(log_prints=True)defsome_work():environment=os.environ.get("EXECUTION_ENVIRONMENT","local")print(f"Coming to you live from {environment}!")not_so_secret_value=os.environ.get("MY_NOT_SO_SECRET_CONFIG")ifnot_so_secret_valueisNone:raiseValueError("You forgot to set MY_NOT_SO_SECRET_CONFIG!")do_something_important(not_so_secret_value)
While not the focus of this guide, note that this deployment definition uses a default "global" pull step, because one is not explicitly defined on the deployment. For reference, here's what that would look like at the top of the prefect.yaml file:
To provide the EXECUTION_ENVIRONMENT and MY_NOT_SO_SECRET_CONFIG environment variables to this deployment, we can add a job_variables section to our deployment definition in the prefect.yaml file:
If you want to use environment variables that are already set in your local environment, you can template these in the prefect.yaml file using the {{ $ENV_VAR_NAME }} syntax:
As before, run prefect deploy -n demo-deployment to deploy the flow with these job variables, and you should see them in the UI under the Configuration tab.
If you're using the .deploy() method to deploy your flow, the process is similar, but instead of having your prefect.yaml file define the job variables, you can pass them as a dictionary to the job_variables argument of the .deploy() method.
We could add the following block to our demo_project/daily_flow.py file from the setup section:
if__name__=="__main__":flow.from_source(source="https://github.com/zzstoatzz/prefect-monorepo.git",entrypoint="src/demo_project/demo_flow.py:some_work").deploy(name="demo-deployment",work_pool_name="local",# can only .deploy() to a local work pool in prefect>=2.15.1job_variables={"env":{"EXECUTION_ENVIRONMENT":os.environ.get("EXECUTION_ENVIRONMENT","local"),"MY_NOT_SO_SECRET_CONFIG":os.environ.get("MY_NOT_SO_SECRET_CONFIG")}})
Note
The above example works assuming a couple things:
- the machine where this script is run would have these environment variables set.
When running flows, you can pass in job variables that override any values set on the work pool or deployment. Any interface that runs deployments can accept job variables.
Custom runs allow you to pass in a dictionary of variables into your flow run infrastructure. Using the same env example from above, we could do the following: