Server-less CDN router with AWS Lambda and Cedexis Openmix API

Eyevinn Technology
4 min readNov 26, 2017

For high available live OTT delivery it is common to use a multi CDN approach, and in this blog post Jonas Rydholm Birmé describes a prototype for how the CDN selection can be made using an AWS Lambda function and Cedexis Openmix API as decision maker.

To begin with I need to point out that even though I am describing this based on AWS I believe the same could be accomplished on for example GCP or Azure. With that said…

When a video player is streaming live content it first downloads a manifest file that contains the references to the video segments. This solution assumes that the video segments in the manifest are specified relative to the location of the manifest file. For live content the video player requests a new manifest on a regular interval as it is constantly updated with new segments on the server.

The principle here, as described in the drawing above, is that the video player is appointed to fetch the manifest from the CDN Router (implemented as an AWS Lambda function). Once the player parsed the manifest and requests video segments (e.g. DASH segments), the player will request the video segments from the CDN Router as they are relative to the location of the manifest. Instead of returning the actual video segment the CDN Router will respond with an HTTP 301 redirect to the actual location of the video segment. The actual location of the video segment depends on which CDN that was selected.

The decision on which CDN to choose is based on Cedexis Openmix API. With Cedexis you can configure the parameters to base this decision on. It can be the current RTT latency for a given CDN (data collected by Cedexis probes), cost, ISP and much more. Any custom logic that are not covered by Openmix can of course be added in the Lambda function.

Let us dig a little bit deeper into this solution and look at how we can make this scale. As every single request from ALL video players will hit the CDN Router this needs to be taken into account. To handle this I used an API gateway (basic requirement for AWS Lambda) with a CloudFront distribution configured for edge delivery on top.

exports.handler = (event, context, callback) {
const protocol = event.headers['CloudFront-Forwarded-Proto'];
const clientIPs = event.headers['X-Forwarded-For'].split(',');
const re = new RegExp('^/dash/(.*)$');
const m = event.path.match(re);
let url;
if (m) {
url = m[1];
makeDecision(clientIPs[0]).then(decision => {
if (isManifest(url)) {
return requestManifestFromCDN(protocol + "://" + decision.host + "/" + url);
} else {
return redirectSegmentToCDN(protocol + "://" + decision.host + "/" + url);
}
}).then(response => {
callback(null, response);
}).catch(err => {
callback(null, errorResponse(500, err));
});
} else {
callback(null, errorResponse(404, "Invalid URL"));
}
}

The Lambda function is quite simple and the above Javascript code shows the gist of it. The function issues an HTTP request to the Cedexis Openmix API (makeDecision) and depending on whether it is a manifest or a video segment requested, it either fetches the manifest from the selected CDN (requestManifestFromCDN) or redirects the client to the selected CDN (redirectSegmentToCDN). In this case we provide the IP to the decision maker which makes it possible for example to base the decision on the clients Internet Service Provider. As every video segment request is passed through this CDN Router we have the possibility to do in-stream switching between the available CDNs.

As a consequence of this setup that we need a decision on every video segment request we also need to shield the Cedexis Openmix API. In this setup I have just put a CloudFront in front of the API as the API responds with a TTL on each response set.

It is yet to prove whether this AWS setup can really scale and we would also need a fallback scenario in case the CDN Router is failing and not responsive.

If you have any comments or question please leave a comment in the section below or find me in the Streaming Tech Sweden slack (http://slack.streamingtech.se). Thanks for reading!

Jonas Rydholm Birmé is a Solution Architect at Eyevinn Technoloy. A Swedish based independent consultancy company specialized in the video and streaming technology.

--

--

Eyevinn Technology

We are consultants sharing the passion for the technology for a media consumer of the future.