How to use Pub/Sub in your serverless app

Link to chapter — https://serverless-stack.com/examples/how-to-use-pub-sub-in-your-serverless-app.html

FYI - The downloaded code issued the following error:

2022-06-10T21:55:21.112Z ba6d593f-be63-4105-9122-284910c5e137 INFO ws.onmessage {
“message”: “Forbidden”,
“connectionId”: “ThqXce21oAMCLuw=”,
“requestId”: “ThqXdH80IAMFwOQ=”
}

Changing from StackContext to Stack fixed it.

Hey @David can you show me the line in the repo that you changed? I’ll have the team update it. Or if you could submit a PR that’d be great.

In retrospect, believe the proper fix is to move the @serverless-stack items from devDependencies to dependencies in the package.json.

New:

“devDependencies”: {
“typescript”: “^4.7.2”,
@tsconfig/node16”: “^1.0.2”,
“vitest”: “^0.12.9”
},
“dependencies”: {
@serverless-stack/cli”: “^1.2.3”,
@serverless-stack/resources”: “^1.2.3”
},

Now I’m unable to recreate the Forbidden error. But, there is a different problem.

The sample code creates the subscriptions with the following syntax:

// Create Topic
const topic = new Topic(stack, “Ordered”, {
subscribers: {
receipt: “functions/receipt.main”,
shipping: “functions/shipping.main”,
},
});

That syntax doesn’t match the documentation at Topic | Serverless Stack (SST) which uses the following syntax:

subscribers: [“src/subscriber1.main”, “src/subscriber2.main”],

The syntax in the documentation results in an error.

Also, the syntax in the documentation for creating a fifo SNS doesn’t compile.

snsTopic: {
  fifo: true,
},

TS2345: Argument of type ‘{ snsTopic: { fifo: boolean; }; subscribers: { receipt: string; shipping: string; }; }’ is not assignable to parameter of type ‘TopicProps’. Object literal may only specify known properties, and ‘snsTopic’ does not exist in type ‘TopicProps’.

So, why doesn’t the documentation seem to be accurate?

Now realize that the correct documentation is at Topic | Serverless Stack (SST). Still, the logic to add a fifo sns doesn’t seem to work:

  // Create Topic
  const topic = new Topic(stack, "Ordered", {
    subscribers: {
      receipt: "functions/receipt.main",
      shipping: "functions/shipping.main",
    },
    cdk: {
      topic: {
        fifo: true,
      },
    },
  });

UPDATE_FAILED | AWS::SNS::Subscription | OrderedSubscriberOrderedreceiptTopic595F06C5 | Invalid parameter: Invalid protocol t
ype: lambda (Service: AmazonSNS; Status Code: 400; Error Code: InvalidParameter; Request ID: 7dc667e7-9c04-5912-9f82-3cf9d5f36d5f; Proxy: null)

How is this issue solved?

Hey @David, I just gave this a try and got the same error. After some digging, it seems FIFO Topic does not yet support Lambda subscriptions. This is a limitation on the AWS side.

I’m sure this will be supported at some point. Here’s a relavent StackOverflow discussion - amazon web services - Why do I get an error when trying to add an SNS trigger to my AWS Lambda function? - Stack Overflow.

^ Updated the doc on FIFO Topics.

How would I write unit tests for the ExampleStack when adding an sns topic?