Search code examples
node.jsaws-lambdaamazon-sqsserverlesslocalstack

Using localstack SQS as an event source for a lambda on serverless offline


Is have a node.js lambda on serverless offline which I want to invoke with a localstack SQS as an event source.

I took the localstack version from here:
https://github.com/localstack/localstack

and deployed it to my docker with

docker compose up -d

The container runs perfectly and I can interact with the SQS service using the AWS CLI.

In my serverless.yaml I included the serverless-localstack plugin and configured it like this:

app: my-app
service: my-service

frameworkVersion: "3"

provider:
  name: aws
  runtime: nodejs18.x
  stage: api
  region: us-east-1

functions:
  update:
    handler: src/handler.update
    events:
      - sqs:
          arn:
            Fn::GetAtt:
              - myQueue:
              - Arn

plugins:
  - serverless-localstack
  - serverless-dotenv-plugin
  - serverless-offline
  - serverless-webpack

custom:
  localstack:
    host: http://localhost
    edgePort: 4566
    stages:
      - api
  serverless-offline:
    lambdaPort: 3001
    httpPort: 3000
  dotenv:
    path: .env.development

resources:
  Resources:
    myQueue:
      Type: AWS::SQS::Queue
      Properties:
        QueueName: my-queue

I made my handler print a log when it is invoked, but even though I do have messages in this queue, the handler is not invoked.
I'm running my function with sls offline.

What am i doing wrong?


Solution

  • Hi — LocalStack Serverless Plugin doesn't support such a setup as of now. The plugin starts the LocalStack container and sets up LocalStack as the endpoint for the SDK clients that the Serverless Framework uses. It does not consider if the user uses the Serverless Offline plugin. Apart from this, the Serverless Plugin creates another extra layer to mock the Lambas and API Gateway which doesn't communicate with LocalStack.