Create an SNS Topic
I: Implement DRS
The Dash Replenishment Service (DRS) uses SNS Topics to notify you (the device maker) of changes to device status, order state, subscription status, and more.
Amazon's Simple Notification Service (SNS) is a pub-sub service from AWS that makes it easy to set up, manage, and send messages instantly to applications or users. See Notification Messages for a complete list of supported DRS notifications.
- Step 1: Create an SNS Topic for DRS
- Step 2: Allow DRS to Publish Messages to Your Topic
- Step 3: Process Incoming Notifications
Step 1: Create an SNS Topic for DRS
- Log in to the AWS Management Console.
- Select Simple Notification Service from Application Integration
- From the SNS Dashboard, click Create topic.
- Enter a Topic name and Display name, then click Create topic.
- Topic Name - Used to create a permanent unique identifier called an Amazon Resource Name (ARN), which will be used to configure your SNS Notifications.
Note: Choose a friendly Topic Name and Display Name to improve code readability.
Step 2: Allow DRS to Publish Messages to Your Topic
-
Copy the ARN you just created. You will use this to link DRS-enabled devices to your new topic in step 6.
- Click the ARN for the topic you've created.
- Click Other topic actions and then click Edit topic policy.
- From Basic View, under Allow these users to publish messages to this topic, select Only these AWS user and enter your AWS Account ID and the DRS AWS Account ID.
- To access your AWS Account ID, go to the AWS Console.
- Enter the following for the DRS AWS Account ID:
476877469412
- Click Update policy.
- Go to your DRS Self-Service portal and paste the ARN you copied in step 1 in the "Notification" field under "App Settings".
Step 3: Process Incoming Notifications
DRS will notify you of changes to the customers reordering preferences, their subscription status, their selection and orders your smart devices place using the DRS APIs. Whenever the configured SNS topic receives SNS notifications from DRS, you may choose to process these within AWS, through your existing infrastructure or outside of AWS, using an HTTPS endpoint.
This tutorial covers 2 scenarios:
- Your smart home infrastructure in built on AWS
- Your smart home infrastructure is not built on AWS
Scenario 1: Smart Home Infrastructure On AWS
In this scenario, we are going to use AWS Lambda as the sole or primary infrastructure to process the smart device capabilities.
-
From the AWS navigation bar click on Services and select "Lambda" under the "Compute" group.
- Click on "Create a function" and select "Blueprints". If you have used Lambda before, you may choose "Author from scratch" instead.
- In the "Filter" field, type "sns" and select the "sns-message" blueprint.
-
Provide a name for the new function. Under "Role" select "Create a new role from one or more templates". Mirror the settings in the following screenshot:
-
Select the SNS trigger you created on step 1. Click on "Enable trigger"
- At the bottom of the page, click on the "Create function" button.
You may now modify the function code to invoke different Lambda functions or HTTP endpoints depending on the type of notification you receive. For example, you could have the following Lambda function:
console.log('Loading function');
exports.handler = async (event, context) => {
const message = event.Records[0].Sns.Message;
const notification = JSON.parse(message);
const notificationType = notification.notificationInfo.notificationType;
if(notificationType == undefined || notificationType == null){
console.log("Not a valid DRS notification");
return message;
}
switch (notificationType){
case 'OrderPlacedNotification':
case 'OrderCancelledNotification':
case 'ItemShippedNotification':
// Update the inventory and the database
console.log("Order notification received");
break;
case 'SubscriptionChangedNotification':
// Update the companion app and the database if required.
console.log("Subscription notification received");
break;
case 'DeviceRegisteredNotification':
// If pre-registration is enabled create a user record. If pre-registration is not enabled, update the database entry for the user
console.log("Device registered notification received");
break;
case 'DeviceDeregisteredNotification':
// Database clean up
console.log("Device deregistered notification received");
break;
}
console.log('From SNS:', JSON.stringify(message));
return message;
};
Scenario 2: Smart Home Infrastructure Not On AWS
In this scenario, we will forward all SNS messages to an HTTPS endpoint that is capable of handling the incoming notifications.
The official AWS documentation contains a tutorial that explains how to set this up.
You completed the tutorial! You can find a comprehensive list of all the DRS SNS notifications here.
Last updated: Aug 07, 2018