You may need to send a notification to a set of recipients from a Databricks notebook. For example, you may want to send email based on matching business rules or based on a command’s success or failure. This article describes two approaches to sending email or SMS messages from a notebook. Both examples use Python notebooks:
This approach requires that you have an Amazon Simple Notification Service (SNS) topic to send the notification to. Recipients who are subscribed to the SNS topic will receive the email or SMS notification.
You may need to install
boto3, a Python library for working with Amazon AWS:
pip install boto3
Add the following Python commands to your notebook, replacing <sample values> with your own:
# Import the boto3 client import boto3 # Set the AWS region name, retrieve the access key & secret key from dbutils secrets. # For information about how to store the credentials in a secret, see # https://docs.databricks.com/user-guide/secrets/secrets.html AWS_REGION = "<region-name>" ACCESS_KEY = dbutils.secrets.get('<scope-name>','<access-key>') SECRET_KEY = dbutils.secrets.get('<scope-name>','<secret-key>') sender='<email@example.com>' #Create the boto3 client with the region name, access key and secret keys. client = boto3.client('sns',region_name=AWS_REGION, aws_access_key_id=ACCESS_KEY, aws_secret_access_key=SECRET_KEY) # Add email subscribers for email in list_of_emails: client.subscribe( TopicArn=topic_arn, Protocol='email', Endpoint=email # <-- email address who'll receive an email. ) # Add phone subscribers for number in list_of_phone_numbers: client.subscribe( TopicArn=topic_arn, Protocol='sms', Endpoint=number # <-- phone numbers who'll receive SMS. ) # Send message to the SNS topic using publish method. response = client.publish( TopicArn='<topic-arn-name>', Message="Hello From Databricks..", Subject='Email Notification from Databricks..', MessageStructure='string' )