<@ULVA73B9P> Using a self hosted prefect server, h...
# ask-marvin
r
@Marvin Using a self hosted prefect server, how do I set up an automation such that when a prefect run enters 'Completed' it schedules a new run to be run automatically
m
thought for 99.6 seconds
Yes — you can do this on a self-hosted Prefect server using an automation that listens for the
prefect.flow-run.Completed
event and then runs a deployment. The basic pattern is: 1. A flow run finishes and enters
Completed
2. Prefect emits the
prefect.flow-run.Completed
event 3. An automation catches that event 4. The automation runs another deployment Important: automations trigger deployments, not ad hoc raw flow functions. So the “new run” should come from a deployment you want to launch. Example automation YAML:
Copy code
yaml
name: rerun-on-completion
enabled: true

trigger:
  type: event
  posture: Reactive
  expect:
    - prefect.flow-run.Completed
  match_related:
    prefect.resource.name: "my-flow"
    prefect.resource.role: "flow"
  threshold: 1
  within: 0

actions:
  - type: run-deployment
    source: selected
    deployment_id: "YOUR_DEPLOYMENT_ID"
Then create it with:
Copy code
bash
prefect automation create --from-file automation.yaml
I verified the CLI exists: -
prefect automation
-
prefect automation create --from-file ...
A few notes: If you want the flow to trigger itself again You can point
deployment_id
at the same deployment. That will create a loop, so be careful — it will keep launching new runs every time one completes. If you want flow A to trigger flow B Set the
deployment_id
to flow B’s deployment. If you only want this for one specific upstream deployment You can filter more narrowly using related deployment info instead of just the flow name. A common setup is:
Copy code
yaml
name: chain-flow-runs
enabled: true

trigger:
  type: event
  posture: Reactive
  expect:
    - prefect.flow-run.Completed
  match_related:
    prefect.resource.name: "upstream-flow"
    prefect.resource.role: "flow"
  threshold: 1
  within: 0

actions:
  - type: run-deployment
    source: selected
    deployment_id: "DOWNSTREAM_DEPLOYMENT_ID"
Useful docs: - Automations concepts - Event triggers - Creating automations - Chaining deployments with events If you want, I can also help you write the exact automation YAML for: - rerunning the same deployment, or - triggering a different deployment after completion.
r
@Marvin I want to make a simple Flow run compltede -> Trigger another flow run (different flow). In the UI it is seemingly not possible to select a specific deployment form the flow run I want to trigger the automation, only a flow run state, but my flow run has two deployments, and I only want one of the deployments to trigger the next flow, how can I do that?
m
thought for 111.3 seconds
Yes — the way to distinguish which deployment produced the completed flow run is to filter on the related deployment resource, not just on the flow-run state. In Prefect, a
prefect.flow-run.Completed
event is about the flow run, but that event also includes related resources like: - the flow - the deployment - possibly work pool, tags, etc. So if your flow has two deployments, you want your automation trigger to say: - event must be
prefect.flow-run.Completed
- and one of the related resources must be the specific deployment you care about Use
match_related
for that. Example trigger JSON:
Copy code
json
{
  "type": "event",
  "posture": "Reactive",
  "expect": ["prefect.flow-run.Completed"],
  "match": {
    "prefect.resource.id": "prefect.flow-run.*"
  },
  "match_related": {
    "prefect.resource.id": "prefect.deployment.YOUR_DEPLOYMENT_ID"
  },
  "threshold": 1,
  "within": 0
}
You can also filter by deployment name instead of ID:
Copy code
json
{
  "type": "event",
  "posture": "Reactive",
  "expect": ["prefect.flow-run.Completed"],
  "match": {
    "prefect.resource.id": "prefect.flow-run.*"
  },
  "match_related": {
    "prefect.resource.name": "my-specific-deployment"
  },
  "threshold": 1,
  "within": 0
}
Why the UI feels limited The UI’s simple form often only exposes the state/event selector. To target a specific deployment, you usually need to switch to the custom JSON/YAML-style trigger config and add
match_related
. So your automation should be: - Trigger:
prefect.flow-run.Completed
- Filter:
match_related
on the upstream deployment - Action:
run-deployment
for the downstream flow’s deployment Example full automation YAML:
Copy code
yaml
name: upstream-deployment-a-completed
enabled: true

trigger:
  type: event
  posture: Reactive
  expect:
    - prefect.flow-run.Completed
  match:
    prefect.resource.id: prefect.flow-run.*
  match_related:
    prefect.resource.id: prefect.deployment.UPSTREAM_DEPLOYMENT_ID
  threshold: 1
  within: 0

actions:
  - type: run-deployment
    source: selected
    deployment_id: DOWNSTREAM_DEPLOYMENT_ID
A couple practical tips: - Best filter:
prefect.resource.id
with the full deployment ID, since names can change - Good enough sometimes:
prefect.resource.name
if you want something easier to read - If you only use flow-level matching, both deployments of that flow can trigger it How to find the upstream deployment ID: - in the deployment page/details in the UI - or via
prefect deployment ls
If you want, I can help you write the exact JSON/YAML for your two deployments if you share: - upstream deployment name - downstream deployment name
r
@Marvin Can you give me a source for this
m
thought for 9.4 seconds
Yes — here are the relevant Prefect docs/source-backed references for using
match_related
to filter on a deployment related to a flow-run event: Docs - Event triggers — explains
match
vs
match_related
for event triggers - Creating automations — shows how automations are defined with event trigger fields - Chaining deployments with events — relevant pattern for triggering one deployment from another deployment’s events Source / implementation -
prefect.events.schemas.automations.EventTrigger
— this is the trigger schema that includes
match
and
match_related
-
prefect.events.schemas.events.ResourceSpecification
— this is the resource matching structure used for those fields If you want the most direct wording, the key concept is: -
match
filters the primary resource for the event -
match_related
filters related resources included with the event For a
prefect.flow-run.Completed
event: - the primary resource is the flow run - the deployment is a related resource So filtering a specific upstream deployment is done with
match_related
, e.g.:
Copy code
yaml
match_related:
  prefect.resource.id: prefect.deployment.<deployment-id>
If you'd like, I can also give you a copy-paste quote from the relevant doc section formatted for an internal note or PR comment.