On-Demand Video Streaming! AWS Elemental MediaConvert and AWS Lambda
Tags: aws, awselementalmediaconvert, awslambda, security
Published: 2021-07-18 17:26:32

“Challenges faced to find the solution of how to convert the video file format from mp4 to hls or mp4 in compressed file size within some seconds”. I have checked different ways so that I can get my video file format conversion to the same or different format with compressed file size within a few seconds and within my budget. I got the solution to use an amazon elastic transcoder service or aws elemental mediaconvert. I have chosen aws elemental mediaconvert, as it is cheaper in cost as compared to transcoder and includes additional features with high security[DRM]. In aws elemental mediaconvert, i am able to stream the video file on On-demand basis. Also the job can be automatically able to create whenever there is a video file in source bucket and s3 trigger on lambda function which transcodes the file and stores it in s3 destination bucket as per output group settings. Also can be able to get notified for complete and error status of job. In terms of cost, it is a very cheaper one for transcoding a file in other formats.

AWS Elemental MediaConvert is a file-based video transcoding service with broadcast-grade features. It allows you to easily create video-on-demand (VOD) content for broadcast and multiscreen delivery at scale. The service combines advanced video and audio capabilities with a simple web services interface and pay-as-you-go pricing. With AWS Elemental MediaConvert, you can focus on delivering compelling media experiences without having to worry about the complexity of building and operating your own video processing infrastructure. To learn more, read the [AWS Elemental MediaConvert](https://docs.aws.amazon.com/mediaconvert/latest/ug/what-is.html). 

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers, creating workload-aware cluster scaling logic, maintaining event integrations, or managing runtimes. With Lambda, you can run code for virtually any type of application or backend service - all with zero administration. Just upload your code as a ZIP file or container image, and Lambda automatically and precisely allocates compute execution power and runs your code based on the incoming request or event, for any scale of traffic. You can set up your code to automatically trigger from 140 AWS services or call it directly from any web or mobile app. You can write Lambda functions in your favorite language (Node.js, Python, Go, Java, and more) and use both serverless and container tools, such as AWS SAM or Docker CLI, to build, test, and deploy your functions. To learn more, read the [AWS Lambda](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html). 

In this post, you will get to know how to convert any on-demand video file to HLS or MP4 format using MediaConvert and Lambda. Also get notified via email regarding job complete or error status. Here I have taken two S3 buckets and then created a lambda function for convert of formats with IAM role and set a S3 trigger on it which will automatically generate the output video files of same or different formats in the s3 destination bucket. Setup of cloudwatch event on mediaconvert for complete and error job status with notification service.

#Prerequisites
You’ll need an Amazon Simple Storage Service for this post. [Getting started with amazon Simple Storage Service](https://aws.amazon.com/s3/getting-started/) provides instructions on how to create a bucket in simple storage service. For this blog, I assume that I have two buckets for input and output of videos.

#Architecture Overview    
##Diagram                                                                                                            
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h6cub75igiay8tzxaaew.png)
The architectural diagrams show the overall deployment architecture with data flow, AWS S3, AWS Elemental MediaConvert, AWS Lambda, Cloudwatch event and Amazon SNS.

#Solution overview
The blog post consists of the following phases:
1. Create IAM role and folder in source bucket 
2. Create Lambda function with S3 trigger
3. Get notification via email for Job status using Simple Notification Service and Cloudwatch Event
4. Testing on-demand video file streaming
5. Check of cloudwatch log groups and metrics for media convert

I have two S3 bucket for input and output of video as below →
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/llbtgkp5facdg9u1vnq6.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s1sbmkxy2dkotikoizik.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gaoar6ouj8zmq6j8dwxn.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sg91hdvuphmrkc7b9y9h.png)

##Phase 1: Create IAM role and folder in source bucket 
1. In AWS IAM console, create a role named as MediaConvertRole with policy as s3 full access and api gateway invoke full access.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbjj7ctcjisrtdmoch83.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gz2rth3detl4z7inx9v.png)
2. Create a role named as VODLambdaRole with policy as lambda basic execution role and inline policy as VODLambdaPolicy for mediaconvert.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zdfzxsf8byh5jtj35t9p.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t1q1xngz6luzolygvct.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sfqkb053ob3b7viyqyij.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ddc3mwlbsofubbcvb5ci.png)
3. Create a folder in the source bucket named as inputs for incoming video files as on demand basis.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uc19crhk1n54cplb1f70.png)

##Phase 2: Create Lambda function with S3 trigger
1. In AWS Lambda Console, create a function named as VODLambdaConvert with python3.8 and VODLambdaRole. Set runtime setting of handler as convert.handler.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/uvoe9j7cdo0ma6cy4lkd.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d0gb5hbennrodwrb9frl.png)
	
2. Import zip file in function which contains convert.py and job.json. Convert.py consist of code for convert of video format and job.json consist of output group setting format.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6hl2avtr8yhjzivpl4zu.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tcm9p2go2eqe3gwm1d0n.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqpxqe5jgyka708owovm.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92ha4b6453fghs9aneld.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/stduemrulowjoxgtb0y7.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jzy844nnlqqsmg0emd4v.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ue21lpan2997w3b4ihad.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hiaw7y2qb5u0m9rhp5w.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ut0uajvrmq4saqufsc5m.png)

