<@ULVA73B9P> Can I set an email automation on a fl...
# ask-marvin
j
@Marvin Can I set an email automation on a flow-run, but only for the production deployment and not the development one?
m
thought for 462.1 seconds
Yes. In Prefect 3.x you can scope an Automation to only your “production” deployment and exclude “development”. Two common ways to do it: Option A — Use a production tag (recommended) 1) Tag your deployments differently - Production deployment: add a tag like
production
- Development deployment: add a tag like
dev
For example, when deploying in code:
Copy code
from prefect import flow

@flow
def my_flow():
    ...

# Production
my_flow.deploy(
    name="my-flow-prod",
    work_pool_name="prod-pool",
    tags=["production"]
)

# Development
my_flow.deploy(
    name="my-flow-dev",
    work_pool_name="dev-pool",
    tags=["dev"]
)
2) Create an Automation that targets only production - Trigger: Flow run state changes (e.g., Failed and/or Crashed) - Filter (Related resource): Tag equals
production
- Action: Send Notification (choose your Email notification destination) This filter matches runs that carry the
production
tag (from either the deployment or the flow run). Option B — Target a specific deployment If you prefer not to use tags, you can filter by the specific deployment: - Filter (Related resource): Deployment equals your production deployment (select it in the UI) This ensures only runs from that deployment trigger the email. Programmatic example (if you’d rather define it in Python):
Copy code
from prefect.automations import Automation
from prefect.events.schemas.automations import EventTrigger
from prefect.events.actions import SendNotification

automation = Automation(
    name="Email on Prod Failures",
    trigger=EventTrigger(
        expect={"prefect.flow-run.Failed"},
        posture="Reactive",
        # Filter by tag:
        match_related={
            "prefect.resource.id": "prefect.tag.production",
            "prefect.resource.role": "tag",
        }
        # Or, filter by a specific deployment id (replace UUID):
        # match_related={
        #     "prefect.resource.id": "prefect.deployment.<DEPLOYMENT-UUID>",
        #     "prefect.resource.role": "deployment",
        # }
    ),
    actions=[
        SendNotification(
            # In the UI, select your Email notification destination
            subject="PROD failure",
            body="A production flow run has failed."
        )
    ],
)
automation.create()
Notes - Tags on deployments automatically show up on related flow-run events, so filtering by
production
will only hit production. - You can also add tags to individual flow runs if needed; the same
prefect.tag.<name>
filter works. - Make sure you have an Email notification destination configured in your workspace (Settings -> Notifications). If you don’t see Email, you can use a webhook to your email provider or another channel like Slack. Helpful docs: - Automations - Deployments If you share how you distinguish prod vs dev today (tags, names, or separate projects), I can give you the exact filter to use in the UI.
j
@Marvin How do I set the deployment's tag in prefect.yaml?
m
thought for 104.0 seconds
In Prefect 3.x, set deployment tags in prefect.yaml under the deployment entry’s
tags
field. Minimal example:
Copy code
prefect-version: "3.0"
name: my-project

pull:
  - prefect.deployments.steps.set_working_directory:
      directory: .

deployments:
  - name: my-deployment
    entrypoint: flows/my_flow.py:my_flow
    tags:
      - production
      - critical
    parameters: {}
    work_pool:
      name: default
      work_queue_name: null
      job_variables: {}
Key path:
deployments[].tags
(a list). Tags you set here propagate to flow runs created from this deployment. CLI equivalents if you prefer flags when running `prefect deploy`: - Single tag:
prefect deploy --tag production
- Multiple tags:
prefect deploy --tag production --tag critical
(repeat the flag) - Short form:
prefect deploy -t production -t critical
Notes: - This is the same tags field name in 2.x and 3.x; however, 3.x uses
prefect deploy
(the old
prefect deployment build
and
Deployment.build_from_flow()
are removed).