Vor
Vor

Reputation: 35099

How to check for a SNS in ec2?

As far as I understand SNS can push messages to subscribers. I have a simple script that will push message to the SQS. But what I want to do is basically: I have an SNS topic, Have an EC2 instance, Have a SQS. SQS subscribed to the SNS Topic.

I want to do the following, when I create a new msg in SNS, I want EC2 received this notification to go check an SQS. I can do the last part (go and check for an SQS) but don't understand how to receive msg??????

For example,

PUB:

context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % '5556')
socket.send("HI")  

SUB

socket.recv()

From example above, PUB should be my SNS and SUB is my EC2, right? So how to listen for a new SNS message?

Upvotes: 2

Views: 269

Answers (1)

Abdullah Khawer
Abdullah Khawer

Reputation: 5678

If you want a simple way to set up a listener that includes automatic deletion of messages when they're finished being processed, and automatic pushing of exceptions to a specified queue, you can use the pySqsListener package.

You can set up a listener like this:

from sqs_listener import SqsListener

class MyListener(SqsListener):
    def handle_message(self, body, attributes, messages_attributes):
        run_my_function(body['param1'], body['param2']

listener = MyListener('my-message-queue', 'my-error-queue')
listener.listen()

There is a flag to switch from short polling to long polling - it's all documented in the README file.

Upvotes: 1

Related Questions