You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
plugins:
...
- serverless-lift
- serverless-offline-sqs # MUST BE ABOVE serverless-offline OR ELSE IT WON'T WORK
- serverless-offline...constructs:
myQueue:
type: queue# fifo: true # if you want fifoworker:
handler: src/functions/myQueue.handlerprovider:
environment:
...MY_QUEUE_URL: ${construct:myQueue.queueUrl} # You'll need this for deployments, but not for running locallycustom:
serverless-offline-sqs:
autoCreate: trueregion: us-east-2endpoint: http://localhost:4566apiVersion: '2012-11-05'accessKeyId: foobar # localstack authsecretAccessKey: foobar # localstack authqueueName: myQueue # required for lift support, not supported by default but we'll fix that
Set up your publisher
import{SQSClient}from'@aws-sdk/client-sqs';import{SendMessageCommand}from'@aws-sdk/client-sqs';exportconstsqs=newSQSClient({region: 'us-east-2',endpoint: 'http://localhost:4566'});// Somewhere else in the code...console.log('Publishing event...');awaitsqs.send(newSendMessageCommand({MessageBody: '{"hello": "there"}',QueueUrl: 'myQueue'}));
Set up your consumer
import{SQSBatchResponse,SQSEvent}from'aws-lambda/trigger/sqs';import{writeFileSync}from'fs';voidimport('source-map-support/register');// Optional but recommendedasyncfunctionserver(event: SQSEvent,context: Record<string,unknown>): Promise<void|{}|SQSBatchResponse>{for(constrecordofevent.Records){console.log('Consuming event...');console.log({event: record.body});}return{};}module.exports.handler=server;
Patch serverless-offline-sqs:
This plugin has had issues with lift since forever:
thanks @serg06! I wanted to point out that I was getting an error while running the script
sed: 1: "node_modules/serverless ...": extra characters at the end of n command
I'm in macos so the only work around was to add an empty string in the sed command sed -i "" "s|const sqsEvent = new SQSEventDefinition|// patched\n const {queueName} = this.options;\n\n if (queueName) rawSqsEventDefinition.queueName = queueName;\n\n const sqsEvent = new SQSEventDefinition|" "$lib"
Guide for setting up serverless-offline-sqs + serverless-lift + localstack sqs
I just spent the last few hours figuring this out, so I'll share my solution with you all.
Disclaimer: This guide is only for sls offline. It likely needs modification before deploying.
Disclaimer: You will have to modify
serverless-offline-sqs
as it doesn't work withlift
by default.Set up the SQS server
Install the Node packages:
Set up your serverless.yaml
Set up your publisher
Set up your consumer
Patch serverless-offline-sqs:
This plugin has had issues with lift since forever:
In order to fix that, you need to apply a patch:
Ideally you'd apply that patch in your
postinstall
script, but you can do it manually too.Test it out
The text was updated successfully, but these errors were encountered: