Trigger Lambda Function in AWS when the message is present in SQS Queue
Asked Answered
N

3

6

I am using AWS Lambda function to process the messages in Queue it's working fine. But i need to execute this Lambda function when messages available or added in SQS queue.

Is it possible to trigger the Lambda function based on SQS queue.Please suggest one method to achieve this goal.

Nappie answered 14/12, 2016 at 7:59 Comment(0)
D
15

Invoking Lambda functions from SQS queues is not directly supported. You can see the list of available triggers here: http://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html

Possible solutions:

  1. Replace SQS queue with Kinesis or DynamoDB. Both can trigger Lambda functions on updates.
  2. Inject SNS before SQS. SNS can add items to SQS and trigger Lambda function.

If you don't need near real-time processing, these two options are also valid:

  1. Create CloudWatch Event Rule that will trigger the Lambda function every N minutes (e.g. every minute).
  2. Create CloudWatch alarm watching ApproximateNumberOfMessagesVisible parameter for your SQS queue. This alarm should publish to an SNS topic, which in turn will trigger Lambda function.
Deacon answered 14/12, 2016 at 8:48 Comment(5)
Can you please explain this method with details : "Inject SNS before SQS. SNS can add items to SQS and trigger Lambda function"Nappie
You can publish to SNS topic thought SNS API. SNS will fanout messages to multiple endpoints.Deacon
I am not well in this AWS, because i am just beginer here. Can you please help me with detailsNappie
See docs.aws.amazon.com/sns/latest/dg/SendMessageToSQS.html for more information. Basically you change your system that pushes messages to your SQS queue; it should post the messages to an SNS topic, which will then fan out to your SQS queue if you set up the queue as a subscriber.Snuff
When injecting SNS before SQS, how do you guarantee that the lambda function is triggered after the message is added to SQS?Fallingout
S
5

Lambda now supports SQS as a native event source

https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html

Shellyshelman answered 7/9, 2018 at 19:55 Comment(0)
D
0

Using messages in a SQS queue to trigger a Lambda function is a popular and effective AWS feature. Using SQS Event Source Mapping you can achieve this.For further reference, you can refer to the official AWS documentation on configuring SQS event source mapping:

https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-configure-lambda-function-trigger.html

Dibbuk answered 12/3 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.