DMS Airflow supports alert notifications that notify you when a DAG succeeds or fails. You can use built-in DMS message notifications or configure a custom SMTP email service to send alert emails.
Prerequisites
You have set up an Airflow environment.
Use DMS message notification
-
Go to the target code repository.
-
Configure the dependency.
from airflow.providers.alibabadms.cloud.notifications.dms_notification import DmsNotifier -
Configure alert notifications.
By default, alert notifications are sent to the workspace administrator. You can change the recipients in Notification management.
-
Failure alert: In the
DAG, add theon_failure_callbackparameter and set its value tolambda context: DmsNotifier(success=False).notify(context). -
Success alert: In the
DAG, add theon_success_callbackparameter and set its value tolambda context: DmsNotifier(success=True).notify(context).
Sample code:
dag = DAG( dag_id='iac_ove_test', on_failure_callback=lambda context: DmsNotifier(success=False).notify(context), on_success_callback=lambda context: DmsNotifier(success=True).notify(context) )NoteThe preceding configuration sets alerts at the DAG level, so only one notification is sent per DAG run. To send a separate notification for each task, place
on_failure_callbackandon_success_callbackin the DAG'sdefault_args. -
Use a custom email service
By configuring SMTP service parameters and an Airflow connection, you can use a custom email service to send Airflow alert notification emails. The configuration involves two steps:
-
Set the SMTP service parameters in the Airflow instance configuration options.
-
Create an
smtp_defaultconnection in Airflow to store the email authentication credentials.
Airflow can send alert emails only after both steps are completed.
Step 1: Configure SMTP service parameters
Log in to DMS 5.0.
-
Click the
icon in the upper-left corner of the console, and then choose .NoteIf you are using the non-compact console mode, choose from the top menu bar.
-
Click the workspace name that contains the Airflow instance, and then choose in the left-side navigation pane.
-
In the Operation column of the target Airflow instance, click Configuration Options.
-
In the dialog box that appears, click Add Configuration Option and add the following SMTP configuration options.
Importantsmtp.smtp_sslandsmtp.smtp_starttlsare mutually exclusive encryption methods and cannot be set toTrueat the same time.Configuration option
Example value
Description
smtp.smtp_hostsmtp.qiye.aliyun.comThe SMTP server address provided by your email service provider
smtp.smtp_port465The SMTP service port. Typically 465 for SSL or 587 for STARTTLS
smtp.smtp_sslTrueWhether to enable SSL encryption. If enabled, set
smtp.smtp_starttlstoFalsesmtp.smtp_starttlsFalseWhether to enable STARTTLS encryption. If enabled, set
smtp.smtp_ssltoFalsesmtp.smtp_mail_fromsender@example.comThe sender email address for alert emails
smtp.smtp_usersender@example.comThe login account for SMTP server authentication, usually the same as the sender address
-
Click Confirm.
Wait for the configuration to take effect. The Airflow instance status changes to Deploying and then back to Running, which indicates that the configuration has taken effect.
Step 2: Configure the Airflow connection
After the SMTP service parameters are configured, configure an Airflow connection to store the email authentication credentials.
-
In the Operation column of the target Airflow instance, click Open to access the Airflow Web UI.
-
In the left-side navigation pane, choose .
-
Click Add Connection in the upper-right corner and configure the following parameters.
Parameter
Value
Description
Connection ID
smtp_defaultFixed value. Do not change this value. Airflow uses this ID by default to retrieve the SMTP authentication credentials.
Connection Type
SMTP
Select SMTP
Login
Your email address
Must be the same as the
smtp.smtp_userconfiguration valuePassword
Your email password
The login password or app-specific password (authorization code) for the email account
-
Click Save.
After the configuration is complete, you can use EmailOperator in a DAG or set email_on_failure = True to trigger alert emails and verify that the SMTP configuration works.
> Airflow Instance