In the Debug a data pipeline tutorial, you learned how to troubleshoot failed flow runs. In this tutorial, you’ll learn how to set up an email alert that proactively notifies your team about failures. Alerts let team members engage with the troubleshooting process, even if they don’t typically log into Prefect Cloud.

This tutorial starts where the Set up a platform for data pipelines tutorial leaves off, so complete that one first. You will need a paid Prefect Cloud account.

Set up an automation

Automations are used to set up alerts in Prefect. First, you need to create a new automation.

  1. Sign in to Prefect Cloud, and use the workspace switcher to open the staging workspace.
  2. Go to Automations and then click Add Automation.

Specify the trigger

An automation triggers whenever a specified condition is met.

  1. Select the Flow run state trigger type.
  2. Trigger whenever the data-pipeline flow has flow runs that enter the Failed or Crashed states.
  3. Click Next to go to the next step.

Specify the action

Once an automation triggers, it can perform one or more actions.

  1. Choose the Send a notification action type
  2. In order to send an email notification, you need to create a block. Blocks store configuration and provide an interface for external systems.
    1. Under Block, click Add.
    2. Find the Email block, and then click Create.
    3. Specify email-alerts as the block name, and provide a JSON list of email addresses to send notifications to (e.g. ["you@example.com"]).
    4. Click Create to save the block.
  3. Click Next to go to the next step.

Save the automation

Give the automation a name and description so that you can identify its purpose later.

  1. Name: email-alert
  2. Description: Send an email when the 'data-pipeline' flow fails
  3. Click Save.

You’ve now created an automation that sends an email whenever the data-pipeline flow in the staging workspace has a failed run.

Test the alert

To test the alert, you need to trigger a flow run failure in the staging workspace.

# Switch to the staging workspace (if not already in it)
prefect cloud workspace set --workspace "<account handle>/staging"

# Run the script to simulate a failed flow run
python simulate_failures.py --fail-at-run 1 --runs 1

After the script finishes, you should receive an email with details about the failed flow run. The email should look similar to the following:

Subject: Prefect flow run notification

Flow run data-pipeline/courageous-buffalo observed in state Failed at 2024-12-11 18:59:17.212585+00:00.
Flow ID: 89053371-0d7e-4ef6-9a5f-63ea69689f66
Flow run ID: 33c2dac9-7d2a-48dd-a8cf-3894c6cd6063
Flow run URL: https://app.prefect.cloud/account/9b649228-0419-40e1-9e0d-44954b5c0ab6/workspace/a2127afa-ad10-4862-bdb0-5d065df64c8a/flow-runs/flow-run/33c2dac9-7d2a-48dd-a8cf-3894c6cd6063
State message: Flow run encountered an exception: Exception: Run failed

You can open the URL to view the flow run in Prefect Cloud.

Next steps

In this tutorial, you used Prefect Cloud automations to set up an email alert.

To learn more about automations:

Need help? Book a meeting with a Prefect Product Advocate to get your questions answered.