Streaming video from Mux requires a playback ID.
Mux Playback IDs have two types: public
and signed
.
public
playback URLs can be watched anywhere, any time.
signed
playback URLs require a Mux signing key
which is used to generate a token via JSON Web Tokens.
This example includes an AWS Lambda function that can receive a Mux playback ID and return a signed url that's ready for use.
# Generate signed playback URL
curl -X POST \
-H "Content-Type: application/json" \
-d '{"playbackId": "1234"}' \
https://example.com/my-example-lambda
# Response
{
"playbackId": "1234",
"token": "some-token",
"signedUrl": "https://stream.mux.com/1234.m3u8?token=some-token"
}
Generate a signed playback URL by making a POST request to your lambda.
Pass playbackId
and other configuration in the body of the request.
- Run
yarn build
to bundle all js code and create a zip file that can be uploaded to AWS Lambda. - Create a new empty function in AWS Lambda. Be sure to select Node.js for the runtime option.
- Add API Gateway as a trigger in the designer section.
- Change the code entry type option under the Function code section to "Upload a .zip file" and upload the zip created in the dist folder.
The handler reference will be
dist/lambda.handler
. - Generate a Mux access token and secret if you have not already. Set environment variables
MUX_ACCESS_TOKEN
andMUX_SECRET
accordingly. - Create a test event to confirm that your lambda works. For example:
{
"httpMethod": "POST",
"body": {
"playbackId": "your-playback-id-here"
}
}