<@ULVA73B9P> I have a process work pool, and I wan...
# ask-marvin
j
@Marvin I have a process work pool, and I want to copy an environment variable into each worker. How do I do that/
m
thought for 692.3 seconds
n
the same value?
j
Yeah - it's just a prototype at the moment
n
you can set in the
env
on the one work pool, all workers will use that base job template
j
To clarify: The parent process that runs prefect worker start --pool <whatever> knows the value, but I don't know it in advance to put in the prefect.yaml file.
It should just be inherited?
n
you don't need to put it in the
prefect.yaml
, you can put it in here and mutate it via UI or SDK at any time
j
Can I reference a secret there?
n
if you trust your wherever the work pool record is stored (prefect cloud, your server). if you don't want to trust it then you can give it to the worker process (because it happens to be a process worker) but in general you could always put the name of a secret you load at runtime in
env
instead of the plaintext value
1
m
Great question. In Prefect 3.x, there are two related but different targets for environment variables. Which one do you want? 1) Ensure each worker process has an environment variable - Start the worker with the variable set in its shell environment:
Copy code
export MY_VAR="foo"
  prefect worker start -p my-process-pool
or
Copy code
MY_VAR="foo" prefect worker start -p my-process-pool
- If you use a service manager (e.g., systemd), set it there so every worker inherits it:
Copy code
[Service]
  Environment=MY_VAR=foo
  ExecStart=/usr/local/bin/prefect worker start -p my-process-pool
2) Ensure every flow run launched by the process workers gets the variable - Define it in the pool/deployment job variables so the worker passes it into each subprocess: - In prefect.yaml:
Copy code
deployments:
      - name: my-deployment
        entrypoint: flows/my_flow.py:flow
        work_pool:
          name: my-process-pool
          job_variables:
            env:
              MY_VAR: "{{ $MY_VAR }}"
This “copies” the worker’s environment variable
MY_VAR
into the flow run environment. The
{{ $ENV_VAR }}
syntax references environment variables at runtime. See prefect.yaml guide and customize job variables. - Or set it at the work pool level by editing the base job template to include a default env: 1) Get the default template:
Copy code
prefect work-pool get-default-base-job-template --type process --file base-job-template.json
2) Edit
base-job-template.json
to add:
Copy code
{
  "job_configuration": {
    "env": {
      "MY_VAR": "{{ $MY_VAR }}"
    }
  }
}
3) Update the pool:
Copy code
prefect work-pool update "my-process-pool" --base-job-template base-job-template.json
Docs: prefect work-pool CLI Notes - Using
{{ $MY_VAR }}
lets you “copy” the worker host’s environment variable into the flow run’s environment at runtime. - If you just need a constant value for all runs, you can set
env.MY_VAR: "foo"
instead of templating. Quick check: Do you want the variable set on the worker processes themselves, on every flow run they execute, or both? I can tailor the exact steps based on your setup (local shell, systemd, containers, etc.).