Hi, I am trying to run a prefect flow using a trig...
# ask-community
n
Hi, I am trying to run a prefect flow using a trigger from a Webhook. I am trying to figure out a way to send the data I received in the body of the webhook to the flow as a parameter, but I can’t figure out a way to do it. I followed the example given here but instead of passing
{{ event }}
(which is basically just the name of the event, I tried passing in
{{ resource.event_data }}
. Please note that
resource.event_data
is a property I defined this property in my webhook template, it gets populated from the body of the webhook I received originally. So, what’s happening is that the flow gets executed but instead of receiving the actual
resource.event_data
it just received the raw string “{{ resource.event_data }}“. Is it even possible to pass on dynamic data from the original event to the deployment as a parameter?
n
hi @Nikhil Jain - you’ll want to keep the top level thing as event so, {{ event.resource.id }} or whatever thing under resource you’re interested in passing
sorry i’m on mobile, but down on that same page you linked, there should be an E2E example where we pass part of the event body as parameters into the trigger
n
how will prefect know that it is supposed to interpret the parameter as a jinja template? Should I include something like:
"__prefect_kind": "jinja"
in the parameter definition?
Also, is it possible to set the parameters via Prefect UI? or does it have to be done via
DeploymentEventTrigger
only?
As far as I can tell, changing the parameter to
{{ event.resource.event_data }}
did not help. The flow got the raw string as the parameter instead of interpreting it as a template:
n
can you show how you defined your trigger in code? something may not be getting passed correctly, and yeah you can do it in the UI automations > new > set trigger > set action (you’d set parameters while setting the Run Deployment action)
n
Copy code
triggers=[
            DeploymentEventTrigger(
                name="dev_document_upload_event_trigger",
                enabled=True,
                match_related={
                    "prefect.resource.id": "prefect-cloud.webhook.94676184-be06-4d32-ab88-7b8e55e96ad1"
                },
                parameters={
                 "event_data_str": "{{ event.resource.event_data }}"
                },
            )
        ],
Here’s the full deployment code:
Copy code
DEPLOYMENT_CONFIGS = [
    {
        "flow": document_upload_event_handler_flow,
        "envs": ["dev"],
        "triggers": [
            DeploymentEventTrigger(
                name="dev_document_upload_event_trigger",
                enabled=True,
                match_related={
                    "prefect.resource.id": "prefect-cloud.webhook.94676184-be06-4d32-ab88-7b8e55e96ad1"
                },
                parameters={"event_data_str": "{{ event.resource.event_data }}"},
            )
        ],
    },
    {
        "flow": document_upload_event_handler_flow,
        "envs": ["staging", "prod"],
    },
]

for config in deployment_configs:
          flow.deploy(
            deployment_name,
            work_pool_name=work_pool_name,
            job_variables={
                "task_definition_arn": task_def_arn,
            },
            build=False,
            # image param is redundant because image is always picked from the
            # task-def-arn, but deploy function complains if not provided
            image=image,
            tags=tags,
            schedule=config.get("schedule", None),
            parameters=config.get("parameters", None),
            print_next_steps=False,
            triggers=config.get("triggers", None),
        )
n
🧐 hmm thats odd, that looks how I would expect i just tried the example from the docs and Im seeing templating against cloud at least what version of the SDK are you using? and are you using prefect server or cloud?
n
prefect cloud
Copy code
'prefect >= 2.14.8, < 3.0.0',
👍 1
When I click on “Show parameters” for this automation in the UI, I see this:
Copy code
{
  "event_data_str": {
    "value": "\"{{ event.resource.event_data }}\"",
    "__prefect_kind": "json"
  }
}
n
to be clear this shouldn't be necessary, but can you try this for
parameters
?
Copy code
parameters={
                  "event_data_str": {
                      "__prefect_kind": "json",
                      "value": {
                          "__prefect_kind": "jinja",
                          "template": "{{ event.resource.event_data }}"
                      },
                   },
                },
n
Will it work if I edit this in the UI? or do I have to do this via
DeploymentEventTrigger
?
n
in the UI there should be explicit options to provide JSON or Jinja, so you wouldn't be entering this
__prefect_kind
stuff
so if you're able to try my suggestion, it would be in code, i.e.
DeploymentEventTrigger
n
okay, trying that now.
btw, I just tried this via the UI:
User Jinja input
-> and then set value to
{{ event.resource.event_data }}
(without
"
) and it worked. But I want to try your suggestion using the
DeploymentEventTrigger
since that is what we’ll eventually use for deployment, not the UI
Automation Action Failed
Copy code
"Unable to create flow run from deployment: InvalidJSON()"
btw, even for the setting I did from UI using Jinja template input, the JSON parsing eventually failed in my flow because the string has single quotes instead of double quotes. This is how the parameters looked like:
Copy code
{
  "event_data_str": "{'data': {'applicationId': '8867b27e-c8d8-4219-9d74-b613d33bf143', 'id': 127239, 'tag': {'id': 82, 'name': 'DMI: Driver License Collected'}}}"
}
116 Views