In this article, I am going to show you how to use Terraform to create and configure an SNS topic that delivers SMS messages to a mobile phone number. In the second part, I will use the SnsPublishOperator to send messages from Airflow.

Table of Contents

  1. Define an SNS topic and subscription
  2. Sending SMS message from Airflow

Define an SNS topic and subscription

First, we have to define a new aws_sns_topic resource in Terraform:

resource "aws_sns_topic" "send-sms" {
    name = "send-sms"
}

After that, we have to add each recipient as a new SNS topic subscription:

resource "aws_sns_topic_subscription" "send-sms-recipient" {
    topic_arn = aws_sns_topic.send-sms.arn
    protocol = "sms"
    endpoint = "phone number with country code"
}

In the end, we have to define an access policy that allows publishing messages:

data "aws_iam_policy_document" "sns_topic_policy" {
  policy_id = "__default_policy_ID"

  statement {
    actions = [
      "SNS:Publish"
    ]

    effect = "Allow"

    principals {
      type        = "AWS"
      identifiers = ["aws_identifier_of_the_AWS_user_used_by_Airflow"]
    }

    resources = [
      aws_sns_topic.send-sms.arn,
    ]

    sid = "__default_statement_ID"
  }
}

Sending SMS message from Airflow

To send a text message, we have to call the SnsPublishOperator inside an Airflow DAG. Of course, we can use templates to pass values to the message:

from airflow.contrib.operators.sns_publish_operator import SnsPublishOperator

send_sms = SnsPublishOperator(
    task_id='send_sms',
    target_arn='sns_topic_arn',
    message='Here is the message. You can use the template variables to get values from XCom or DAG parameters.',
    aws_conn_id='aws_connection_id'
)
Subscribe to the newsletter
Now Enrolling: A new cohort for my premium course on fixing AI hallucinations. Limited 'Founding Member' spots available. Learn more