3. In the configuration tab, set the timeout as 2min 0sec, environment variable as shown in screenshot and monitoring enabled.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zcg6na4myvomvef5776p.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0y46xe8v49mo9z2d7fp7.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c1ftocigvrm8ou31c39i.png)

4. Set S3 trigger on lambda function as choose source bucket, event type as object created and prefix as inputs/.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9xuwh35dkykx958dd3mh.png)

##Phase 3: Get notification via email for Job status using Simple Notification Service and Cloudwatch Event
1. In the Amazon Simple Notification Service console, create a topic named as VODNotification with default settings. And create a subscription for email notification basis and do subscription confirmation in subscribed email id. Update the access policy of SNS with topic arn as shown in screenshot.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2njwtoql334x86r2pwnc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/j4a2u0808hrkduqzqg45.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8etuvq4firt6vlu8y71.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p2heusyvd41x1sij9oqc.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hshd7g8xsyr5hylfcr9x.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u5bqlahxmgyvil8rus79.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n16xihqnjzdxkguuomt4.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5o0fjhex5gq5dvaywfke.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tqwgrpfov4e266zu4cc2.png)

2. Goto Cloudwatch console, Create a rule named as VODNotifyEventRule with complete and error policy. And set of target as SNS topic and input transformer pattern.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4t5u6kjot6g66qvri8fw.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ss3ttqsw4pzwj5e2o5we.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5kqjma8bje2fmt944n6.png)

##Phase 4: Testing on-demand video file streaming
1. Input of video file in source bucket named as vod-inputfolder-bucket. The S3 trigger on the lambda function starts the job process in media convert and the event gets notification on subscribed email once the running job gets finished with complete or error status. 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tu0h7b8y8hvrbpxmybwu.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sug3o388rm43seyfxcd9.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4ev0xmtwn7c7muue0p49.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wxflnhitzcrpohs3xctt.png)

2. Check the job detailed summary in screenshot such as duration of job, start and completion of job with input and output details.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ultuvzgftcb2ur6j9bvb.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z8iramvw5qcgbct4dbsb.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4e5ebtul8cvuaimaviqj.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/437mgfw1e04fnqfo8055.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9ym8prkmvlo03amm08ur.png)

3. Checkout the destination bucket named as vod-outputmediafolder-bucket with convert formats of input video file in hls, mp4 and thumbnails. Also check the streaming of video in VLC Player.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/67ukdvjaprt9btalk2hx.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jar67h3u5yj1rm0ftm0.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3f56nb82szqlwqk26fs6.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/v6xlsx74xuy9z34x2wxr.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bk5472zt5c60cv8cs1oo.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oqhsfyakmizh4go7d8nt.png)

##Phase 5: Check of cloudwatch log groups and metrics for mediaconvert
1. You can get the logs of jobs in the cloudwatch log group.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/44y7hdjejlbye65h85vt.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms0gr0d0we1mx5saw58d.png)

2. You can also check the metrics of mediaconvert in cloudwatch.
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qcy2ip8afqjqijmwrau5.png)
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2jsm18x5y04bigitr3fp.png)

#Clean-up
Delete the environment as: S3 buckets, IAM roles, Lambda function and cloudwatch log group. Note: Mediaconvert jobs created cannot be deleted.

#Pricing
I review the pricing and estimated cost of this example -
 
For AWS Elemental MediaConvert →
Cost for BOM-P-AVC-HD-MHQ-30 of 12.307 minutes = $0.52
Cost for BOM-P-AVC-SD-MHQ-30 of 36.090 minutes = $0.76
Cost for BOM-B-AVC-HD-S-30 of 383.913 minutes = $5.76
Cost for BOM-B-AVC-SD-S-30 of 383.913 minutes = $2.88
Cost of mediaconvert = $(0.52+0.76+5.76+2.88) = $9.92
For Lambda →
Cost of request and lambda-GB per second = $0.00
For Simple Storage Service →
Cost of APS3-Requests = $0.04
For cloudwatch, data transfer and simple notification (under free tier) →	 
Cost = $0.00 
Total Cost = $(9.92+0.00+0.04+0.00) = $9.96

#Summary
In this post, I have shown you how to convert any video file to HLS or MP4 format using MediaConvert and Lambda.

For more details on mediaconvert and lambda service, Checkout Get started with AWS Elemental MediaConvert and AWS Lambda, open the [AWS Elemental MediaConvert console](https://ap-south-1.console.aws.amazon.com/mediaconvert/home?region=ap-south-1#/welcome) and [AWS Lambda console](https://ap-south-1.console.aws.amazon.com/lambda/home?region=ap-south-1#/functions). To learn more, read the [AWS Elemental MediaConvert documentation](https://docs.aws.amazon.com/mediaconvert/?id=docs_gateway) and [AWS Lambda documentation](https://docs.aws.amazon.com/lambda/?id=docs_gateway).