<@ULVA73B9P> How would you pass a string in the bo...
# ask-marvin
l
@Marvin How would you pass a string in the body of an HTTP Request to a webhook and trigger a deployment using the value of that body?
m
To send a string in the body of an HTTP request using Prefect, you would need to ensure that the HTTP request is properly formatted with the appropriate headers and body content. If the Content-Type of the request is
application/json
, the body will be parsed as a JSON object and made available to the webhook templates. If it is
application/x-www-form-urlencoded
, the body will be parsed into a flat dictionary-like object of key-value pairs. Regarding triggering a deployment using a webhook in Prefect, there appears to be functionality related to Prefect Cloud Webhook CLI as mentioned in the release notes for Prefect 2.0a4, indicating a streamlined way to deploy flows. However, the provided snippets do not include specific examples of how a webhook triggers a deployment. For a more detailed guide or examples on how to specifically trigger a deployment using a webhook, we would need to search Prefect's official documentation or GitHub for instances where this process is described in full. If you are looking for code examples or step-by-step instructions, please let me know so I can assist you further.
l
The body of the HTTP request is formatted correctly. I can see that an automation can trigger a flow run from an event, but how do you pass the body contents of that event to the triggered flow run's parameter? A code example would be helpful
n
@Marvin The body of the HTTP request is formatted correctly. I can see that an automation can trigger a flow run from an event, but how do you pass the body contents of that event to the triggered flow run's parameter? A code example would be helpful
m
In Prefect, you can configure a deployment to be triggered by specific events and pass event data into the deployment run as parameters. This is done through Jinja templating and the automation system in Prefect Cloud. Here’s a code example to help illustrate this:
Copy code
python
triggers: [
    {
        "enabled": True,
        "match": "prefect.resource.id: prefect.flow-run.*",
        "expect": [
            "prefect.flow-run.Completed"
        ],
        "match_related": {
            "prefect.resource.name": "prefect.flow.etl-flow",
            "prefect.resource.role": "flow"
        },
        "parameters": {
            "param_1": "{{ event }}"
        }
    }
]
In this example, the trigger is set up to start a flow run upon the completion of an upstream flow specified in the
match_related
key. The event data is passed into the flow run as a parameter named
param_1
. In webhooks, you can also accept JSON payloads using a simple pass-through Jinja template. For instance, you could use the following template to convert the entire body of the webhook to JSON, which could then be passed to a Prefect flow as a parameter:
Copy code
python
{{ body|tojson }}
Using this template, you could configure your webhook to parse the incoming HTTP request body as JSON and then pass that data to the flow run. You may tailor the template to extract specific information from the event if needed. Here is a more straightforward example to designate a parameter within the webhook:
Copy code
python
parameters: {
    "my_param": "{{ body['key_from_body'] }}"
}
In this case, the value for
my_param
will be extracted from the
key_from_body
key in the incoming event's body. The name
my_param
corresponds to a parameter within the Prefect flow that is being triggered. To set up these configurations, you would typically define them in the YAML file for your deployment or via the Prefect UI when creating or managing automations and triggers. These triggers and parameters can be tailored to listen to a variety of internal or external events and then act accordingly, passing the event data to your flows seamlessly.
l
@Marvin how do you pass the parameters on the automations UI under the actions section?
m
To pass parameters through the Automations UI in Prefect under the actions section, you follow these general steps: 1. Navigate to the Automations page in your Prefect Cloud UI workspace. 2. Create a new automation by selecting the
+
icon. 3. Configure the trigger condition that will cause the automation to execute. 4. After configuring the trigger, specify one or more actions that the automation should carry out. When setting up an action to kick off a flow run, you will be given the option to define parameters for that run. The parameters can be set to static values, or you can use Jinja templating to dynamically assign values based on the event data. In the Actions configuration, there should be fields where you can specify the names and values of parameters to pass to the flow run. Here's an outline of what it might look like in the Automations UI: - Name the automation and provide a description if necessary. - Set up a trigger based on a specific event, such as a flow run state change or a work pool status change. - Define the action to run a flow, and within that action's configuration, you will find a section to specify parameters. - Enter the parameter names and their corresponding values (static values or using Jinja templates to extract from the event). Please note that precise UI elements may vary slightly based on updates and changes to the Prefect Cloud interface. If you need the exact UI navigation steps or screenshots for clarity, I would recommend consulting the latest Prefect Cloud documentation or seeking assistance directly within the Prefect Cloud UI, as details may change over time and my access to the current UI and documentation is limited to what's available publicly as of my knowledge cutoff in April 2023.
l
@Marvin can you give me an example of a jinja template?
m
Certainly! Jinja is a templating language for Python, allowing for the dynamic insertion of content into web pages or other text formats. In the context of Prefect and automation, Jinja templates are often used to dynamically substitute values into strings based on certain variables or event data. Here's a simple example of a Jinja template that accesses event data in Prefect: Assume you have an event represented as a JSON object like this:
Copy code
json
{
  "event_type": "flow_run_completed",
  "flow_run_id": "12345",
  "status": "Success",
  "triggering_event_data": {
    "param1": "value1",
    "param2": "value2"
  }
}
You want to kick off a new flow run with parameters that are extracted from the triggering event. Here's an example Jinja template for defining parameters based on the event data:
Copy code
jinja
{
  "new_param1": "{{ event.triggering_event_data.param1 }}",
  "new_param2": "{{ event.triggering_event_data.param2 }}"
}
When this template is rendered,
{{ event.triggering_event_data.param1 }}
is replaced with the actual value from the event's data (
"value1"
in this case), resulting in a JSON object that can be passed as parameters:
Copy code
json
{
  "new_param1": "value1",
  "new_param2": "value2"
}
Prefect automations can use similar templating to extract values from webhook payloads or other event data to pass as parameters when triggering flow runs. Jinja's flexibility makes it very powerful for constructing dynamic parameter payloads based on the contents of triggers and events within Prefect Cloud.
l
@Marvin What type should the properties have in the deployment YAML for the given flow run in order for the Prefect UI to accept a Jinja template as a parameter?
m
To accept a Jinja template as a parameter in Prefect UI, you would typically configure your deployment YAML file by including the Jinja template within the
parameters
section. Parameters in the
deployment.yaml
file should be defined as JSON key-value pairs. Here’s an example of how to include a Jinja template inside the `deployment.yaml`:
Copy code
yaml
# Example content in deployment.yaml
version: "1"
type: "deployment"
name: "my-deployment"
# ... other configurations ...
parameters:
  param1: "{{ event.data.value1 }}"
  param2: "{{ event.data.value2 }}"
