2

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?

Thales Minussi
  • 6,965
  • 1
  • 30
  • 48
User7354632781
  • 2,174
  • 9
  • 31
  • 54

1 Answers1

5

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

Thales Minussi
  • 6,965
  • 1
  • 30
  • 48
  • Thanks for the link, it's hard to keep up sometimes! – bwest Mar 26 '19 at 15:08
  • You're more than welcome. Now imagine for us, mere mortals who don't work at AWS, how hard it is to keep up with the pace as you folks release tons of services a year. Please, slow the pace down :p – Thales Minussi Mar 26 '19 at 15:09
  • @ThalesMinussi Thanks but i know how to trigger lambda from aws console. What i am asking is how do i update the SQS template yaml to trigger another lambda. – User7354632781 Mar 26 '19 at 15:28
  • Could you please explain a bit more some of the parts in the yaml? Presumably we replace "MySqsQueue.Arn" with the arn of the SQS? How about "MySqsQueue:" a few lines below? should this be the name of the SQS? – user3062260 Jul 24 '19 at 16:40
  • Hello, @user3062260. Not really. This is CloudFormation and `MySqsQueue` is the definition of a Queue. `!GetAtt MySqsQueue.Arn` fetches its ARN for you. CloudFormation is way too complicated though. If you are just getting started I suggest you go either with the Serverless Framework or Terraform – Thales Minussi Jul 24 '19 at 17:07
  • Thanks for responding so quickly, So will this yaml file create its own Queue? Or do I need to have previously created one? I've tried adding the whole arn as stated on the SQS resource that I've created, and also tried the method you have described for a queue which hadn't been previously defined. I seems to throw errors: Template error: resource MySqsQueue does not support attribute type arn in Fn::GetAtt. If I remove GetAtt is throws an error: ...failed to satisfy constraint: Member must satisfy regular expression pattern: arn:regex Do you have any idea what may be causing this? – user3062260 Jul 25 '19 at 09:01
  • I am sorry, @user3062260. This question is something "old" so I didn't pay too much attention when getting back to you. This is not pure CloudFormation, this is AWS SAM (which is as complicated IMHO). You can check this blog post for reference when creating SQS -> Lambda Subscription purely with CF. The syntax is slightly different though. https://www.itonaut.com/2018/07/11/sqs-queue-as-lambda-trigger-in-aws-cloudformation/ – Thales Minussi Jul 25 '19 at 09:12
  • https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html this may also be helpful. Check the last .yml file on that page and you will see how to fetch the ARN from a SqsQueue with CF. – Thales Minussi Jul 25 '19 at 09:14
  • 1
    Finally found the error! The naming is very fussy! I had used a standard CamelCase name for my projects queue and thought nothing of it but for some reason "DbfToEaLambdaQueue" doesn't work but "MySqsQueue" is fine. Just posting in case anyone else gets stuck on this! Here is the regex the autogenerated arn must satisfy: arn:(aws[a-zA-Z0-9-]*):([a-zA-Z0-9\-])+:([a-z]{2}((-gov)|(-iso(b?)))?-[a-z]+-\d{1})?:(\d{12})?:(.*) Thanks again Thales – user3062260 Jul 25 '19 at 09:53