Zivile
07/11/2025, 2:20 PMMarvin
07/11/2025, 2:21 PMMarvin
07/11/2025, 2:21 PMpython
from prefect.runtime.deployment import id as deployment_id
# Access the deployment ID
current_deployment_id = deployment_id
A few important notes:
1. This only works when your flow is running as part of a deployment. It won't work if you're just running a flow locally.
2. The deployment ID is only available during runtime within the deployment's execution context.
If you need to get deployment IDs outside of a flow run (e.g., for administrative purposes), you can use the Prefect REST API client:
python
from prefect.client import get_client
async def get_deployment_info(deployment_name: str):
async with get_client() as client:
deployment = await client.read_deployment_by_name(deployment_name)
return deployment.id
You can find more details about deployment-related APIs in the Prefect documentation.Zivile
07/11/2025, 3:08 PMMarvin
07/11/2025, 3:08 PMMarvin
07/11/2025, 3:08 PMZivile
07/11/2025, 3:09 PMMarvin
07/11/2025, 3:09 PMMarvin
07/11/2025, 3:10 PMpython
from prefect.server.schemas.automations import Automation
from prefect.server.schemas.events import EventTrigger
from prefect.server.schemas.actions import CancelFlowRun
from datetime import timedelta
auto_to_create = Automation(
name="woodchonk",
description="Cancel flow runs when specific animal events occur", # Optional but recommended
enabled=True, # Optional, defaults to True
trigger=EventTrigger(
match_related={ # Use match_related instead of match for object properties
"genus": "Marmota",
"species": "monax",
},
event_names=["animal.walked"], # Use event_names instead of expect
after=None, # Optional
within_timedelta=timedelta(seconds=10), # Use within_timedelta instead of within
count_within_timedelta=3, # Use count_within_timedelta instead of threshold
),
actions=[CancelFlowRun()]
)
# Create the automation
created_automation = await auto_to_create.save() # Note: This needs to be awaited
The key corrections from your original code are:
1. The expect
parameter should be event_names
and should be a list
2. Use match_related
instead of match
for object properties
3. within
should be within_timedelta
4. threshold
should be count_within_timedelta
5. The posture
parameter is not a valid field for EventTrigger
6. The creation method is save()
not create()
, and it needs to be awaited
Also, make sure you're running this code in an async context. If you're running it in a synchronous context, you'll need to wrap it in an async function and use an async runner:
python
async def create_automation():
# ... automation creation code here ...
return await auto_to_create.save()
# Then run it using:
import asyncio
created_automation = asyncio.run(create_automation())
This should resolve the validation errors you're seeing. The errors occurred because some of the field names and structures have changed in Prefect 3.x compared to earlier versions.