<@ULVA73B9P> I want to create an automation on a f...
# ask-marvin
j
@Marvin I want to create an automation on a flow run, but only when the flow is started by a particular service account. Is that possible?
m
thought for 434.9 seconds
Short answer: not directly. In Prefect 3.x, Automation triggers for
prefect.flow-run.*
events can’t currently filter by the user/service account that created the run. The emitted events don’t expose the service account as a matchable field. Practical ways to achieve what you want: - Preferred: ensure those runs are created via a dedicated deployment (or automation) and filter by the creator. Flow-run events include the creator (deployment/automation) as a related resource, so you can match on it:
Copy code
{
  "type": "event",
  "expect": [
    "prefect.flow-run.*"
  ],
  "match_related": {
    "prefect.resource.role": "creator",
    "prefect.resource.id": "prefect.deployment.<your-deployment-id>"
  }
}
- Tag-based: add a unique tag for runs started by that service account (e.g.,
source:svc-abc
) and filter by that tag:
Copy code
{
  "type": "event",
  "expect": [
    "prefect.flow-run.*"
  ],
  "match_related": {
    "prefect.resource.id": "prefect.tag.source:svc-abc"
  }
}
If those runs are started from a specific deployment, you can put the tag on the deployment so every run inherits it; otherwise add the tag when you create the run programmatically. Notes and links: - Event triggers and related-resource matching: Event triggers - Automations overview: Automations concepts - Events model: Events - Creating automations: How to: Create automations Questions to tailor the setup: - How are these flow runs started by the service account today? (API/SDK, CLI
prefect deployment run
, or something else?) - Can you use a dedicated deployment for this service account’s runs, or add a tag at run creation time? If you share that, I can give you the exact filter and (if needed) code/CLI steps to add the tag or target a specific deployment.
j
@Marvin Started from an API call to
run_deployment
m
thought for 53.5 seconds
Got it — since you’re creating runs via
run_deployment
, here’s what’s possible in Prefect 3.x: - Not possible: filtering an Automation by the specific service account (user) that called
run_deployment
. Flow-run events don’t expose the caller’s service account as a matchable field; the “creator” is recorded as the deployment, not the API caller. Good options you can use: 1) Match the deployment as the creator (no code changes) - Works if this service account uses a dedicated deployment (or you’re okay with all runs from that deployment triggering the automation). - Automation trigger snippet:
Copy code
{
    "type": "event",
    "expect": ["prefect.flow-run.*"],
    "match_related": {
      "prefect.resource.role": "creator",
      "prefect.resource.id": "prefect.deployment.<your-deployment-id>"
      // or, if you prefer by name:
      // "prefect.resource.name": "your-flow-name/your-deployment-name"
    }
  }
2) Tag the flow run in the
run_deployment
call (service-account–specific) - Add a distinctive tag only when invoked by that service account, then filter by the tag in the Automation. - Example:
Copy code
from prefect.deployments import run_deployment

  run = run_deployment(
      name="your-flow/your-deployment",
      parameters={"...": "..."},
      tags=["source:svc-your-service-account"]
  )
- Automation trigger snippet:
Copy code
{
  "type": "event",
  "expect": [
    "prefect.flow-run.*"
  ],
  "match_related": {
    "prefect.resource.id": "prefect.tag.source:svc-your-service-account"
  }
}
If you can’t dedicate a deployment and can’t add tags at call time, there isn’t a reliable way today to target a specific service account for
prefect.flow-run.*
events. If that’s critical, I’d suggest opening a feature request. Docs for reference: - Automations overview: Automations - Event triggers and related-resource matching: Event triggers - Events model: Events Want me to help draft the exact automation JSON for your deployment name/ID or tag?