is there a good tutorial somewhere on building aut...
# prefect-cloud
d
is there a good tutorial somewhere on building automation using webhooks? this guide on event-driven flows was a good start, but i specifically need more guidance on how to setup custom automation triggers with event-based filtering. the syntax isn't intuitive (to me at least), and the docs are a little thin. im staring at this json blob trying to figure out how i build in filters based on the event payload of the webhook .. its been a lot of trial-and-error to make any progress πŸ™ƒ
Copy code
{
  "match": {
    "prefect.resource.id": "prefect.flow-run.*"
  },
  "match_related": {
    "prefect.resource.id": "prefect.deployment.70cb25fe-e33d-4f96-b1bc-74aa4e50b761",
    "prefect.resource.role": "deployment"
  },
  "for_each": [
    "prefect.resource.id"
  ],
  "after": [],
  "expect": [
    "prefect.flow-run.Failed"
  ],
  "posture": "Reactive",
  "threshold": 2,
  "within": 10
}
βœ… 1
j
Hi David. You might find this example helpful.
d
this is helpful, thanks @Jeff Hale .. i figured out that i need to take elements from the webhook payload and write them in as custom elements in the
resource
part of the webhook event. i can then use those elements as filters in the
match
part of the automation trigger setup. probably other ways to accomplish the same end result, but ive got this working for me as desired. πŸ’ͺ
🦜 1
πŸ™Œ 1
j
@David Anderson Do you mind providing an example of how you used the custom elements in the
resource
part of the webhook event as a filter in the
match
part of the automation trigger? I am not sure how to do that. I've tried labeling the item as
resource.custom-element
but that is not working.
j
Hey @John Kang - I just made

this videoβ–Ύ

that might be helpful. You can click on an event that was created from the webhook and then click on Automate in the three dot menu in the top right. If you see something like
Copy code
{
  "match": {
    "prefect.resource.id": "gh-repo-discdiver.41"
  },
You can use an asterisk to match on all the first part of the ids. e.g.
Copy code
{
  "match": {
    "prefect.resource.id": "gh-repo-discdiver.*"
  },
Does that get you what you need?
j
@Jeff Hale Thanks so much for the reply! Not really.. I'm trying to use the body of the webhook call to respond differently. This one webhook is used to indicate when one of two different databases are updated. I want to use the body of the message to trigger different automations. I setup an automation using the contents of the element
prefect.resource.database
which I defined in the webhook:
Copy code
{
  "match": {
    "prefect.resource.id": "webhook.resource.id"
  },
  "match_related": {
    "prefect.resource.name": "ovt_completion",
    "prefect.resource.database": "OVT PRODUCTION"
  },
  "after": [],
  "expect": [
    "webhook.called"
  ],
  "for_each": [],
  "posture": "Reactive",
  "threshold": 1,
  "within": 0
}
webhook results:
Copy code
{
  "id": "868de4b3-f032-46fd-9c56-e5e051f6c984",
  "account": "f2264e8b-bee1-4943-89e5-2480677670cf",
  "event": "webhook.called",
  "occurred": "2023-12-15T17:04:15.057Z",
  "payload": {
    "method": "POST",
    "headers": {
      "host": "api.prefect.cloud",
      "user-agent": "python-requests/2.31.0",
      "accept-encoding": "gzip, deflate",
      "accept": "*/*",
      "content-length": "14",
      "x-request-id": "f5ebdd45-2095-94d2-a790-47fce49933db"
    },
    "body": "OVT PRODUCTION",
    "raw_body": "OVT PRODUCTION"
  },
  "received": "2023-12-15T17:04:15.059Z",
  "related": [
    {
      "prefect.resource.id": "prefect-cloud.webhook.1adf3e46-ff33-4405-b715-de0a8c1f5e24",
      "prefect.resource.name": "ovt_completion",
      "prefect.resource.role": "webhook"
    }
  ],
  "resource": {
    "prefect.resource.id": "webhook.resource.id",
    "producing": "OVT CADS",
    "prefect.resource.database": "OVT PRODUCTION"
  },
  "workspace": "140a070a-5c2c-4de9-9bf6-33d62e675a65"
}
The `match-related`does not seem to work when I add in the `prefect.resource.database`clause
Do I need to update the
prefect.resource.id
and use that instead of generating a new element?
I'm watching your video, should I put that into the expect section of the automation?
oh wow that was it
πŸ™Œ 1
It would be nice to use the match_related though to be more specific
j
Glad it’s working! As I understand it,
match_related
is for matching related Prefect events. I would expect that
expect
has the ability to be used to differentiate the two cases. Is something missing there? cc: @Will Raphaelson
j
@Jeff Hale Appreciate it! Makes sense. It would be nice to be able to be more prescriptive with `expect`because I'm not specifying from what field the expect is trying to match.
Copy code
{
  "match": {
    "prefect.resource.id": "webhook.resource.id"
  },
  "match_related": {
    "prefect.resource.name": "ovt_completion"
  },
  "after": [],
  "expect": [
    "webhook.called",
    "ODIN PROD*"
  ],
  "for_each": [],
  "posture": "Reactive",
  "threshold": 1,
  "within": 0
}
@Jeff Hale @Will Raphaelson Is there a way to structure an automation so that it will start when four distinct deployments enter completed status within an hour? I know I can setup a threshold of say four, but I don't want the automation to trigger if one deployment completed four times. Only when all four deployments enter completed status within an hour.
j
Not that I know of, John, but Will might know better.
πŸ‘ 1
w
Thanks John, this is currently unsupported, but its on our radar for an enhancement to the triggers API. Your current direction is the closest one can get without using multiple automations or custom code.
πŸ‘ 1