Skip to content

Instantly share code, notes, and snippets.

@rahulinux
Last active April 4, 2019 11:18
Show Gist options
  • Save rahulinux/5da7898a6c1e24d3df21f0ccb8f55aad to your computer and use it in GitHub Desktop.
Save rahulinux/5da7898a6c1e24d3df21f0ccb8f55aad to your computer and use it in GitHub Desktop.
notify-sns

Following client script will help to publish message in sns topic

#!/usr/bin/env python

import boto3
import json

REGION = "eu-west-1"

def notify_sns(message, sns_topic_arn):
    client = boto3.client("sns", region_name=REGION)
    response = client.publish(
            TopicArn=sns_topic_arn,
            Message=json.dumps({'default': message}),
            MessageStructure='json'
            )
    return response

message = "cycle has been successfully completed"
sns_topic_arn = 'arn:aws:sns:FAKE_ID'
print(notify_sns(message, sns_topic_arn))

Output :

{'MessageId': 'bbff11b6-70ce-HIDDEN', 'ResponseMetadata': {'RequestId': '2b3f47dd-1115-56f1-HIDDEN', 'HTTPStatusCode': 200, 'HTTPHeaders': {'x-amzn-requestid': '2b3f47dd-1115-56f1-HIDDEN', 'content-type': 'text/xml', 'content-length': '294', 'date': 'Thu, 25 Oct 2018 12:47:09 GMT'}, 'RetryAttempts': 0}}

Input in Lambda function :

{
    "Records": [
        {
            "EventSource": "aws:sns",
            "EventVersion": "1.0",
            "EventSubscriptionArn": "arn:aws:sns:eu-west-1:HIDDEN",
            "Sns": {
                "Type": "Notification",
                "MessageId": "7368932a-9c37-57ad-HIDDEN,
                "TopicArn": "arn:aws:sns:eu-west-1:HIDDEN",
                "Subject": null,
                "Message": "cycle has been successfully completed",
                "Timestamp": "2018-10-25T12:45:20.196Z",
                "SignatureVersion": "1",
                "Signature": "HIDDEN",
                "SigningCertUrl": "HIDDEN",
                "UnsubscribeUrl": "HIDDEN",
                "MessageAttributes": {}
            }
        }
    ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment