AWS for M&E Blog
Automatically stop AWS Elemental MediaLive channels when no input is detected
Introduction
Media organizations and companies around the world use AWS Elemental MediaLive to deliver fast, reliable, and easy-to-use high-quality live video streams without the need to manage infrastructure. MediaLive streamlines live video operations by automating the configuration and management of ingest and encoding components for processing and delivery of live streams. As of today, there is no automated way to stop MediaLive channels when they are idle and not streaming any output. When live streaming stops, the MediaLive channel keeps running, which incurs costs. If customers want to stop the channel when there is no input, they have to do so manually.
In this post, we describe how to create a fully automated solution to stop MediaLive channels and how customers can save on costs when MediaLive is not streaming to any output.
Note that some MediaLive resources do incur small charges even while idle – see MediaLive pricing for more details.
Assumptions
This blog post has the following assumptions:
1. You have a MediaLive channel, configured using single pipeline with an RTMP push input.
2. No redundant input is attached to the channel.
3. Your MediaLive channel is not configured with a file input.
4. Loss of input timer for MediaLive channel is 10 minutes, a user-defined period. We will monitor MediaLive channel alerts for 10 minutes and then will stop the channel.
5. We do not delete the input attached to the MediaLive channel as it can be re-used.
Architecture
In this blog, we are using OBS Studio as our live streaming software. OBS is a free and open-source solution for offline video recording and live streaming that is Mac, Windows, and Linux compatible.
MediaLive ingests a live feed, encodes it in real time, and compresses it into high-quality streams for broadcast. There are 2 channel class options in a MediaLive channel: standard and single pipeline. We use a single pipeline channel to configure our video source input. A channel contains the details that instruct MediaLive how to transcode (decode and encode) and package inputs into specific outputs. MediaLive generates alerts for a channel when an issue or potential issue occurs in either pipeline in a channel. Details about each alert are displayed on the Alerts tab in the MediaLive console.
Amazon EventBridge is a service that provides real-time access to changes in data in AWS services to your own applications, and software as a service (SaaS) applications, without writing code. We will create two rules in EventBridge to monitor the alerts in the MediaLive channel. The first rule will match the incoming event pattern where the alert state is “SET” and alert type is “RTMP Has No Audio/Video” and will send these alerts to our target AWS Step Functions for further processing. The second rule will match the incoming event pattern where the alert type is “RTMP Has No Audio/Video” and sends them to our target Amazon CloudWatch. CloudWatch is a monitoring service for AWS cloud resources and the applications you run on AWS. You can use CloudWatch to collect and track metrics, collect and monitor log files, and set alarms.
Step Functions is a fully managed service that makes it easier to coordinate the components of distributed applications and microservices using visual workflows. We create a workflow of two AWS Lambda functions and a wait state in our Step Functions.
AWS Lambda lets you run code without provisioning or managing servers. With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. The first Lambda function will send a custom Amazon SNS notification email to the user. Amazon SNS is a web service that makes it easy to set up, operate, and send notifications. The Step Functions workflow will now enter a wait state for 10 minutes before invoking the second Lambda function. This is based on our assumption where the user-defined loss of input wait period for MediaLive channel is 10 minutes. The second Lambda function will filter the CloudWatch logs to see if the MediaLive alerts have been cleared, and if not, then it will stop the MediaLive channel.
Prerequisites
You need an AWS account that has permissions to access the following services:
- AWS Elemental MediaLive
- AWS Elemental MediaPackage
- Amazon CloudFront
- AWS Lambda
- Amazon CloudWatch
- AWS Identity and Access Management (AWS IAM)
- AWS Step Functions
- Amazon SNS
- Amazon EventBridge
- AWS CloudFormation
This blog post requires you to set up a live streaming channel using AWS Media Services. Please follow this article to create an end-to-end live streaming channel, using fully managed AWS Media Services.
Step 1: Deploy CloudFormation template
Sign into the AWS Console
Click the below ”Launch Stack” button to deploy the CloudFormation template in the region of your choice.
Enter your Email Address, this will be used to send SNS notification.
Copy the MediaLive channel ARN you created in the prerequisites step and paste it in the MediaLive Arn text box.
Now check the acknowledge box and click Create.
The CloudFormation template will deploy below services in your AWS account
Walkthrough of the workflow
To start, your MediaLive channel should be running, with your OBS source streaming content to it.
- Stop streaming from OBS, which will raise alerts in the MediaLive channel.
- EventBridge rule monitors the alerts, matches the incoming event pattern, and sends the event or the payload to the targets, which are Step Functions and CloudWatch log group.
- Step Functions workflow consists of two Lambda functions and a wait state.
- Workflow invokes the first Lambda function which sends SNS notification to the user.
- Workflow then enters the wait state for 10 minutes, a user-defined waiting period.
- After the wait state, second Lambda function is invoked which filters the CloudWatch log group to check if the alerts raised by the MediaLive channel have been cleared. If not, then it stops the MediaLive channel.
Cost disclaimer
The AWS resources needed to build this workflow are not covered by the Free Tier, so you will incur additional cost while running. You are responsible for the cost of the AWS services used while running this workflow. Remember to clean up your resources once finished to avoid charges due to long-running resources.
Clean up
To avoid incurring future charges, delete the resources that have been created by following this blog post including AWS Lambda, Amazon SNS, Amazon EventBridge (CloudWatch events), MediaLive channel, MediaPackage, and CloudFront distribution.
Conclusion
Having an automated workflow to stop the MediaLive channel lessens the burden of manually monitoring the channel. With an automated workflow, you can identify the alerts and resolve errors much faster than before. The advantage of reducing the time to analyze and stop the channel is that the business can save costs on channels that are not actively streaming.
Engineering teams can free themselves from repetitive tasks of manually monitoring the channels and stopping them. You can further extend it by analyzing the redundant input attached to the channel or for standard pipeline channel or if channel has file inputs.
AWS provides a number of services designed to assist in building media-related workflows. If you would like to explore additional applications for video streaming, processing, and delivery visit AWS Media Services.