---
title: "Send SMS from an Airflow DAG using AWS SNS"
description: "How to configure SNS subscription to send SMS messages and use Airflow to send them"
author: "Bartosz Mikulski"
author_bio: "Principal AI Engineer & MLOps Architect. I bridge the gap between \"it works in a notebook\" and \"it works for 200 million users.\""
author_url: https://mikulskibartosz.name
author_linkedin: https://www.linkedin.com/in/mikulskibartosz/
author_github: https://github.com/mikulskibartosz
canonical_url: https://mikulskibartosz.name/send-sms-from-airflow-using-aws-sns
---

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.

## Define an SNS topic and subscription

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

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

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

```json
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:

```json
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:

```python
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'
)
```