This AWS Lambda function demonstrates how to find the total number of KVS stream resources in any AWS account using the ListStreams API. This helps in monitoring the total streams usage to request limit increases as needed.
The Lambda function performs the following tasks:
- Connects to the Kinesis Video Streams service in the specified AWS region.
- Retrieves the list of Kinesis Video streams using the
ListStreamsAPI. - Iterates through the paginated responses to count the total number of streams.
- Provides retry logic for transient errors such as throttling or service limits.
- Returns the total count of Kinesis Video streams.
Assumption: The total number of streams should be retrievable by a single Lambda execution.
To deploy and use this Lambda function using the AWS Management Console:
- Navigate to the Lambda functions page.
- Choose Create function.
- Select Author from scratch.
- In the Basic information pane, for Function name enter FetchStreamCount.
- For Runtime, choose Python 3.10.
- Leave architecture set to x86_64.
- Choose an IAM role for this function. It must have
kinesisvideo:ListStreamspermission. Create one if necessary. - Choose Create function.
- Switch to the Configuration tab and increase the Timeout. Adjust the timeout based on your maximum stream count quota.
- Switch back to the Code tab and paste the contents of
list_streams_lambda.pyinto thelambda_function.pyfile. - Choose Deploy.
- Choose Test.
- Leave the configurations unchanged. Choose Invoke.
- The stream count will be fetched and the run logs will be shown.
This stream counter Lambda helps to check the total number of streams periodically.
- Set up event triggers (e.g. using Amazon EventBridge) to periodically invoke this function to retrieve the total count of Kinesis Video streams. You can then send the stream count metrics to CloudWatch and set up metric alarms for monitoring.
This Lambda function relies on the following dependencies:
boto3: AWS SDK for Python.botocore: Library for handling exceptions and low-level API interactions in Boto3.
In the Lambda environment, these are already present.
If you are using the list_streams.py on your local environment, follow the boto3 installation instructions:
pip install boto3The function handles various error scenarios gracefully:
- Client Errors: Logs error details and retries for certain exceptions specified in
retryable_exceptions. - Non-retryable Errors: Raises the exception for non-retryable errors, such as a missing permission error (AccessDeniedException).
- Ensure that the Lambda function's execution role has sufficient permissions to access Kinesis Video Streams (specifically, the
kinesisvideo:ListStreamspermission). - The limit for the ListStreams API can be found on the Kinesis Video Streams Limits page.
This Lambda function is released under the Apache 2.0 License.
