This Serverless-offline plugin emulates AWS λ and SQS queue on your local machine. To do so, it listens SQS queue and invokes your handlers.
- Serverless Webpack support.
- SQS configurations: batchsize.
serverless-offline-sqs to your project:
npm install serverless-offline-sqs
Then inside your project's
serverless.yml file, add following entry to the plugins section before
serverless-offline (and after
serverless-webpack if presents):
plugins:- serverless-webpack- serverless-offline-sqs- serverless-offline
How it works?
To be able to emulate AWS SQS queue on local machine there should be some queue system actually running. One of the existing implementations suitable for the task is ElasticMQ.
We also need to setup actual queue in ElasticMQ server, we can use AWS cli tools for that. In example, we spawn-up another container with
aws-cli pre-installed and run initialization script, against ElasticMQ server in separate container.
Once ElasticMQ is running and initialized, we can proceed with the configuration of the plugin.
Note that starting from version v3.1 of the plugin, it supports autocreation of SQS fifo queues that are specified in the cloudformation
The configuration of function of the plugin follows the serverless documentation.
functions:mySQSHandler:handler: handler.computeevents:- sqs: arn:aws:sqs:region:XXXXXX:MyFirstQueue- sqs:arn: arn:aws:sqs:region:XXXXXX:MySecondQueue- sqs:queueName: MyThirdQueuearn:Fn::GetAtt:- MyThirdQueue- Arn- sqs:arn:Fn::GetAtt:- MyFourthQueue- Arn- sqs:arn:Fn::GetAtt:- MyFifthQueue- Arnresources:Resources:MyFourthQueue:Type: AWS::SQS::QueueProperties:QueueName: MyFourthQueueMyFifthQueue: # Support for Fifo queue creation starts from 3.1 onlyType: AWS::SQS::QueueProperties:QueueName: MyFifthQueue.fifoFifoQueue: trueContentBasedDeduplication: true
The configuration of
aws.SQS's client of the plugin is done by defining a
custom: serverless-offline-sqs object in your
serverless.yml with your specific configuration.
You could use ElasticMQ with the following configuration:
custom:serverless-offline-sqs:autoCreate: true # create queue if not existsapiVersion: '2012-11-05'endpoint:region: eu-west-1accessKeyId: rootsecretAccessKey: rootskipCacheInvalidation: false