Deploying LiveKit on AWS EC2

Deploying LiveKit on AWS EC2

LiveKit is an open-source platform that provides a modern, end-to-end WebRTC stack for building live video and audio applications. It can handle large-scale deployments and offers built-in load balancing and automatic scaling capabilities. While LiveKit can be deployed on a virtual machine (VM), this approach requires manual scaling and management of system updates. AWS EC2 offers a more efficient solution with automated scaling and built-in security features.

Here's how to deploy LiveKit on AWS EC2:

Prerequisites:

  • An AWS account

  • A domain name and an SSL certificate

  • A Linux VM (Ubuntu or Amazon Linux recommended) with root access

  • Docker and Docker Compose installed

  • Caddy or another web server

Steps:

  1. Configure your domain and SSL certificate:

    • Point your domain's A record to the IP address of your EC2 instance.

    • Install and configure Caddy or your chosen web server with your SSL certificate.

  2. Generate LiveKit configuration:

    • Use LiveKit's configuration generation tool to create a customized configuration for your domain. This tool generates files for Docker Compose, LiveKit server, and Redis.

    • Run the tool on your local machine:

    docker pull livekit/generate
    docker run --rm -it -v$PWD:/output livekit/generate
  • This will create a folder with your domain name containing the necessary configuration files.
  1. Deploy LiveKit on EC2:

    • Upload the generated files to your EC2 instance.

    • Create a Redis instance on AWS ElastiCache (recommended) or install Redis on your EC2 instance.

    • Edit the docker-compose.yaml file to configure Redis connection details and other settings.

    • Start LiveKit using Docker Compose:

    docker-compose up -d
  1. Test and configure:

    • Open your website in a web browser and verify that LiveKit is functioning properly.

    • Configure additional settings like TURN server details and user management as needed.

  2. Automate deployments with Cloud-Init:

    • Use Cloud-Init to automate the installation and configuration of LiveKit on your EC2 instance. This allows for easier scaling and deployment of multiple LiveKit servers.

    • Generate a Cloud-Init script using LiveKit's tool:

    docker run --rm -it -v$PWD:/output livekit/generate --cloud-init
  • Upload the script to your EC2 instance and follow the instructions to configure Cloud-Init user data.

  • When starting a VM, paste the contents of the file cloud-init.xxxx.yaml into the User data field.

  • That's it! When the machine starts up, it'll execute the cloud-init protocol and install LiveKit.

  • When the install script is finished, your instance should be set up. It will have installed:

    • docker

    • docker-compose

    • generated configuration to /opt/livekit

    • systemd service livekit-docker

To start/stop the service via systemctl:

    systemctl stop livekit-docker
    systemctl start livekit-docker
  1. Firewall:

    Ensure that the following ports are open on your firewall and accessible on the instance:

    • 443 - primary HTTPS and TURN/TLS

    • 80 - TLS issuance

    • 7881 - WebRTC over TCP

    • 3478/UDP - TURN/UDP

    • 50000-60000/UDP - WebRTC over UDP

And if Ingress is desired

  • 1935 - RTMP Ingress

  • 7885/UDP - WebRTC for WHIP Ingress

  1. Troubleshooting

    If something is not working as expected, SSH in to your server and use the following commands to investigate:

     systemctl status livekit-docker
     cd /opt/livekitsudo 
     docker-compose logs