Skip to content

Kinesis Video Stream Web RTC with Lambda

0

My Goal:

  • Stream Video to a lambda from Kinesis Web RTC, I want to connect as a viewer and consume a master stream in my lambda.
  • Connect and consume the master stream for about 2 mins or so in my lambda and process the images that I am interested in within the lambda.
  • I wanted to use kinesis WEBRTC because I want the lowest possible latency and I wanted to use lambda because of its simple and easy scaling. So each channel can be a single lambda that I launch.

Problem I encountered: I was able to connect get the signal channel from kinesis videos streams, I was able to get the ice config and prepare the servers from kinesis video streams web rtc. But it seems to hang when trying to establish the websocket connection in a lambda there is an issue? There is no error just hanging and a timeout. The lambda is never able to connect as a viewer.** Is this possible with my current setup?**

    def get_signaling_channel_endpoint(self):
            endpoints = self.kinesisvideo.get_signaling_channel_endpoint(
                ChannelARN=self.channel_arn,
                SingleMasterChannelEndpointConfiguration={'Protocols': ['HTTPS', 'WSS'], 'Role': 'VIEWER'}
            )
            self.endpoints = {
                'HTTPS': next(o['ResourceEndpoint'] for o in endpoints['ResourceEndpointList'] if o['Protocol'] == 'HTTPS'),
                'WSS': next(o['ResourceEndpoint'] for o in endpoints['ResourceEndpointList'] if o['Protocol'] == 'WSS')
            }
            self.endpoint_https = self.endpoints['HTTPS']
            self.endpoint_wss = self.endpoints['WSS']
        return self.endpoints

# ... omit lots of code ...

        # prepare request for WSS
        aws_request = AWSRequest(
            method='GET',
            url=self.endpoint_wss,
            params={'X-Amz-ChannelARN': self.channel_arn, 'X-Amz-ClientId': self.client_id},
        )
        SigV4.add_auth(aws_request)
        wss_url = PreparedRequest = aws_request.prepare().url

# ... omit lots of code...

# Try connecting ...
websockets.connect(wss_url) as websocket: # PROBLEM: hangs here when trying to connect it appears

Detailed Explanation of Issue: This wss_url is the websocket connection for the kinesis video webrtc signaling channel endpoint and uses SigV4Auth for the request. It works fine when testing locally on my computer and in cloudshell in a private vpc subnet and connecting. However it does not work in lambda it simply seems to hang. I have debugged and try to find if there is some issue but no luck it just times out. I was curious if there is some limitation that I cannot find online for lambda environment and other advice that might be offered. I have verified the security group has egress access to 0.0.0.0/0 and iam permissions are kinesisvideo:* for debugging purposes and no luck so far.

Things I have tried:

  • Moving lambda to public subnet (its in private subnet)
  • Connecting to WSS signed url in cloudshell and confirm that wss url is reachable
  • connect to WSS url locally on my computer to ensure wss url is reachable
  • Ensuring permissions are kinesisvideo:* for lambda to ensure there is not some permissions issue

Ideal Desired Solution Ideally I would like to work with lambda if possible, but may be forced to consider alternative route. My lambda is a container image based on the public.ecr.aws/lambda/python:3.12 image.

General Code example followed adjusted for lambda for my case: https://github.com/aws-samples/python-samples-for-amazon-kinesis-video-streams-with-webrtc/blob/main/source/simple/kvsWebRTCClientViewer.py

1 Answer
-1

Based on your description, it appears you're encountering challenges when trying to establish a WebSocket connection from an AWS Lambda function to a Kinesis Video Streams WebRTC signaling channel. While it's technically possible to consume a Kinesis Video Stream in real-time over WebSocket, there are some limitations and considerations when attempting this within a Lambda environment.

  1. Lambda Execution Environment: Lambda functions have a limited execution time (up to 15 minutes maximum) and are designed for short-lived, stateless operations. Maintaining a persistent WebSocket connection for streaming video may not be ideal in this environment.

  2. Network Constraints: Lambda functions run in a managed environment, and while they can make outbound network connections, there might be restrictions or timeouts that affect long-lived connections like WebSockets.

  3. Resource Limitations: Processing video streams can be resource-intensive, and Lambda's memory and CPU allocations might not be sufficient for continuous video processing.

  4. WebRTC Compatibility: WebRTC is primarily designed for web browsers and may have dependencies that are not readily available in the Lambda execution environment.

Given these challenges, you might want to consider alternative approaches:

  1. Use Amazon EC2 or ECS: For longer-running processes that require persistent connections, using EC2 instances or ECS tasks might be more suitable.

  2. Kinesis Video Streams with GetMedia API: Instead of WebRTC, you could use the GetMedia API to retrieve video data in chunks, which might be more compatible with Lambda's execution model.

  3. Step Functions: If you need to process video in segments, you could use AWS Step Functions to orchestrate multiple Lambda functions, each processing a portion of the video.

  4. Amazon Connect Integration: If you're specifically working with Amazon Connect call streams, there might be more direct integration options available that don't require WebRTC.

If you still want to pursue the Lambda approach, you may need to:

  1. Increase the Lambda timeout to its maximum (15 minutes).
  2. Ensure all necessary dependencies for WebRTC are included in your Lambda container image.
  3. Implement robust error handling and retry logic for connection attempts.
  4. Consider breaking down the video processing into smaller chunks that fit better within Lambda's execution model.

Remember that while Lambda offers great scalability, it may not be the optimal choice for all types of workloads, especially those involving long-lived connections or continuous stream processing.
Sources
Kinesis Video Stream over Websocket | AWS re:Post

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.