Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using AWS Lambda for Multi-Location Media Transform

DZone 's Guide to

Using AWS Lambda for Multi-Location Media Transform

Take a look at how you can use Lambda@Edge to optimize you content for the greatest amount of viewer across multiple formats.

· Cloud Zone ·
Free Resource

With the rising number of global viewership, it is highly critical for media giants to serve their customer needs. With an increasing number of the viewers, the number of device formats has also increased. 

There could be a situation where you have a lot of media files that you need to change dimensions of or apply a watermark to, for example. To make such cases seamless, it is imperative to offer content in multiple formats at multiple locations.

The best example of an organization doing this is Netflix with its 70 billion hours of content in a quarter to nearly 60 million customers using serverless to facilitate their media files in more than 50 formats. 

We all know the benefits of using AWS Lambda. Lambda@Edge extends the capabilities of it and empowers you to execute your code in multiple location near the users. Let's discover how it works!

With high demand, comes higher responsibility. One such demand is extremely low latency. 

With the prerequisite of the least amount of processing time, reducing the latency and minimizing the bandwidth is important than ever. Here are some common scenarios which you might have come across:

  • Resizing image based on the query parameter;
  • Serving appropriate file format based on browser characteristics, for example, WebP for Chrome/Android browsers and JPEG for the rest;
  • Defining whitelist of dimensions to be generated.

This problem can be simplified with the help of Lambda@Edge and CloudFront. This process can be executed by adding four Lambda triggers to CloudFront. Here’s how it works:

Let's assume that the viewer is based out of China while your origin source is "us-east-1." The diagram shown below depicts how the media file is generated for viewers in far away location. 

 

  • Lambda 1 (viewer request): This function is executed to serve the media file in the requested format from the CloudFront cache. No further functions will be executed.
  • Lambda 2 (origin request): If the requested format is not available, this function fetches the media file with requested configurations from the Amazon S3 bucket and cache it to CloudFront. If the file doesn’t exist, Lambda 3 is executed.
  • Lambda 3 (origin response): This function makes a network call which fetches the original image from the S2 bucket, transforms it as per the requirement and uploads it back.
  • Lambda 4 (viewer response): This function serves the requested media file from the CloudFront cache.

Note: Lambdas 2, 3, and 4 are executed only when the requested media file isn’t available in the cache. Here’s more you can do with Labda@Edge:

  • Inspect cookies and rewrite URLs to perform A/B testing.
  • Send specific objects to your users based on the User-Agent header.
  • Implement access control by looking for specific headers before passing requests to the origin.
  • Add, drop, or modify headers to direct users to different cached objects.
  • Generate new HTTP responses.
  • Cleanly support legacy URLs.
  • Modify or condense headers or URLs to improve cache utilization.
  • Make HTTP requests to other Internet resources and use the results to customize responses.

Takeaway

When you use AWS Lambda and Lambda@Edge together, you've two distinct advantages: 

1. No requirement of manually processing and editing images for different types of devices and viewers. This is helpful when you quickly want to test out your design changes through A/B testing. 

2. Since you're using AWS Lambda, your architecture would be a lot more simplified. 

This was just one of the exciting thing which you can do with AWS Lambda and there are many more use cases you can try your hands on! I'd love to hear how have been your experience with AWS Lambda. Do you have any interesting insight to share, feel free to hit me up on Twitter @RohitAkiwatkar

Topics:
aws lambda ,serverless architecture ,serverless functions ,cloud computing ,cloud

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}