A How-to Guide for Deploying Amazon Interactive Video Service and Amazon Personalize in Retail
The ever-increasing popularity of live streaming is opening up new opportunities for online customer interactions. With live streaming providing engaging content, it can facilitate the flow of customers into online shopping, joining the shopping experience with other forms of online engagement. Personalized recommendations also allow online stores to pick the right products at the right time to maximize engagement and conversions.
This blog will walk through a detailed example of how to configure and deploy live streaming and personalization into an ecommerce site. To start this, we’ll be using the AWS retail ecommerce demo environment, our open source platform, to highlight how highly scalable cloud services can enable you to transform customer experiences.
Amazon Interactive Video Service (Amazon IVS) is a managed live streaming solution that is quick and easy to set up, and ideal for creating interactive video experiences. By streaming video to Amazon IVS, you can make low-latency live video available to any viewer in the world. The service is easily customizable using the Amazon IVS Player SDK, and a timed metadata API allows metadata to be distributed to stream consumers using the same video feed. Additionally, Amazon Personalize delivers tailored product recommendations to customers.
Amazon Personalize is a fully managed recommender system that can be used in conjunction with other AWS services, like AWS Amplify and Amazon Pinpoint, to deliver a wide variety of personalized experiences to customers at scale. You can provide Amazon Personalize with historical records of customer-product interactions, in addition to product and customer metadata, and it uses machine learning (ML) to look for patterns in those interactions to recommend products to users that are likely to elicit positive responses in the future. It is also capable of recommending related products and doing personalized re-ranking of search results. Amazon Personalize can respond in real time to trends in the data and to individual behavioral changes by exploiting a runtime interface for ingesting new interactions.
Starting Point: The AWS Retail Demo Store
The AWS Retail Demo Store is an open-source sample retail web application and workshop platform intended as an educational tool for demonstrating how AWS infrastructure and services can be used to build compelling customer experiences for ecommerce, retail, and digital marketing use cases.
AWS Retail Demo Store demonstrates how AWS infrastructure and services can be used to build compelling customer experiences.
The core of the AWS Retail Demo Store is a microservices-based architecture deployed as a collection of RESTful web services in Amazon Elastic Container Service (Amazon ECS). Several AWS Managed Services are leveraged to provide build, deployment, authentication, messaging, search, and personalization capabilities. The web user interface (UI) is a single-page application built using responsive web design frameworks and techniques, producing a native app-like experience tailored to the user’s device.
The architecture uses AWS CloudFormation’s custom resources to deploy Amazon IVS channels, update the UI, and add the new “videos” service to Amazon ECS to stream the videos and expose details of the streams to the UI via a Flask API. Amazon IVS resource types in AWS CloudFormation can also be used.
The following diagram highlights the addition of IVS to the AWS Retail Demo Store.
Diagram of the reference architecture for the AWS Retail Demo Store, highlighting Amazon IVS.
The new UI view displays Amazon IVS video streams of vlog-style videos, with a vlogger presenter interacting with products. Product details are embedded as metadata in the Amazon IVS stream displays details about the product being demonstrated at a given point in time, in addition to related products suggested using Amazon Personalize.
Vlogger shown on live streaming page within AWS Retail Demo Store.
The following sections walk through the main steps to add this functionality and integrate it into the existing ecommerce platform.
Amazon IVS and AWS CloudFormation
To broadcast video with Amazon IVS, the first step is to create a channel. Each channel has capacity for a single video stream at a given point, and outputs a set of streams wrapped in a .m3u8 file, which can be consumed using the Amazon IVS Player. Amazon IVS resources are created through the console, CLI, or with the standard AWS SDKs. To integrate with the infrastructure as code framework of the existing ecommerce backend, Python boto3 library is used to create an AWS CloudFormation custom resource. Custom resources allow AWS Lambda functions to be used in place of typical pre-defined AWS CloudFormation resources, enabling bespoke implementations of AWS resources that may not be possible in default templates.
Any number of videos can be streamed for display in the ecommerce site with a custom resource to create Amazon IVS channels based on the presence of video files in a specified Amazon S3 location. A parameter mapping each video file to a stream is automatically saved in AWS Systems Manager (SSM) Parameter Store, meaning that the application streaming the video to the channel can consistently stream the same video to the same channel.
One of the key features of Amazon IVS is the ability to embed metadata directly within a stream, allowing for synchronized content to be distributed within a video stream. This metadata is sent to the Amazon IVS API for a given channel using the PutMetadata operation, and the metadata is distributed to stream consumers in real time.
To stream metadata alongside pre-recorded video for demonstration purposes, metadata is encoded into the same file as the video being streamed to allow for the best possible synchronization between the video content and the metadata being sent. For this reason, the Matroska Multimedia Container (.mkv) format is used, with metadata included within a video container as a subtitle file.
The SRT subtitle format is chosen for its simple format, readability, and ease of editing for developers; files can be edited as plaintext or when using open-source software. The following snippet shows a sample of how the SRT format is used to capture product metadata.
Here, the product ID is saved in a simple JSON object, with the initial timestamp for each entry being the point at which the product first appears in a video stream.
This subtitles-as-metadata implementation also means that the files can be handled using standard video processing and streaming software. The following command shows how FFmpeg is used to combine a metadata file and a video into a single MKV container. While MP4 is the format mentioned here, this same command is compatible with a number of video formats.
The file output by this command contains everything required to run a metadata-enriched Amazon IVS stream using a pre-recorded video. While FFmpeg is used for recorded videos here, Open Broadcast Software (OBS) can also be used for recording and live streaming events to Amazon IVS.
Video Encoding for Efficiency
Encoding video can be CPU intensive, so in a scenario where pre-recorded video is being repeatedly streamed on a loop, it is beneficial to pre-encode the video as a one-off action. This reduces the CPU requirements of the streaming infrastructure since the video can be directly copied to a stream without the need for additional processing. It also allows for any additional manipulation that may be required to optimize the video for a specific presentational use case. A number of encoding parameters are added to the preceding FFmpeg command to achieve this, as shown:
By pre-encoding the video like this, it is possible to stream the videos to Amazon IVS without any real-time encoding, meaning that a number of video streams can be streamed using a container with minimal CPU, thus keeping compute costs to a minimum.
Manually Streaming Video to Amazon IVS
To test the compatibility of the video content with Amazon IVS, videos are manually streamed to the service using the following FFmpeg command:
The Amazon IVS console is used to view the stream and verify that the video is appearing in the stream as anticipated.
Vlogger streaming product demo within AWS Retail Demo Store.
Video files are streamed to Amazon IVS from a new service, which is added to the existing cluster in the AWS Retail Demo Store. To allow for full integration with a UI, the service provides endpoints returning the stream ingest URLs and the full list of products contained within each stream. The code is wrapped as a Flask API written in Python 3.8.
Upon service startup, the video to Amazon IVS channel mapping is retrieved from SSM and in separate threads each video is streamed to its corresponding Amazon IVS channel on a repeating loop. As this happens, the SRT data in the file is read and processed, with the metadata contained being sent to Amazon IVS using a PutMetadata operation at the appropriate time. The following code snippet gives a sample of how this is done in Python.
With the pre-encoding of the videos, the deployed demo is able to stream five videos simultaneously on a container with 0.25vCPU and 0.5GB of memory.
A “stream_details” endpoint is also implemented, which returns the ingest URLs of each stream, in addition to a list of product IDs for the products shown in each stream, allowing a UI to prefetch any data relating to products contained within the stream, if desired.
Ecommerce UI and Metadata-driven Personalization
Displaying the m3u8 stream containing metadata involves an out-of-the box implementation of Amazon’s IVS player, which can be added to a webpage similarly to any other video. A listener for the player’s TEXT_METADATA_CUE event then allows handing of metadata to be built using a callback function.
For the Retail Demo Store, the IVS streams are presented in a format combining vlog-style videos with UI components highlighting the products being presented in the stream at a given point. As the currently displayed product changes, a selection of related products and a personalized discount (determined using Amazon Personalize) are also shown.
Amazon Personalize for Personalized Discounts
We are leveraging Amazon Personalize to give us the discount that would maximize user uptake. Amazon Personalize gives us personalized re-ranking of available products looking at the user’s and collective historical uptake of discounted products. Then, we would offer the top-ranked products with a personalized discount.
Using Python to Train an Amazon Personalize Solution
For a full tutorial on how to set up Amazon Personalize with your dataset, see the official documentation.
Here, we use the “aws-personalized-ranking” recipe to train a reranking solution, and we tell Amazon Personalize to train against the “DiscountedProductViewed” event type (which is unique to our dataset—your dataset would likely have a different set of event types), to ensure that the solution predicts discounted product view events. An alternative approach would be to train against conversion events (such as “DiscountedProductPurchased”).
Having configured the solution, we can train it and deploy a “campaign” that gives us re-ranking at runtime. We wrap the campaign in an endpoint that contains logic for managing A/B testing, multiplexing recommender systems, and so forth. That endpoint uses the re-ranking to determine which products to offer a discount on. Roughly like this:
Using Recommendation Filters in Amazon Personalize, products that were previously purchased can be automatically removed. Additional custom logic can also be written to filter out recommendations pertaining to specific business KPIs.
The combination of Amazon IVS and Amazon Personalize gives endless opportunities for development in the retail space. By using these hosted, scalable services, you are able to focus on your differentiating business applications. All code referenced in this demo is integrated into the Retail Demo Store GitHub repository, which is available here and can be deployed to your own AWS account for exploration and experimentation in a matter of minutes.
Kyle Redelinghuys is Head of Engineering at Dae.mn, a London based consultancy which helps technology work better for their customers. He has a technical background, and currently helps drive the engineering function through the development and delivery of exciting products and services. Dae.mn have deep experience with product development using AWS services, and hold the AWS Retail Competency.