Scott Pedersen
09/20/2024, 4:52 AMMarvin
09/20/2024, 4:53 AMScott Pedersen
09/20/2024, 5:07 AMMarvin
09/20/2024, 5:08 AMjson
{
"event": "deployment-trigger",
"resource": {
"prefect.resource.id": "deployment.{{ body.deployment_id }}",
"prefect.resource.name": "{{ body.friendly_name }}"
}
}
Step 2: Send an event to the webhook
A POST request may look something like this:
sh
curl -X POST <https://api.prefect.cloud/hooks/your-webhook-id> -d '{"deployment_id":"1", "friendly_name":"Deploy_1"}'
Step 3: Create an automation
Set up an automation in Prefect that reacts to this event.
- Trigger: Configure the trigger to match the event type deployment-trigger
.
- Action: Use conditional logic in automation rules to start the appropriate deployment based on the deployment_id
or other content in the event.
Example Automation Setup (Prefect 2.x and 3.x):
Use the Prefect UI or API to create an automation. The trigger might look like:
python
from prefect import flow, serve
from prefect.events import DeploymentEventTrigger
@flow
def deploy_1():
print("Executing Deployment 1")
@flow
def deploy_2():
print("Executing Deployment 2")
if __name__ == "__main__":
deployment_1 = deploy_1.to_deployment(name="deployment_1")
deployment_2 = deploy_2.to_deployment(name="deployment_2")
serve(deployment_1, deployment_2)
Ensure your automation checks the content of the event and decides which deployment to trigger:
python
trigger = DeploymentEventTrigger(
expect={"deployment-trigger"},
match_related={"prefect.resource.id": "deployment_1"},
actions=[...]
)
another_trigger = DeploymentEventTrigger(
expect={"deployment-trigger"},
match_related={"prefect.resource.id": "deployment_2"},
actions=[...]
)
References:
- Prefect 2.x Webhooks Documentation
- Prefect 2.x Automations Documentation
- Prefect 3.x Webhooks Documentation
- Prefect 3.x Automations Documentation
With these steps, you can set up an automation in Prefect that reacts to webhook events and triggers different deployments based on the content of those events.Scott Pedersen
09/24/2024, 8:54 PMdeployment_1
would be replaced with whatever deployment name is sent through as part of the web hook payload, and using a single automation we will use it to trigger the deployment (based on the deployment name).Marvin
09/24/2024, 8:54 PMprefect.yaml
file.
1. Setup your `prefect.yaml`:
Add a trigger definition for your deployment in prefect.yaml
. This allows you to render the event data (like deployment names) using Jinja templates.
2. Webhook Setup:
Define a webhook that captures the data from the event and triggers the deployment. You can use curl or any HTTP method to simulate this.
Here is an example:
yaml
deployments:
- name: my-deployment
triggers:
- type: webhook
parameters:
deployment_name: "{{ event.payload.deployment_name }}"
You can then use a webhook to send the deployment name dynamically:
bash
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=my-deployment-1"
### Prefect 3.x
Similarly, in Prefect 3.x, this feature is quite similar but offers enhanced capabilities such as inferencing for dynamic events. You can still make use of prefect.yaml
and webhooks to define event-driven deployments.
1. Setup your `prefect.yaml`:
Add the trigger definition in your deployment YAML.
2. Define Webhooks:
Use webhook templates with dynamic placeholders to trigger appropriate deployments.
Here is an example configuration:
yaml
deployments:
- name: my-deployment
triggers:
- type: webhook
parameters:
deployment_name: "{{ context.webhook.payload.deployment_name }}"
Invoke the webhook like so:
bash
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=my-dynamic-deployment"
### References
- Prefect 2.x Automation
- Prefect 2.x Webhooks Guide
- Prefect 3.x Automations and Triggers
- Prefect 3.x Webhook Triggers
These configurations and examples should help you dynamically trigger deployments using Prefect based on the webhook payload received.Scott Pedersen
09/24/2024, 9:23 PMMarvin
09/24/2024, 9:23 PMScott Pedersen
09/24/2024, 9:25 PMMarvin
09/24/2024, 9:26 PMyaml
deployments:
- name: dynamic-deployment
triggers:
- type: webhook
parameters:
deployment_name: "{{ event.payload.deployment_name }}"
You can send different payloads to this webhook to trigger different deployments.
bash
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=deployment_1"
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=deployment_10"
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=deployment_15"
### Prefect 3.x
Prefect 3.x also supports this functionality, providing advanced trigger mechanisms and flexible event handling.
Example:
yaml
deployments:
- name: dynamic-deployment
triggers:
- type: webhook
parameters:
deployment_name: "{{ context.webhook.payload.deployment_name }}"
Invoke the webhook with different payloads to trigger various deployments.
bash
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=deployment_1"
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=deployment_10"
curl -X POST <https://api.prefect.cloud/hooks/YOUR_HOOK_ID> -d "deployment_name=deployment_15"
References:
- Prefect 2.x Guide: Triggers and Automations
- Prefect 3.x Guide: Automations and Triggers
This approach allows you to manage and trigger different deployments dynamically based on the payload of the incoming webhook, making your workflows highly adaptable and responsive to external events.Scott Pedersen
09/24/2024, 9:27 PMyaml
deployments:
- name: dynamic-deployment
triggers:
- type: webhook
parameters:
deployment_name: "{{ event.payload.deployment_name }}"
Marvin
09/24/2024, 9:27 PMprefect.yaml
file. This is where you define various deployment settings, including triggers that handle events from webhooks.
### Example Configuration in prefect.yaml
Here is how you can define deployment triggers within your `prefect.yaml`:
yaml
deployments:
- name: dynamic-deployment
entrypoint: path/to/your_flow.py:flow_function
work_pool:
name: my-work-pool
triggers:
- type: webhook
parameters:
deployment_name: "{{ event.payload.deployment_name }}"
### Steps to Use It
1. Prepare the YAML File: Add the necessary configuration to your prefect.yaml
file as shown in the example.
2. Deploy: Use the prefect deploy
command to deploy this configuration.
bash
prefect deploy -n dynamic-deployment --trigger your_trigger_file.yaml
### References
- Prefect 2.x Automations DocumentationScott Pedersen
09/24/2024, 9:28 PMprefect.yaml
the only way this can be done, or can it be done programmatically using the Prefect API?Marvin
09/24/2024, 9:28 PMprefect.yaml
file is one way to define deployment triggers, you can also do this programmatically using the Prefect API in both Prefect 2.x and 3.x. This allows you to create and manage your deployments with more flexibility directly within your Python code.
### Prefect 2.x
You can define deployment triggers programmatically using Prefect's deployment-related classes and methods.
Example:
python
from prefect import flow
from prefect.events import DeploymentEventTrigger
@flow(log_prints=True)
def my_flow(param_1: str):
print(param_1)
if __name__ == "__main__":
my_flow.deploy(
name="dynamic-deployment",
triggers=[
DeploymentEventTrigger(
enabled=True,
match={"prefect.resource.id": "some_resource"},
expect=["resource.updated"],
parameters={
"deployment_name": "{{ event.payload.deployment_name }}"
}
)
]
)
### Prefect 3.x
Similarly, in Prefect 3.x, you can use deployment APIs to achieve this programmatically.
Example:
python
from prefect import flow, serve
from prefect.events import DeploymentEventTrigger
@flow(log_prints=True)
def my_flow():
print("Running dynamic deployment")
if __name__ == "__main__":
deployment = my_flow.to_deployment(
name="dynamic-deployment",
triggers=[
DeploymentEventTrigger(
expect={"prefect.resource": "some_event"},
match_related={"prefect.resource.name": "{{ context.webhook.payload.deployment_name }}"}
)
]
)
serve(deployment)
References:
- Prefect 2.x Automations Documentation
- Prefect 3.x Automations Documentation
This provides a more programmatic way of setting up your deployments, giving you the flexibility to define and manage them using Python in addition to the standard YAML configurations.Scott Pedersen
09/26/2024, 1:44 AMMarvin
09/26/2024, 1:45 AMpython
import requests
from prefect import flow
from prefect.events import DeploymentEventTrigger
@flow(log_prints=True)
def my_flow(param_1: str):
print(param_1)
if __name__ == "__main__":
deployment = my_flow.deploy(
name="my-deployment",
triggers=[
DeploymentEventTrigger(
enabled=True,
match={"prefect.resource.id": "some_resource"},
expect=["resource.updated"],
parameters={
"deployment_name": "{{ event.payload.deployment_name }}"
}
)
]
)
# Create a webhook via API
webhook_payload = {
"event": "model-update",
"resource": {
"prefect.resource.id": "product.models.{{ body.model_id }}",
"prefect.resource.name": "{{ body.friendly_name }}",
"run_count": "{{ body.run_count }}"
}
}
response = <http://requests.post|requests.post>(
"<https://api.prefect.cloud/hooks/YOUR_HOOK_ID>",
json=webhook_payload,
headers={"Authorization": "Bearer YOUR_PREFECT_API_TOKEN"}
)
if response.status_code == 200:
print("Webhook created successfully")
### Prefect 3.x
Similarly, Prefect 3.x allows for dynamic event handling using the Prefect API:
Example:
python
import requests
from prefect import flow, serve
from prefect.events import DeploymentEventTrigger
@flow(log_prints=True)
def my_flow():
print("Running dynamic deployment")
if __name__ == "__main__":
deployment = my_flow.to_deployment(
name="dynamic-deployment",
triggers=[
DeploymentEventTrigger(
expect={"prefect.resource": "some_event"},
match_related={"prefect.resource.name": "{{ context.webhook.payload.deployment_name }}"}
)
]
)
serve(deployment)
# Create a webhook via API
webhook_payload = {
"event": "model-update",
"resource": {
"prefect.resource.id": "product.models.{{ body.model_id }}",
"prefect.resource.name": "{{ body.friendly_name }}",
"run_count": "{{ body.run_count }}"
}
}
response = <http://requests.post|requests.post>(
"<https://api.prefect.cloud/hooks/YOUR_HOOK_ID>",
json=webhook_payload,
headers={"Authorization": "Bearer YOUR_PREFECT_API_TOKEN"}
)
if response.status_code == 200:
print("Webhook created successfully")
### References
- Prefect 2.x Automations Documentation
- Prefect 3.x Automations Documentation
- Prefect 3.x Webhook Triggers
By leveraging these examples, you can programmatically create and manage automations linked to webhooks, allowing you to dynamically trigger deployments based on incoming webhook data.Scott Pedersen
09/26/2024, 1:52 AMMarvin
09/26/2024, 1:52 AMpython
import requests
from prefect import flow
from prefect.events import DeploymentEventTrigger
@flow(log_prints=True)
def my_flow(param_1: str):
print(param_1)
if __name__ == "__main__":
deployment = my_flow.deploy(
name="my-deployment",
triggers=[
DeploymentEventTrigger(
enabled=True,
match={"prefect.resource.id": "some_resource"},
expect=["resource.updated"],
parameters={
"deployment_name": "{{ event.payload.deployment_name }}"
}
)
]
)
# Assuming the webhook already exists
webhook_id = "YOUR_EXISTING_HOOK_ID"
# Use API to reference the webhook in automation
automation_payload = {
"name": "my_automation",
"triggers": [
{
"type": "webhook",
"match": {"prefect.resource.id": webhook_id},
"expect": ["custom.webhook.triggered"],
"parameters": {
"deployment_name": "{{ event.payload.deployment_name }}"
}
}
]
}
response = <http://requests.post|requests.post>(
"<https://api.prefect.cloud/automations/>",
json=automation_payload,
headers={"Authorization": "Bearer YOUR_PREFECT_API_TOKEN"}
)
if response.status_code == 200:
print("Automation created successfully")
else:
print("Failed to create automation:", response.text)
### Prefect 3.x
Similarly, in Prefect 3.x, you can reference an existing webhook in your automation programmatically.
Example:
python
import requests
from prefect import flow, serve
from prefect.events import DeploymentEventTrigger
@flow(log_prints=True)
def my_flow():
print("Running dynamic deployment")
if __name__ == "__main__":
deployment = my_flow.to_deployment(
name="dynamic-deployment",
triggers=[
DeploymentEventTrigger(
expect={"prefect.resource": "custom.webhook.triggered"},
match_related={"prefect.resource.name": "{{ context.webhook.payload.deployment_name }}"}
)
]
)
serve(deployment)
# Assuming the webhook already exists
webhook_id = "YOUR_EXISTING_HOOK_ID"
# Use API to reference the webhook in automation
automation_payload = {
"name": "my_automation",
"triggers": [
{
"type": "webhook",
"match": {"prefect.resource.id": webhook_id},
"expect": ["custom.webhook.triggered"],
"parameters": {
"deployment_name": "{{ event.payload.deployment_name }}"
}
}
]
}
response = <http://requests.post|requests.post>(
"<https://api.prefect.cloud/automations/>",
json=automation_payload,
headers={"Authorization": "Bearer YOUR_PREFECT_API_TOKEN"}
)
if response.status_code == 200:
print("Automation created successfully")
else:
print("Failed to create automation:", response.text)
### References
- Prefect 2.x Automations Documentation
- Prefect 3.x Automations Documentation
- Prefect 3.x Webhook Triggers
By using an existing webhook ID and referencing it in your automation payload, you can dynamically create and manage automations that respond to webhook events.