Ad insertion on VOD and Live Media Content using AWS MediaTailor and CloudFront

Hetul Sheth
ScaleCapacity
Published in
5 min readAug 9, 2020

In today’s industry monetizing the video assets is gaining popularity and so we need an easy method to monetize the content. We are going to use the MediaTailor service of AWS for our purpose.

Our Architecture

Prerequisites:

  1. Here is to have your HLS media asset with you stored in your S3 bucket. If it does not have an AD marker than it would not be an issue. Refer this for creating your own HLS Media Asset using MediaConvert from your mp4 video file.
  2. An Ad Decision Server (ADS), or you may use a static VAST response XML hosted on a server. Here, we will use a sample VMAP response hosted and publicly made available by Google’s DoubleClick for Publishers and can be found.

So let’s start:

  1. From the AWS Management Console, choose Services then select AWS Elemental MediaTailor.
  2. Click on Create configuration.
  3. Enter MyTestCampaign for the Configuration Name. You can give it any other name also according to the convenience.
  4. For the Video content source, enter the S3 URL link to the asset Endpoint URL https://s3-us-west-2.amazonaws.com/vast-demo-bucket/caminandes/master.m3u8 but without the manifest filename. That is, omit master.m3u8.
  5. Enter https://pubads.g.doubleclick.net/gampad/ads?sz=640x480&iu=/124319096/external/ad_rule_samples&ciu_szs=300x250&ad_rule=1&impl=s&gdfp_req=1&env=vp&output=vmap&unviewed_position_start=1&cust_params=deployment%3Ddevsite%26sample_ar%3Dpremidpost&cmsid=496&vid=short_onecue&correlator=[avail.random] for the Ad decision server.

6. Click Create Configuration. Click on the Configurations link to see the configuration you just created. Click on MyTestCampaign to see the Playback endpoints populated with playback URLs. Note down the HLS playback prefix as you’ll need it in the next section.

Now test your MediaTailor Playback:

  1. To verify that ads are making it into your video, you may use a standalone video player to view the HLS playback endpoint such as QuickTime, VLC, or any workstation-based player that supports HLS. Alternatively, you may use one of the following web-based players to stream your video:

2. Your full playback URL will be the HLS playback prefix (eg. https://26ba774678dd400b9ba547a4ba49881a.mediatailor.us-east-1.amazonaws.com/v1/master/77f1053bfb725e4f0b572f28e98c75ea84707be4/MyTestCampaign/) concatenated with the manifest filename of the asset in MediaStore (eg. master.m3u8)

Provide the full playback URL to the player of your choice (eg. https://26ba774678dd400b9ba547a4ba49881a.mediatailor.us-east-1.amazonaws.com/v1/master/77f1053bfb725e4f0b572f28e98c75ea84707be4/MyTestCampaign/endgame.m3u8)

With the VMAP response used here, you should see a 10-second preroll ad right at the beginning:

Integrate with Amazon CloudFront

Add Origins to Your Distribution

  1. From the AWS Management Console, choose Services then select CloudFront.
  2. Click on Create Distribution.
  3. Select the Web delivery method for your content and hit the Get Started button.
  4. Under Origin Settings, enter the domain name of your origin for Origin Domain Name. In our case, this is the domain name of our S3 bucket (eg. vast-demo-bucket.s3.amazonaws.com).
  5. Under Origin Protocol Policy, select HTTPS only.
  6. Take the default for all the other settings. But scroll down until you see the Comment textbox and enter CloudFront for MediaTailor.
  7. Click the Create Distribution button. This will take you back to the main Distribution page of cloudfront. Your distribution will be in an In Progress state. The quickest way to tell which of the Distributions is yours is by the comment you entered.
  8. Go back to the Origins and Origin Group tab, and click on the Create Origin button.
  9. Enter MediaTailor’s hostname for the Origin Domain Name. This will come from the HLS playback prefix of MediaTailor (e.g. f445cfa805184f3e8d86dc2ac1137efa.mediatailor.us-west-2.amazonaws.com)
  10. Update the Origin Protocol Policy to HTTPS Only. Click Create.
  11. Go back to the Origins tab, and click on the Create Origin button.
  12. Enter MediaTailor’s ad server hostname for the Origin Domain Name. If MediaTailor is being set up in us-west-2 then the Origin is: ads.mediatailor.us-west-2.amazonaws.com
  13. Update the Origin Protocol Policy to HTTPS Only. Click on Create.

3b. Add Cache Behaviors to Your Distribution

  1. Click on the Behaviors tab and click on the Create Behavior button.
  2. Enter /caminandes/* for the Path Pattern.
  3. Under Origin, select the S3 origin.
  4. Click the Create button to add another cache behavior.
  5. Click on Create Behavior button.
  6. Enter /v1/* for the Path Pattern.
  7. Under Origin, select the MediaTailor origin.
  8. For Query String Forwarding and Caching, select Forward all, cache based on all.
  9. Click the Create button to add another cache behavior.
  10. Select the Default behavior, and click on the Edit button.
  11. Make sure the Origin is pointed to the Ad Server origin (e.g. ads.mediatailor.us-west-2.amazonaws.com)
  12. For Query String Forwarding and Caching, select Forward all, cache based on all.
  13. Click on Yes, Edit button.
  14. Double-check the precedence of the caching behavior as this matters. Your primary precedence should be the MediaTailor origin (precedence 0), followed by the S3 origin (precedence 1), and lastly by the ads origin (which is at Precedence 2 and is the Default). If this is not the precedence reflected, select one of the Behaviors and Change Precedence by clicking on either the Move Up or Move Down button, to make the adjustment.

Update MediaTailor Configuration with CloudFront Details

  1. From the AWS Management Console, choose Services then select AWS Elemental MediaTailor.
  2. Click on the Configuration (MyTestCampaign) you created in section 1 and hit the Edit button.
  3. For the CDN content segment prefix, construct your URL by putting together the protocol, the CloudFront Domain Name, and the path of the MediaStore origin. For example: https://da4bw8c4fh0km.cloudfront.net/caminandes
  4. For the CDN ad segment prefix, construct your URL by putting together the protocol, and the CloudFront Domain Name. For example: https://da4bw8c4fh0km.cloudfront.net

Test MediaTailor Playback with CloudFront

  1. Once your CloudFront distribution is in the Deployed status, and Enabled state, try playing back your stream using the same player you selected in section 2 of this lab. Take your playback URL from Section 2, Step 2 and replace the MediaTailor hostname with the CloudFront Domain Name that was assigned to your distribution.
  2. For example, if your MediaTailor playback URL is:
  3. https://26ba774678dd400b9ba547a4ba49881a.mediatailor.us-east-1.amazonaws.com/v1/master/77f1053bfb725e4f0b572f28e98c75ea84707be4/MyTestCampaign/endgame.m3u8
  4. then your CloudFront playback URL is: https://dy9vusm71q2r9.cloufront.net/v1/master/77f1053bfb725e4f0b572f28e98c75ea84707be4/MyTestCampaign/endgame.m3u8

Congratulations! You’ve successfully integrated your HLS video asset with AWS Elemental MediaTailor.

--

--

Hetul Sheth
ScaleCapacity

AWS Certified Solutions Architect, Developer and SysOps Admin Associate | Azure Certified