4
votes

I want to trigger Lambda function whenever new message added to SQS. Note that I don't want to add new message (events) to SQS.

What I'm trying to do:

  1. My app will send message to SQS
  2. Whenever new message added to queue CloudWatch event gets generated
  3. CloudWatch Event triggers lambda

Problem:

In AWS console while configuring CloudWatch Events I haven't found any option to add source of event i.e. URL or Name of my SQS queue.

I'm not sure if this use case is valid but please help me out.

2

2 Answers

4
votes

EDIT: AWS now supports SQS as an event source to trigger Lambda functions. See this blog post for more details.

ORIGINAL ANSWER: SQS is not supported as a direct event source for AWS Lambda functions. If there are properties of a queueing system that you need for your use case, then you could have a "cron-job" type Lambda function that runs on a schedule, receives messages from the queue, and calls your worker Lambda function in response to each message received. The problem with this approach is that you must continually poll SQS even during periods when you don't expect messages, which incurs unnecessary cost.

The easiest approach is to use SNS instead. Create a topic, publish events to that topic instead of adding a message to an SQS queue, and have your Lambda function subscribe to that SNS topic. It will then be invoked each time a message is published to that SNS topic. There's a tutorial on this approach here:

http://docs.aws.amazon.com/lambda/latest/dg/with-sns-example.html

3
votes

I would recommend to change your approach.

Your application should publish a message to an existing SNS topic. Your SQS and Lambda should than subscribe to this SNS topic.

Application -> publish -> SNS_TOPIC
                            -> SQS is notified
                            -> Lambda is notified