Email Alerts from Snowflake

Last Updated: 2023-10-25


You can send your team email alerts directly from Snowflake. This is useful when you want to monitor:

You can do this by pairing a Snowflake Alert with a Notification Integration. Notification Integrations are Snowflake constructs that provide an interface to 3rd party services (e.g. queues, email, etc). Email-type integrations are natively supported.

  1. Create a Notification Integration for Email
CREATE NOTIFICATION INTEGRATION email_integ
TYPE=EMAIL
ENABLED=TRUE
ALLOWED_RECIPIENTS=('rigomoran@mail.com');
  1. Define a query to watch for a specific condition
SELECT SUM(credits_used) AS credits_used
FROM snowflake.account_usage.WAREHOUSE_METERING_HISTORY
WHERE start_time between CURRENT_DATE() and CURRENT_TIMESTAMP()
AND credits_used > 1;

This query shows the total credit consumption over the current day. This helps ensure we keep our consumption low.

  1. Send a test email
CALL SYSTEM$SEND_EMAIL(
    'email_integ',
    'rigomoran@mail.com',
    'Email Alert: High Credit Usage for Warehouse',
    'Credit usage has exceeded 1 credit for today'
)

Snowflake provides a stored procedure to the email.

  1. Create an Alert
CREATE OR REPLACE ALERT ALERT_HIGH_CREDIT_USAGE
WAREHOUSE = COMPUTE_WH
SCHEDULE = '30 MINUTES'
IF (EXISTS (
	-- Check for a specific condition
)
THEN 
	-- Send an email
;

This is the basic syntax for an Alert. It requires a WAREHOUSE to handle the compute, a SCHEDULE to set the interval, an IF condition to check for, and THEN the action to perform.

  1. Put everything together
CREATE OR REPLACE ALERT ALERT_HIGH_CREDIT_USAGE
WAREHOUSE = COMPUTE_WH
SCHEDULE = '30 MINUTES'
IF (EXISTS (
	-- Check for a specific condition
    SELECT SUM(credits_used) AS credits_used
    FROM snowflake.account_usage.WAREHOUSE_METERING_HISTORY
    WHERE start_time between CURRENT_DATE() and SNOWFLAKE.ALERT.SCHEDULED_TIME()
    AND credits_used > 1;
)
THEN 
	-- Send an email
	CALL SYSTEM$SEND_EMAIL(
    'email_integ',
    'rigomoran@mail.com',
    'Email Alert: High Credit Usage for Warehouse',
    'Credit usage has exceeded 1 credit for today'
);

References

  1. Alerts Overview Snowflake Docs. snowflake.com
  2. Snowflake Alerts AICG Channel. youtube.com