
Share:
Adam is a developer and consultant who enjoys ultra-running, blogging/vlogging, and helping others tame technology to accomplish amazing things with an insatiable desire to mentor and help.
Creating a Video Session in AWS Lambda With Python
Time to read: 8 minutes
In this post, you'll deploy a Vonage Video OpenTok session client as a microservice to AWS Lambda, written in Python. You do so using this Python application available on Github that uses Flask, and Serverless.
This example application provides the first steps required to utilize the Vonage Video API. This microservice enables applications to request a video chat session. By obtaining a session, you also get a session_id to allow a token request. The corresponding /token request method in this app provides that.
 Prerequisites
 Setup Instructions
Clone the nexmo-community/opentok-session-lambda-python repo from GitHub, and navigate into the newly created directory to proceed.
 Environment
Rename .env.default to .env and add values to OPENTOK_API_KEY and OPENTOK_API_SECRET provided by your Vonage Video APIs account.
 Usage
To start, create a virtualenv from within the project root to contain the project as you proceed. Then activate it, as follows:
Next, initialize npm and follow the prompts to get it set up. In most cases, you should select the defaults, unless you desire to change any of them. Also, use npm to install some needed dependencies for development to enable Serverless and Lambda to work with the Flask app. Use the following commands to do complete this step.
Now you should use pip to install the required Python dependencies from the requirements.txt included in the cloned code.
 Running Local
With the virtualenv set up, you can run the app locally and test things out before deploying to AWS Lambda. You can serve it with the following command:
By default, running locally on your system serves the app at http://localhost:5000. Hitting Ctrl+c closes it down after you finish.
 Deploy to Lambda
With all the above finished successfully, you can use Serverless to deploy the app to AWS Lambda.
After deployment, you receive the URL needed to access the application via the API Gateway. Make a note of the URL for the next step.
IMPORTANT: The example application, as-is, does not carry out any authentication or verification. Anyone with access to the URL provided after deployment can access it. Doing so could cause unexpected charges to your Vonage account. Therefore, please secure the app if you intend to leave it active.
 Available Endpoints
There are 4 URL endpoints available with this client:
GET request to
/Doesn't perform any actions, but provides a quick way to test
POST request to
/session/sessionprovides the session ID.By including a form POST like the following, you can change default parameters used to create a session: (defaults shown as if you leave the body empty.)
location=None, media_mode=relayed, archive_mode=manualNote: See documentation for media_mode and archive_mode options.
NOTE: Location expects an IP address.
/token/<session_id>You can then request a new session by passing the
<session_id>to the/tokenendpoint.
 Examples:
Go to the URL provided by the deploy process. Below are some examples of what requests may look like: (Your URL will vary.)
GET https://7ulasfasdasdfw4.execute-api.us-east-1.amazonaws.com/dev/
The / endpoint returns the generic message.
POST https://7ulasfasdasdfw4.execute-api.us-east-1.amazonaws.com/dev/session
The session endpoint will return the session_id needed to request a token.
https://7ulasfasdasdfw4.execute-api.us-east-1.amazonaws.com/dev/token/9807adsf0sae89fu0se87r0sf
The token endpoint will return the token needed to interact with OpenTok.
 Deactivating Virtualenv
To exit the virtualenv, you can deactivate it when desired.
 Next Steps
If you have any questions or run into troubles, you can reach out to @VonageDev on Twitter or inquire in the Vonage Developer Community Slack team. Good luck.