Skip to content

Guide for setting up serverless-offline-sqs + serverless-lift + localstack sqs #239

@serg06

Description

@serg06

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 with lift by default.

Set up the SQS server

# Install Python and Docker beforehand

# Install CLI tools if you don't have them
pip install awscli  # `aws`
pip install awscli-local  # `awslocal`

# Start localstack
docker run -p 4566:4566 -d --restart unless-stopped localstack/localstack

Install the Node packages:

yarn add @aws-sdk/client-sqs
yarn add -D serverless-offline-sqs serverless-lift @types/aws-lambda

Set up your serverless.yaml

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 fifo
    worker:
      handler: src/functions/myQueue.handler

provider:
  environment:
    ...
    MY_QUEUE_URL: ${construct:myQueue.queueUrl}  # You'll need this for deployments, but not for running locally

custom:
  serverless-offline-sqs:
    autoCreate: true
    region: us-east-2
    endpoint: http://localhost:4566
    apiVersion: '2012-11-05'
    accessKeyId: foobar  # localstack auth
    secretAccessKey: foobar  # localstack auth
    queueName: 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';

export const sqs = new SQSClient({
    region: 'us-east-2',
    endpoint: 'http://localhost:4566'
});

// Somewhere else in the code...
console.log('Publishing event...');
await sqs.send(new SendMessageCommand({
    MessageBody: '{"hello": "there"}',
    QueueUrl: 'myQueue'
}));

Set up your consumer

import {SQSBatchResponse, SQSEvent} from 'aws-lambda/trigger/sqs';
import {writeFileSync} from 'fs';

void import('source-map-support/register');  // Optional but recommended

async function server(event: SQSEvent, context: Record<string, unknown>): Promise<void | {} | SQSBatchResponse> {
    for (const record of event.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:

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

sls offline --verbose --useInProcess

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions