Add SNS Notifications to DevOps

Add SNS notifications to DevOps workflows — AWS Cloud & DevOps with Python tutorial, lesson 45. Learn how to set up Amazon Simple Notification Service (SNS) to alert your team on pipeline events, with hands-on steps and best practices.

Focus: add sns notifications to devops workflows

Sponsored

You've automated your builds, tests, and deployments, but when something fails at 3 AM, how do you know? Staring at a silent CI/CD dashboard is a recipe for disaster. The pain is clear: without proactive notifications, your DevOps workflows are flying blind, and every failure becomes an incident you discover too late. That’s exactly why you need to add SNS notifications to your DevOps workflows — to turn silent pipelines into loud, actionable alerts that reach your team the moment something matters.

The problem this lesson solves

In a modern DevOps environment, your pipelines run when you're not watching. A failed deployment, a flaky test, or a cost anomaly can go unnoticed for hours, turning a small issue into a major outage. The problem isn't that failures happen — it’s that you’re not notified. Manual polling is inefficient, Slack and email don't integrate natively with your build tools, and custom hooks are brittle. You need a reliable, AWS-native service that can route notifications to any endpoint — email, SMS, Lambda, or an HTTP webhook. That service is Amazon Simple Notification Service (SNS), and wiring it into your workflows is a core DevOps skill.

Core concept / mental model

Think of SNS as a pub/sub broadcast system. One publisher sends a message to a topic, and SNS fans that message out to every subscriber that has opted in. It’s like a radio station: the station broadcasts on a frequency, and any radio tuned to that frequency receives the signal. The station (publisher) doesn’t need to know who’s listening, and the radios (subscribers) don’t affect each other.

In your DevOps workflow, the publisher is your pipeline — a build step, a Lambda function, or a test runner. The topic is a logical channel like deploy-failures or build-notifications. Subscribers can be an email address, a mobile phone (for SMS), an HTTP endpoint for a Slack webhook, or another AWS service like Lambda. This decoupling lets you add, remove, or change notification channels without touching your pipeline code. You just publish a message to the topic, and SNS handles the routing.

Key definitions you'll use: - Topic: A communication channel you publish to. - Publisher: The component that sends messages (your Python script or pipeline). - Subscriber: The endpoint that receives messages (email, SMS, HTTPS, Lambda). - Message:** The payload, which can be plain text, JSON, or a structured protocol like application/json.

How it works step by step

Adding SNS notifications to your DevOps workflow follows a predictable sequence.

Step 1: Create an SNS topic

Your topic is the hub. You can create it via the AWS console, the CLI, or boto3. Choose a meaningful name like devops-alerts.

Step 2: Subscribe your target endpoint

Each subscriber needs a subscription. For email, you enter an address; SNS sends a confirmation request. The user must click the link in that email to confirm. For SMS, you provide a phone number. For HTTP/S, you provide a URL (often a Slack or Teams webhook). SNS assigns an arn to each subscription.

Step 3: Publish messages from your workflow

In your Python code, you use the boto3 sns client to call publish with the topic ARN and a message. Your pipeline triggers this call on success, failure, or specific conditions.

Step 4: Handle delivery

SNS handles delivery, retries, and format. You don't worry about the mechanics — you just publish and trust SNS to fan out.

This flows logically: publisher → topic → subscribers. No tight coupling, no missed alerts.

Hands-on walkthrough

Let’s get your hands dirty. We’ll set up an SNS topic, subscribe an email, and publish a test message from Python. Then we’ll integrate that into a fake CI/CD script.

Prerequisites

  • Python 3.10+ with boto3 installed (pip install boto3)
  • AWS credentials configured (via aws configure or environment variables)
  • An email address you can check

Create the topic and subscription

First, let’s create a topic and subscribe your email.

import boto3

sns = boto3.client('sns', region_name='us-east-1')

# Create topic
response = sns.create_topic(Name='devops-alerts')
topic_arn = response['TopicArn']
print(f'Topic created: {topic_arn}')

# Subscribe email (replace with your address)
email = 'your-email@example.com'
sub_response = sns.subscribe(
    TopicArn=topic_arn,
    Protocol='email',
    Endpoint=email
)
print(f'Subscription ARN: {sub_response['SubscriptionArn']}')

Expected output:

Topic created: arn:aws:sns:us-east-1:123456789012:devops-alerts
Subscription ARN: arn:aws:sns:us-east-1:123456789012:devops-alerts:abc123...

Pro tip: Check your inbox and click the Confirm subscription link before publishing. Until you confirm, SNS won’t deliver to that endpoint.

Publish a simple message

Now publish a test notification.

import boto3

topic_arn = 'arn:aws:sns:us-east-1:123456789012:devops-alerts'
sns = boto3.client('sns', region_name='us-east-1')

# Publish plain text
response = sns.publish(
    TopicArn=topic_arn,
    Message='Hello from your DevOps pipeline! Build finished successfully.',
    Subject='Build success'
)
print(f'Message ID: {response['MessageId']}')

Expected output:

Message ID: 59c4b4d0-9d1e-4f2e-b8c9-1234567890ab

You should receive that email within a minute.

Integrate with a CI/CD script

Here’s a realistic wrapper that sends a failure alert if a deployment step fails.

import boto3
import subprocess
import sys

def notify(subject, message):
    sns = boto3.client('sns', region_name='us-east-1')
    sns.publish(
        TopicArn='arn:aws:sns:us-east-1:123456789012:devops-alerts',
        Subject=subject,
        Message=message
    )

