Reputation: 613
I have an application that publishes messages via Amazon SNS to specified target. I need to get this message somehow using python. What is the simplest way to do it?
Upvotes: 7
Views: 25117
Reputation: 6505
Amazon SNS does not directly allow messages to be retrieved. Rather, for a given topic in Amazon SNS, you configure subscribers. Subscribers can be, for example, an email address, an Amazon SQS queue, an HTTP endpoint or a few other options. See https://aws.amazon.com/sns/faqs/ (search for "How does Amazon SNS work") for an overview of how it works.
To receive a published from a python script, your best bet is to setup a new Amazon SQS queue for your script, and to subscribe the Queue to the SNS topic.
You can then poll the SQS queue to see if there are any messages in the queue.
This technique has the extra advantage that you won't miss any messages even if your python script is not running - they will be waiting there for you in the SQS queue.
boto is a great python library for interracting with Amazon, and this tutorial explains how to access an SQS queue.
Alternatively, if you can deploy your Python script as a web application with an Http endpoint as an API, you can subscribe your Http endpoint to the SNS topic, and your endpoint will get invoked each time there is a new message. This techique is not recommended as if your script is offline, you will miss messages.
Upvotes: 12