DEV Community

Cover image for Track your infrastructure events with AWS SNS and Notify17
Alberto Marchetti
Alberto Marchetti

Posted on

Track your infrastructure events with AWS SNS and Notify17

AWS SNS is a widely used notification service for messages coming from a multitude of AWS services, like EC2, SQS, Lambda, CloudWatch, and so on.

SNS can be used pretty easily to let you know what happens around your infrastructure. Let's look at some use-cases, where you may need to get a message:

  • A CloudWatch alarm has turned into ALERT state, you should definitely know that, check SNS for CloudWatch docs or EventBridge docs
  • An EC2 instance needs replacement, which means it's most likely to stop working soon! Set up an SNS alert by following CloudWatch docs
  • Any custom code you run in your EC2 instances or Lambda functions can generate an SNS message with one simple command

All of these cases share the fact that you need to be alerted because of an event, and that this event is meaningful for your infrastructure.

You know what makes a difference? The location where you receive these alerts. There is a fine line between being properly ready to act when an alert comes up, and ending up forgetting about it (don't we know how many blood emails we get all the time, and how many we just lose in the history?).

Notify17 has been my answer to the issue of losing alerts on the way. Notify17 is a mobile app where you can centralize notifications from a multitude of sources: as long as you can generate a webhook, an HTTP request, you can get a notification on your phone.

Notify17 also integrates seamlessly with AWS SNS! This means that you can create an SNS HTTPS endpoint straight to a Notify17 raw API key or notification template, without the need to manually confirm the subscription.

A simple SNS flow

Can you give me an example?

Yes! You can experiment with AWS SNS topics and Notify17 by cloning and deploying the https://gitlab.com/notify17-examples/aws-sns-terraform-example repository.

In that example repository you can find a terraform project, which shows you:

The general concept is that you can trigger SNS notifications from any kind of AWS resource, and be notified immediately on your mobile phone. If you use AWS CLI, this is an example of a command you can run to generate SNS notifications from any client:

aws sns publish \  
  --region us-east-1 \  
  --topic-arn "arn:aws:sns:us-east-1:123456789012:n17-example-aws-sns-test-hook-templated" \  
  --message "Hello!"

# Optionally you can also use
  [--subject "Notification title"]
  [--message-attributes '{"myKey1":{"DataType":"String","StringValue":"Mr. Anderson"}}']
Enter fullscreen mode Exit fullscreen mode

Just give me a notification template!

If you just want to get started with Notify17 and SNS, you can import the following notification template, to catch all possible SNS messages:

  1. Import the template into Notify17.
  2. Add https://hook.notify17.net/api/template/TEMPLATE_API_KEY/sns as HTTPS endpoint for your SNS topic.
  3. Publish and receive your SNS notifications!.

Note: you can find more info about AWS SNS and Notify17 in the docs!

How to import

label: SNS catch-all  
title: |  
  {{   
    default  
      (  
        printf   
        "SNS message - %s"   
        (last (splitList ":" .TopicArn))  
      )   
      .Subject   
  }}  
content: |-  
  {{ if isJson .Message }}  
  {{/* Decode the message and dump its content */}}  

  {{ dump (parseJson .Message) }}  

  {{ else }}  
  {{/* This is a normal text message */}}  

  {{ .Message }}  

  {{ if .attributesMap }}  
  {{/* If we have any attributes, let's show them */}}  
  Attributes: {{ dump .attributesMap | nindent 2 }}  
  {{ end }}  

  {{ end }}  
testFieldType: yaml  
testBodyYAML: |-  
  Message: Hello there!  
  MessageAttributes:  
    name:  
      Type: String  
      Value: General Kenobi  
  MessageId: 88302c8d-9d71-514c-bbb8-746c487fde28  
  Subject: Greetings!  
  Timestamp: 2021-04-26T03:01:02.694Z  
  TopicArn: arn:aws:sns:us-east-1:123:test-sns-template  
  attributesMap:  
    name: General Kenobi
Enter fullscreen mode Exit fullscreen mode

Discussion (0)