Configuring an SMTP server to send e-mail notifications
Warning
This tutorial was tested on clusters with the Apache Airflow™ version below 3.0.
Use a directed acyclic graph (DAG) to configure your SMTP server to send e-mail notifications. Data for connecting to the DB is stored in Yandex Lockbox and automatically inserted into the graph.
To configure an SMTP server to send e-mail notifications, follow these steps:
- Set up your infrastructure.
- Create a Yandex Lockbox secret.
- Prepare the DAG file and run the graph.
- Check the result.
If you no longer need the resources you created, delete them.
Set up your infrastructure
-
Create a service account named
airflow-sawith the following roles:managed-airflow.integrationProviderlockbox.payloadViewer
There is no need to assign the
lockbox.payloadViewerrole for the whole folder. It is enough to assign it for a specific Yandex Lockbox secret once you create it. -
Create an Object Storage bucket in any configuration.
-
Edit the ACL of the new bucket to give the
READpermission to theairflow-saservice account. -
Create a Managed Service for Apache Airflow™ cluster with the following parameters:
- Service account:
airflow-sa. - Bucket name: Name of the new bucket.
- Use Lockbox Secret Backend: Make sure to enable this option.
Under Airflow configuration, set up additional Apache Airflow™ properties
to configure the SMTP server:Key Value smtp.smtp_hostsmtp.yandex.rusmtp.smtp_port465smtp.smtp_starttlsFalsesmtp.smtp_sslTruesmtp.smtp_mail_fromuser@yandex.ruFill in the fields manually or import the settings from a configuration file (see a configuration file example
). - Service account:
Required paid resources
The support cost for this solution includes:
- Managed Service for Apache Airflow™ cluster fee: using computing resources allocated to hosts and disk space (see Managed Service for Apache Airflow™ pricing).
- Fee for using public IP addresses if public access is enabled for cluster hosts (see Virtual Private Cloud pricing).
- Fee for an Object Storage bucket: data storage and data operations (see Object Storage pricing).
- Yandex Lockbox fee: storing secrets (see Yandex Lockbox pricing).
Create a Yandex Lockbox secret
Create a Yandex Lockbox secret named airflow/connections/smtp_default, containing json.dumps(conn), where conn is a connection to an SMTP server.
conn = {
"conn_type": "smtp",
"login": "user@yandex.ru",
"password": "<password_for_external_apps>"
}
The airflow/connections/smtp_default secret will store the data for connecting to the SMTP server.
Prepare the DAG file and run the graph
-
Create a local file named
test_smtp_notification.pyand paste the following script to it:from airflow.decorators import dag, task default_args = { "email": ["user@yandex.ru"], "email_on_failure": True, "email_on_retry": True } @dag(schedule=None, default_args=default_args) def test_smtp_notification(): @task def failing_task(): raise RuntimeError("some test error") failing_task() test_smtp_notification() -
Upload the
test_smtp_notification.pyDAG file to the bucket you created earlier. This will automatically create a graph with the same name in the Apache Airflow™ web interface. -
Make sure a new graph named
test_smtp_notificationhas appeared in the DAGs section.It may take a few minutes to upload a DAG file from the bucket.
-
To run the graph, click
in the line with its name.
Check the result
To check the result in the Apache Airflow™ web interface:
- In the DAGs section, open the
test_smtp_notificationgraph. - Go to the Graph section.
- Select failing_task.
- Go to Logs.
- Make sure the logs do not contain the
some test errorline. This means the query was successful.
Delete the resources you created
Some resources are not free of charge. Delete the resources you no longer need to avoid paying for them: