User7354632781
User7354632781

Reputation: 2274

How to trigger lambda when messages published to SQS?

I am trying to implement lambda1 that'll be triggered when messages will be published to SQS. I am able to send messages to SQS queue and receive the messages. I created the SQS lambda template as follows:

  GetPatientStatusSQS:
    Type: AWS::SQS::Queue
    Properties:
      MaximumMessageSize: 1024
      QueueName: !Sub "${EnvironmentName}-GetPatientStatusSQS"
      VisibilityTimeout: 30

I checked on aws documentation but couldnt find any example showing how to trigger lambda when messages published to SQS queue.

I found this link Can an AWS Lambda function call another but not sure if that's helpful.

How do i update the SQS template above so it'll trigger the lambda1?

Upvotes: 2

Views: 2048

Answers (1)

Thales Minussi
Thales Minussi

Reputation: 7235

As per Jun 28, 2018, Lambda functions can be triggered by SQS events.

All you need to do is Subscribe your Lambda function to the desired SQS queue.

Go to SQS's console, click on your Queue -> Queue Actions -> Configure Trigger for Lambda function

enter image description here

Set the Lambda's ARN you want to send messages to and that's it, your function will now be triggered by SQS.

Keep in mind that your function will process, at most, a batch of up to 10 messages at once.

If you think you may run into concurrency issues, you can then limit your function's concurrency to 1.

Here's a sample template you can use to wire SQS and Lambda together.

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Example of processing messages on an SQS queue with Lambda
Resources:
  MySQSQueueFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: node8.10
      Events:
        MySQSEvent:
          Type: SQS
          Properties:
            Queue: !GetAtt MySqsQueue.Arn
            BatchSize: 10
  MySqsQueue:
    Type: AWS::SQS::Queue

From the docs

Upvotes: 5

Related Questions