In this example we will look at how to use SNS to create a pub/sub system in our serverless app using Serverless Stack (SST). We’ll be creating a simple checkout flow.

Requirements

Create an SST app

Let’s start by creating an SST app.

$ npx create-serverless-stack@latest pub-sub
$ cd pub-sub

By default our app will be deployed to an environment (or stage) called dev and the us-east-1 AWS region. This can be changed in the sst.json in your project root.

{
  "name": "pub-sub",
  "stage": "dev",
  "region": "us-east-1"
}

Project layout

An SST app is made up of two parts.

  1. lib/ — App Infrastructure

    The code that describes the infrastructure of your serverless app is placed in the lib/ directory of your project. SST uses AWS CDK, to create the infrastructure.

  2. src/ — App Code

    The code that’s run when your API is invoked is placed in the src/ directory of your project.

Adding SNS Topic

Amazon SNS is a reliable and high-throughput messaging service. You are charged based on the number of API requests made to SNS. And you won’t get charged if you are not using it.

Replace the lib/MyStack.js with the following.

import * as sst from "@serverless-stack/resources";

export default class MyStack extends sst.Stack {
  constructor(scope, id, props) {
    super(scope, id, props);

    // Create Topic
    const topic = new sst.Topic(this, "Ordered", {
      subscribers: ["src/receipt.main", "src/shipping.main"],
    });
  }
}

This creates an SNS topic using sst.Topic. And it has two subscribers. Meaning when the topic is published, both the functions will get run.

Setting up the API

Now let’s add the API.

Add this below the sst.Topic definition in lib/MyStack.js.

// Create the HTTP API
const api = new sst.Api(this, "Api", {
  defaultFunctionProps: {
    // Pass in the topic arn to our API
    environment: {
      topicArn: topic.snsTopic.topicArn,
    },
  },
  routes: {
    "POST /order": "src/order.main",
  },
});

// Allow the API to access the topic
api.attachPermissions([topic]);

// Show the API endpoint in the output
this.addOutputs({
  ApiEndpoint: api.url,
});

Our API simply has one endpoint (/order). When we make a POST request to this endpoint the Lambda function called main in src/order.js will get invoked.

We’ll also pass in the arn of our SNS topic to our API as an environment variable called topicArn. And we allow our API to publish to the topic we just created.

Adding function code

We will create three functions, one handling the /order API request, and two for the topic subscribers.

Add a src/order.js.

export async function main() {
  console.log("Order confirmed!");
  return {
    statusCode: 200,
    body: JSON.stringify({ status: "successful" }),
  };
}

Add a src/receipt.js.

export async function main() {
  console.log("Receipt sent!");
  return {};
}

Add a src/shipping.js.

export async function main() {
  console.log("Item shipped!");
  return {};
}

Now let’s test our new API.

Starting your dev environment

SST features a Live Lambda Development environment that allows you to work on your serverless apps live.

$ npx sst start

The first time you run this command it’ll take a couple of minutes to deploy your app and a debug stack to power the Live Lambda Development environment.

===============
 Deploying app
===============

Preparing your SST app
Transpiling source
Linting source
Deploying stacks
dev-pub-sub-my-stack: deploying...

 ✅  dev-pub-sub-my-stack


Stack dev-pub-sub-my-stack
  Status: deployed
  Outputs:
    ApiEndpoint: https://gevkgi575a.execute-api.us-east-1.amazonaws.com

The ApiEndpoint is the API we just created. Let’s test our endpoint. Run the following in your terminal.

$ curl -X POST https://gevkgi575a.execute-api.us-east-1.amazonaws.com/order

You should see {status: 'successful'} being printed out. And if you head back to the debugger, you should see Order confirmed!.

Publishing to our topic

Now let’s publish a message to our topic.

Replace the src/order.js with the following.

import AWS from "aws-sdk";

const sns = new AWS.SNS();

export async function main() {
  // Publish a message to topic
  await sns
    .publish({
      // Get the topic from the environment variable
      TopicArn: process.env.topicArn,
      Message: JSON.stringify({ ordered: true }),
      MessageStructure: "string",
    })
    .promise();

  console.log("Order confirmed!");

  return {
    statusCode: 200,
    body: JSON.stringify({ status: "successful" }),
  };
}

Here we are getting the topic arn from the environment variable, and then publishing a message to it.

Let’s install the aws-sdk.

$ npm install aws-sdk

And now if you head over to your terminal and make a request to our API.

$ curl -X POST https://gevkgi575a.execute-api.us-east-1.amazonaws.com/order

You’ll notice inside the debug log that our topic subscribers are called. And you should see Receipt sent! and Item shipped! printed out.

Deploying to prod

To wrap things up we’ll deploy our app to prod.

$ npx sst deploy --stage prod

This allows us to separate our environments, so when we are working in dev, it doesn’t break the API for our users.

Cleaning up

Finally, you can remove the resources created in this example using the following commands.

$ npx sst remove
$ npx sst remove --stage prod

Conclusion

And that’s it! We’ve got a completely serverless checkout system, powered by SNS. Check out the repo below for the code we used in this example. And leave a comment if you have any questions!