Skip to content

0.76.0: SNS -> publish to subscribed SQS queue, "Records" assumption leads to lost message, empty array. #134

@bitsofinfo

Description

@bitsofinfo

using 0.76.0

My code is sending a message to SNS like this:

    sns = boto3.client(service_name='sns',
                                endpoint_url=http://localhost:4002)

    message = {
        "my_event": {
            "x": "something"
        }
    }

    # publish it
    response = sns.publish(
        TopicArn=event_topic_id,
        Subject="test-my-event",
        Message=json.dumps(message),
        MessageAttributes={
            "data_ingress_type": {
                "DataType": "String",
                "StringValue": "test"
            }
        })  

I have an SQS queue (in serverless-offline-sqs) that is subscribed to the event topic the code is publishing to (which is properly setup and I see the queue existing in elasticmq)

  serverless-offline-sns:
    port: 9324
    debug: true
    subscriptions:
      - topic: ${self:provider.environment.INGRESS_EVENT_TOPIC_NAME}
        queue: http://localhost:9324/queue/my-events

When I startup the app, I see the queue being successfully subscribed to my serverless-offline-sns created topic as expected.

When the code runs to send the SNS message, it is never received.

Tracking things down in the code I see that sns-server.publish() gets called and for each subscription it creates a new SNS message that ends up looking like this which wraps the message above that I published via the boto client:

event = JSON.stringify(

{
  "SignatureVersion": "1",
  "Timestamp": "2021-08-01T23:07:52.737Z",
  "Signature": "EXAMPLE",
  "SigningCertUrl": "EXAMPLE",
  "MessageId": "c0f69f87-ce52-483d-ac74-c61f579fee28",
  "Message": "{\"my_event\": { \"x\": \"something\"}}",
  "MessageAttributes": {
    "data_ingress_type": { "Type": "String", "Value": "test" }
  },
  "Type": "Notification",
  "UnsubscribeUrl": "EXAMPLE",
  "TopicArn": "arn:aws:sns:us-west-2:123456789012:my-events",
  "Subject": "test-my-event"
}   

The sns-server.publish() method then calls sns-server.publishSqs(). In particular this line erroneously assumes that that event contains a Records property array... which it doesn't... then yields an empty array [] ultimately resulting in my message being lost and never sent:

const records = JSON.parse(event).Records ?? [];

Seems like the sns-server.publishSqs() event is expecting the passed event to be in the format of the helpers.createSnsLambdaEvent() structure instead?

export function createSnsLambdaEvent(

Original issue comment:
#88 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions