Home > Software engineering >  How to trigger events based of sqs queue?
How to trigger events based of sqs queue?

Time:04-20

I'm creating a sqs queue (sample below), I understand sqs queue can be directly intergrated with a lambda , such that as soon as a message comes , lambda can process the trigger. i assume lambda is polling this queue , to see if there are any message. does this means lambda is running every minute(or something similar) and also is that costly in terms of charges? can sqs add a delay to invoke the lambda? or is there a better set up, which allows to say trigger a lambda such that it doesn't fire lambda for every message that comes in but may be do it for every few message at a time or some time interval?

AWSTemplateFormatVersion: '2010-09-09'
Description: Template to create a queue

Resources:
      
 queue: 
    Type: AWS::SQS::Queue
    Properties: 
      QueueName: 'myQueue'  
      FifoQueue: true
          
Outputs:
  QueueURL:
    Description: Queue URL
    Value: !Ref queue

CodePudding user response:

You are correct in assuming lambda is polling the queue; however, the lambda itself is not running to check for messages. Lambda uses Event Source Mappings that works somewhat separately from lambda to poll the Event Source, in your case SQS.

As for how many messages your lambda processes, see the "Batching Behavior" section to get more detail, but you can set a wait time (Batch window) and a limit to how many messages a lambda processes at once (Batch size). By default, lambda waits for 10 messages in the queue or for 5 minutes, whichever comes first. When setting up your event source, these values can be changed. Checkout this guide from Amazon on a more detailed description on Using Lambda with Amazon SQS.

  • Related