<@ULVA73B9P> I want to create an automation that l...
# ask-marvin
m
@Marvin I want to create an automation that launches a deployment run from an event. Can I pass an event attribute as a parameter to the deployment run?
m
Yes, you can pass an event attribute as a parameter to a deployment run in Prefect. You can create a deployment with a trigger specification that will run the deployment based on events. The event data can be passed into the deployment run as parameters using Jinja templating. Here's an outline of how this can be achieved: 1. Setup Deployment with Trigger: - Configure a deployment with a trigger that responds to specific events. - Use Jinja templating to pass event attributes as parameters to the deployment run. 2. Webhook for Event: - Define a webhook to create and send events to Prefect. - Include any relevant data in the body of the webhook request. 3. Automation and Deployment Execution: - Implement the automation to listen for the events and trigger the deployment. - Ensure flow run parameters are dynamically set based on the event data. For an in-depth example and guidance, check out the following resources: - Prefect Deployments Documentation - Prefect Automations Guide These resources will provide you with detailed steps and code snippets to help you configure and test your deployment triggers effectively.
m
@Marvin can you give me an example of using jinja templating to pass an event attribute as a parameter to the deployment run?
m
Sure, let's create a concrete example of how to use Jinja templating to pass an event attribute as a parameter to a deployment run. ### Step-by-Step Example: 1. Create a Flow: First, create a basic flow that takes parameters:
Copy code
python
   from prefect import flow

   @flow(name="process_event_data")
   def process_event_data(event_param: str):
       print(f"Received event parameter: {event_param}")

   if __name__ == "__main__":
       process_event_data("example")
2. Deployment YAML with Jinja Templating: Create a deployment YAML file where we specify the parameters using Jinja templating. `deployment.yaml`:
Copy code
yaml
   name: process_event_data_deployment
   version: 1
   schedule:
     cron: "0 0 * * *"
   flow:
     path: flow.py
     entrypoint: process_event_data
   parameters:
     event_param: "{{ event.param }}"
   triggers:
     - enabled: true
       match: 
         prefect.resource.id: prefect.flow-run.*
       expect:
         any:
           - key: "event.param"
3. Trigger the Flow with an Event: Use a webhook or another method to send an event that triggers the flow and passes attributes. Here, we simulate sending a webhook.
Copy code
sh
   curl -X POST <https://api.prefect.cloud/hooks/your-webhook-id> -d "param=event_value"
### Summary: - Flow executes and logs the passed event parameter. - Deployment YAML uses Jinja templating to substitute the event attribute into the parameter. ### Resources: For more detailed information, refer to the complete Prefect Deployment Guide and Prefect Automations Guide.