Building a Live Video Streaming Workflow with AWS Elemental MediaLive and Terraform
Published: 2024-01-26 08:54:06

**Introduction:**

I am big fan of video streaming technologies, it always interests me whichever domain i work on.

I love to explore the video streaming tech resources and tools ,infact i have worked with one of the biggies in this industry ,still that sting has gotten me keep exploring it.

One of the major event in this is LIVE media delivery.

Lets explore it with AWS elemental:
 

In the modern digital landscape, live video streaming has become an integral part of many businesses and organizations, enabling real-time engagement with audiences across the globe. AWS Elemental MediaLive, a cloud-based live video processing service, offers powerful capabilities for ingesting, processing, and delivering live video streams at scale. In this blog post, we'll explore how to leverage Terraform, a popular infrastructure as code tool, to set up a live video streaming workflow using AWS Elemental MediaLive and integrate it with a content delivery network (CDN) for global distribution.

**Overview of the Solution**

Our goal is to set up a live video streaming workflow that ingests a live feed with an MPEG-DASH format (.m2ua manifest) using AWS Elemental MediaLive and delivers it to viewers via a CDN. Here's a high-level overview of the solution architecture:

**AWS Elemental MediaLive Channel:** We'll create an AWS Elemental MediaLive channel to ingest the live video feed, process it according to our requirements, and prepare it for distribution.

**CDN Integration:** We'll configure a CDN service such as Amazon CloudFront to cache and deliver the live video stream to viewers worldwide, ensuring low-latency and high-performance delivery.

**Terraform Deployment:** Leveraging Terraform, we'll define the infrastructure components required for our live video streaming workflow, including the AWS Elemental MediaLive channel, CDN distribution, and any supporting resources.

**Setting Up the Live Video Streaming Workflow**
Step 1: Define Terraform Configuration
We'll start by defining the Terraform configuration that describes our AWS infrastructure. This includes declaring resources for the AWS Elemental MediaLive channel, CDN distribution, and associated configurations.

Lets see the IAC :

main.tf 

```
provider "aws" {
  region = var.region
}

resource "aws_media_live_channel" "movietime_channel" {
  name = var.channel_name

  input {
    input_security_groups = [var.input_security_group]
    source_endpoints {
      url = var.input_source_url
    }
  }

  destinations {
    stream_name = var.output_stream_name
    hls_settings {
      hls_manifests {
        id                    = var.manifest_id
        manifest_name         = var.manifest_name
        include_i_frame_only_stream = false
        manifest_window_seconds = var.manifest_window_seconds
      }
    }
  }
}

resource "aws_cloudfront_distribution" "example_distribution" {
  origin {
    domain_name = aws_media_live_channel.example_channel.arn
    origin_id   = "MediaLiveChannel"
  }

  default_cache_behavior {
    // Configure caching behavior
  }

  // Other CloudFront distribution configurations
}

```
variables.tf

```
variable "region" {
  description = "AWS region"
  type        = string
}

variable "channel_name" {
  description = "Name of the MediaLive channel"
  type        = string
}

variable "input_security_group" {
  description = "ID of the input security group"
  type        = string
}

variable "input_source_url" {
  description = "URL of the input source"
  type        = string
}

variable "output_stream_name" {
  description = "Name of the output stream"
  type        = string
}

variable "manifest_id" {
  description = "ID of the HLS manifest"
  type        = string
}

variable "manifest_name" {
  description = "Name of the HLS manifest"
  type        = string
}

variable "manifest_window_seconds" {
  description = "Window duration for the HLS manifest"
  type        = number
}
```
prod.tfvars

```
region                     = "us-west-2"
channel_name               = "example-channel"
input_security_group       = "sg-0123456789abcdef0"
input_source_url           = "udp://239.0.0.1:1234"
output_stream_name         = "example-stream"
manifest_id                = "m2ua-manifest"
manifest_name              = "example-manifest"
manifest_window_seconds    = 60
```
In this setup:

main.tf contains the main Terraform configuration for creating AWS Elemental MediaLive channel and CloudFront distribution.
variables.tf defines the input variables required for the Terraform configuration.
prod.tfvars contains values for the "prod" environment. You can create similar files (e.g., dev.tfvars, stage.tfvars, etc.) for different environments.
To use this setup, initialize Terraform, specify the environment variables file (-var-file=prod.tfvars), and apply the configuration:

```
terraform init
terraform apply -var-file=prod.tfvars
```


**Step 2: Deploy Infrastructure**
With our Terraform configuration defined, we'll initialize Terraform and deploy the infrastructure to AWS. Terraform will create the AWS Elemental MediaLive channel, configure the CDN distribution, and set up any necessary networking components.

If you guys are using any CI/CD tool , you can configure pipelines for the stages and run it.

**Step 3: Monitor and Test**
After deployment, we'll monitor the status of our AWS Elemental MediaLive channel and CDN distribution to ensure they're functioning correctly. We'll also perform thorough testing to verify that the live video stream is ingested, processed, and delivered to viewers as expected.

**Conclusion:**
By leveraging AWS Elemental MediaLive and Terraform, we've successfully set up a robust live video streaming workflow capable of ingesting live video feeds, processing them according to our requirements, and delivering them to viewers worldwide via a CDN. This scalable and flexible solution empowers businesses and organizations to engage with their audiences in real-time, whether it's for live events, webinars, or online broadcasting.

Stay tuned for more insights and best practices on leveraging cloud technologies to enhance your video streaming capabilities.

As usual feedacks welcome and happy to collaborate for any tech discussions and a coffee.

Linked Connect : https://www.linkedin.com/in/balajisivamani/