In this case,
param1
and
param2
are parameters expected by the entrypoint flow function within your deployment, and they will be filled in with values from an event object when the flow run is triggered. The Jinja template specifies the source of the parameter values using event data. When you apply this deployment YAML via the
prefect deployment apply
command, this configuration would be set up in the Prefect Cloud API. You can then trigger flow runs based on this deployment, and the parameters will be dynamically resolved using the Jinja templates you’ve included in the YAML file. The Prefect UI doesn't always show you raw Jinja templates for direct editing. Still, it allows you to edit default parameter values from the details page for a deployment by selecting Edit from the commands menu. If the parameter values are intended to be dynamic, you typically set up the Jinja templating within the deployment YAML itself or handle them programmatically, if the UI supports Jinja templating syntax directly.
l
@Marvin Should that yaml have double-quotes? The auto-generated deployment yaml from "prefect deployment build" doesn't have any
m
In YAML, strings can generally be written with or without quotes unless the string includes special characters that could be misinterpreted (e.g., the colon
:
, which is used to denote key-value pairs, or the curly braces
{}
used for objects). If the value contains special characters, using quotes is necessary to ensure the string is interpreted correctly. For Jinja templates within YAML files, it's common practice to use quotes because the templates often contain characters that YAML could misinterpret. Here is how you might write the parameters with a Jinja template in a `deployment.yaml`:
Copy code
yaml
# Example content in deployment.yaml with quoted Jinja templates
version: "1"
type: "deployment"
name: "my-deployment"
# ... other configurations ...
parameters:
  param1: "{{ '{{ event.data.value1 }}' }}"
  param2: "{{ '{{ event.data.value2 }}' }}"
In this example, double quotes are used around the Jinja template within the YAML to clearly indicate that the enclosed
{{ ... }}
are part of a string rather than YAML syntax. However, if the auto-generated deployment YAML from
prefect deployment build
does not include quotes around Jinja template strings, it's possible that Prefect's parsing of YAML files correctly handles these templates without quotes. In that case, you can follow the generated format convention as output by the Prefect CLI tool. Be sure to test your deployment YAML with the flow and the Prefect Cloud UI to ensure that the Jinja templates are being properly interpreted. If there is any ambiguity in the parsing, or you encounter issues, you may choose to add quotes to the Jinja templates for clarity.
n
i think marvin is a little off the rails here, you wouldnt be able to do any sort of jinja templating in a
deployment.yaml
file, itd be in a
prefect.yaml
, specifically, itd be in your
triggers
for a given deployment definition. if its helpful, here's one of my webhook templates where I grab stuff off the event payload