user100123122
user100123122

Reputation: 339

Polling an AWS SQS queue for messages with certain attributes

I have set up a standard queue with AWS SQS and I want to poll this queue for messages containing a specific attribute, preferably using the boto3 library in python. I know that boto3 has a method recieve_message() which polls messages from the queue. However, I want to only get those messages which contain a specific attribute. A naive approach is to iterate through the receive_message() output and check if a message in receive_message() contains the attribute, but I was wondering if there is another solution to this problem.

Upvotes: 3

Views: 6932

Answers (2)

lennon310
lennon310

Reputation: 12689

You can't filter certain messages with SQS solely, however, you can do that with SNS.

You can publish the messages to an SNS topic. The message filter feature of SNS enables endpoints subscribed to an SNS topic to receive only the subset of topic messages it is interested in. So you can ensure only the relevant messages with specific attributes are enqueued to the consumer's queue.

Refer to Filter Messages Published to Topics and SNS subscription filtering policies.

Upvotes: 7

jellycsc
jellycsc

Reputation: 12259

Selective polling is not supported in SQS ReceiveMessage API. An alternative is to let your EC2 send messages containing different attributes into different SQS queues.

Upvotes: 0

Related Questions