Search code examples
aws-lambdaserverlessserverless-framework

Add trigger on AWS lambda using Serverless


Having an existing SQS queue on AWS (using ARN), how can I set it as a trigger for a function on serverless.yml?

service: test-service

provider:
  name: aws
  runtime: nodejs18.x
  stage: dev 
  region: eu-central-1

functions:
  test:
    handler: index.handler


Solution

  • That is one of the most basic use cases for AWS Lambda and Serverless Framework. Just add an events key like this:

    functions:
      test:
        handler: index.handler
        events:
          - sqs: arn:aws:sqs:region:XXXXXX:InsertYourExistingQueueHere
    

    You can find more detailed examples on Serverless.com's SQS Queues documentation here: https://www.serverless.com/framework/docs/providers/aws/events/sqs

    Additionally, you mentioned in your original post that you created the SQS Queue outside of your serverless.yaml. I suggest that you should create your SQS Queue from within your serverless config. There are may benefits to doing so. If you choose to proceed: Serverless.com recommends using serverless-lift plugin to create and manage the queue for you (see bottom of doc linked above).