<@ULVA73B9P> I'm using a self hosted prefect serve...
# ask-marvin
r
@Marvin I'm using a self hosted prefect server on k8s. I'm currently using automations to send slack notifications when a flow runs stays in state RUNNING for a certain amount of time. But instead of having to rely only on slack notifications, I would like to send an event. I've a NATS bus set up in my cluster, is it possible to send an event to NATS through a prefect automation in self hosted prefect ?
m
thought for 100.3 seconds
Short answer: not directly to NATS, no. Prefect automations in self-hosted Prefect 3.x do not have a built-in
send-to-nats
action. But you can do this indirectly, and the usual pattern is: Prefect automationwebhook actionsmall in-cluster HTTP servicepublish to NATS The important part is that self-hosted automations do support webhook actions, so this is definitely feasible. Relevant docs: - Automations concepts - Custom notifications / webhooks - Custom events - Prefect events Python API What’s supported For self-hosted, automations can do things like: - send notifications - run deployments - change flow-run state - call webhooks There is no native NATS integration/action. Recommended approach 1. Create a small HTTP service in your cluster that accepts a POST 2. That service publishes the payload to your NATS subject 3. Point a Prefect
call-webhook
automation action at that service For example, your automation can send JSON like:
Copy code
json
{
  "flow_run_id": "{{ flow_run.id }}",
  "flow_name": "{{ flow.name }}",
  "state": "{{ flow_run.state.name }}",
  "deployment_id": "{{ deployment.id }}",
  "event": {{ event | tojson }}
}
And your bridge service can publish that to NATS. A minimal sketch of the bridge service:
Copy code
python
from fastapi import FastAPI, Request
import json
import nats

app = FastAPI()

@app.post("/publish")
async def publish(request: Request):
    body = await request.json()

    nc = await nats.connect("<nats://nats:4222>")
    try:
        await nc.publish(
            "prefect.flow.running_too_long",
            json.dumps(body).encode()
        )
    finally:
        await nc.drain()

    return {"ok": True}
Then in Prefect, configure a webhook block to call that endpoint. Example block creation:
Copy code
python
from prefect.blocks.webhook import Webhook

webhook = Webhook(
    url="<http://your-service.your-namespace.svc.cluster.local/publish>",
    method="POST"
)
webhook.save("nats-bridge-webhook", overwrite=True)
Then use that block in your automation’s
call-webhook
action. Alternative patterns If you want, you could also: - trigger a deployment from the automation, and have that flow publish to NATS - emit custom Prefect events from flows/tasks and build automation logic around those But for your use case—“flow has been
RUNNING
too long, publish to cluster bus”—the webhook bridge is probably the cleanest option. Bottom line Yes, this is possible in self-hosted Prefect, but not as a direct NATS target. Use a webhook automation action to hit an internal service that republishes to NATS. If you want, I can help you with either: - a concrete automation JSON/YAML example for the webhook action, or - a more production-ready FastAPI bridge service for Kubernetes.