Sending Slack Alerts on Airflow Task’s Failure

Requirements:

  1. Python > 3.0
  2. Airflow > 2.0
  3. Slack Webhook

Python Packages:

apache-airflow-providers-http

Setting up Slack

  1. Create a Slack Webhook.
  2. Create a slack_webhook connection in Airflow > Admin > Connections
  • Insert the first part of the webhook URL as host.
  • Insert later part formatted as “T00000000/B00000000/XXXXXXXXXXXXXXXXXXXXXXXX” as password.

Setting up python function in Airflow dag

  • Create a function using SlackWebhookOperator to generate the slack message.
def slack_notification(context):
slack_msg = """
:red_circle: Task Failed.
*Task*: {task}
*Dag*: {dag}
*Execution Time*: {exec_date}
*Error Msg*: {error_message}
*Log Url*: {log_url}
""".format(
task=context.get('task_instance').task_id,
dag=context.get('task_instance').dag_id,
ti=context.get('task_instance'),
exec_date=context.get('execution_date'),
error_message=context.get('exception') or context.get('reason'),
log_url=context.get('task_instance').log_url,
)
failed_alert = SlackWebhookOperator(
task_id='slack_notification',
http_conn_id='slack_webhook',
channel='#slack-channel',
message=slack_msg)
return failed_alert.execute(context=context)
  • Pass the above function as ‘on_failure_callback’ function on default_args.
default_args = {
'owner': 'Airflow',
'depends_on_past': False,
'start_date': days_ago(2),
'email_on_failure': False,
'email_on_retry': False,
'retries': 1,
'retry_delay': timedelta(seconds=60),
'on_failure_callback': slack_notification
}

--

--

--

tech enthusiast

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Divide and Conquer Algorithm

Divide and Conquer Algorithm Graphical Represenation

Inheritance in Java:

Shoutout for Kalle Hallden!

Stratos Partners ColdStack to Integrate the First Decentralized Storage Aggregator

How to Connect to an API in Unity 3D

C static libraries

Contract Law in Utah

OAuth 2.0 framework

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
anish pradhan

anish pradhan

tech enthusiast

More from Medium

How to automate data extractions using AWS

The Mystery of Folders on AWS S3

Serverless data migration from TiDB Cloud to REDSHIFT

Serverless data migration

How to use DAG to tigger another DAG in Airflow?