def deploy():
    print('Starting deployment...')
    # Simulate a deployment step
    result = subprocess.run([sys.executable, 'deploy.py'], capture_output=True)
    if result.returncode != 0:
        notify(
            'DEPLOYMENT FAILED',
            f'Python deployment failed on host {__import__("socket").gethostname()}.'
        )
        raise SystemExit(1)
    notify('DEPLOYMENT OK', 'Python deployment completed successfully.')

if __name__ == '__main__':
    deploy()

Expected output:

Starting deployment...
(If deploy.py fails, you'll get an SNS email; otherwise a success email.)

This pattern works in Jenkins, GitHub Actions, or GitLab CI — just call a Python script that publishes to SNS.

Compare options / when to choose what

Not every notification method fits every scenario. Here’s how SNS stacks up against alternatives.

Option Pros Cons When to use
SNS (email/SMS) Native AWS, simple, reliable, fan-out to many endpoints Requires subscription confirmation; email can be noisy Team alerts, critical failures, low-volume notifications
SNS → Lambda Can transform messages, integrate with Slack/Datadog More moving parts, Lambda cold starts Advanced routing, custom logic, integration with other services
Slack webhook directly Immediate, rich formatting Couples your pipeline to Slack; no fan-out Quick notification when only Slack is needed
CloudWatch alarms Good for metric thresholds, auto-scaling events Not designed for pipeline events; separate service Infrastructure health, not build/deploy status

Choose SNS when you need a single source of truth for notifications across multiple channels — email, SMS, and internal tools. Prefer a direct webhook for a quick, throwaway alert. Use SNS + Lambda when you need to filter or enrich messages before they hit Slack.

Troubleshooting & edge cases

You’ll hit real-world issues. Here’s how to fix the common ones.

Email subscription stays “Pending confirmation”

If your subscription ARN shows arn:aws:sns:...:pending confirmation, you haven’t clicked the confirmation link. Resend with:

sns.confirm_subscription(
    TopicArn=topic_arn,
    Token='...'  # from the URL
)

Or re-subscribe and click the link in the email.

Messages published but not received

  • Check subscription confirmation — same as above.
  • For SMS, verify the phone number is in a supported region and has a valid country code.
  • Check IAM permissions — the publisher (your script’s role) needs sns:Publish on the topic, and the subscriber needs sns:Subscribe.
  • Incorrect topic ARN — double-check the ARN is exactly as created; a typo causes silent failure.

Message size limits

SNS has a 256 KB limit per message. For larger payloads, store data in S3 and send a reference via SNS.

Spammy topics

Too many alerts? Implement a filter policy on the subscription to only receive specific message attributes. Or move to a Lambda subscriber that batches and deduplicates.

Delivery failures

SNS retries for HTTP/S endpoints, but for email there’s no retry — if the email server is down, the message is lost. Use a queue (SQS) or Lambda as a fallback for critical alerts.

What you learned & what's next

You’ve mastered the core of adding SNS notifications to DevOps workflows. Let’s recap what you can now do:

  • Create an SNS topic and manage subscriptions via boto3.
  • Publish messages from Python scripts, including from your CI/CD pipeline.
  • Understand the pub/sub mental model and why SNS decouples producers from consumers.
  • Choose between SNS, Lambda, and direct webhooks based on your needs.
  • Debug common issues like pending confirmations and IAM permission errors.

You’ve covered both learning objectives: explaining the core concept and completing a practical exercise. Next, you’ll dive into Monitoring Python Apps on AWS — learning how to use CloudWatch to collect logs and metrics, and trigger SNS notifications automatically based on thresholds. That’s where SNS really shines in a full observability pipeline.

Practice recap

Quick exercise: Modify the CI/CD script from this lesson to publish a JSON message with a status attribute (like {"status": "failed"}) and add a filtering policy on your email subscription that only allows status = failed. Test by triggering a fail and a success. This reinforces your control over alert noise and message structure — both key for real production pipelines.

Common mistakes

  • Skipping the email confirmation step — your subscription stays pending and no messages get through.
  • Publishing to a topic with IAM permissions that are too restrictive; the sns:Publish call fails silently.
  • Relying on email as the only subscriber for critical alerts — email isn't real-time; combine with SMS or Slack via Lambda.
  • Hardcoding sensitive endpoints or ARNs in your code instead of using environment variables or AWS Secrets Manager.

Variations

  1. Use SNS as a trigger for a Lambda function instead of sending email directly, allowing custom processing, formatting, and forwarding to Slack or Teams.
  2. Use SNS with SQS fan-out to decouple multiple consumers that each need a copy of the notification message.
  3. Use a Slack incoming webhook URL as an HTTPS subscription to SNS, turning SNS into a unified alert router.

Real-world use cases

  • CI/CD pipeline failure alerts: send an email and SMS to the on-call engineer when a deployment fails, with a link to the build logs.
  • Security scanning notifications: publish an alert to SNS when a vulnerability scan finds critical issues, triggering a Lambda to open a ticket.
  • Cost anomaly detection: an AWS Lambda function monitors spending and publishes a message to SNS, which then notifies the finance team via email and Slack.

Key takeaways

  • SNS is a pub/sub service that decouples your pipeline from its notification channels.
  • Always confirm email and SMS subscriptions before expecting delivery.
  • Use boto3 to create topics, subscribe endpoints, and publish messages from Python.
  • Monitor IAM permissions and message size limits to avoid silent failures.
  • Choose SNS when you need multi-channel fan-out; a direct Slack webhook is simpler for one-off alerts.
  • Integrate SNS with Lambda or SQS for advanced routing and reliability.

Sponsored

Sponsored

Discussion

Questions, corrections, and tips help everyone reading this page.

0 comments

Add a comment

Shown publicly with your comment.

Be constructive · max 4,000 characters

No comments yet — start the thread.

Related tutorials, quizzes, and articles for this topic.