Setup Your Own CoTurn Server Using AWS EC2 Instance
By: Narendiran Krishnan
Tags: JavaScript, WebRTC, Peer To Peer, Servers, Ec2

Setup Your Own CoTurn Server Using AWS EC2 Instance

Some random dude in google images

NAT (Network Address Translation) traversal servers in webRTC are the reason that the media gets properly connected and those servers are STUN and TURN.

If you guys are wondering what this webRTC still then have a read through my blog on it

What is Turn Server ?

Traversal Using Relays around NAT is a protocol that assists in the traversal of network address translators or firewalls for multimedia applications.

It may be used with the Transmission Control Protocol and User Datagram Protocol. It is most useful for clients on networks masqueraded by symmetric NAT devices.

When Turn Server is needed?

On a typical webRTC app, about 20% of connections require a TURN server. It may work fine for you, but try accessing your webRTC service from a cell phone connection (which will usually require TURN), and you’ll see that not all connections are equal when it comes to p2p.

For peer to peer, you will need to install and run a TURN server.

But the question is why ?

Because some firewalls block certain types of traffic. Many just block UDP, but some may even block TCP.

Turn server Hardware

It is not mandatory that we should have a higher configuration or something. You can make the setup even with basic configurations like using a free tier instance, which is available on the AWS for EC2 i.e. t2.micro.

If you wish to test whether your browser supports this webRTC then you can test it out here.

All right Now let’s start to implement it from the EC2 instance,

Step 1:

Here you can just grab whatever is available on the free tier for EC2 instance.

Step 2: EC2 Instance security group settings

Then only changes while launching an instance is in the security groups, I have given the details of the security groups port then we need to

UDP → 32355–65535, 3478–3479 TCP → 32355–65535, 3478–3479

Step 3: Install coturn

CoTurn is an Open Source Implementation of TURN and STUN Servers.

Code is as follows,

sudo apt-get -y update sudo apt-get install coturn

Step 4: Stop the CoTurn Server & make the changes

Before we make any changes we need to stop the coturn server first.

Then we need to uncomment the TURNSERVER_ENABLED=1 and move the following location,

Code is as follows,

sudo systemctl stop coturn / sudo systemctl stop coturn.services sudo vi /etc/default/coturn

Now un-comment and it should look like the following,

UNCOMMENT IT

Step 5: Make changes in the turnserver.conf file / coturn setting file

Move to that location or else open the file from the following location,

Code is as follows,

cd /etc/

Then you need to open either using a vim editor / nano editor / vi editor in super user mode i.e. “sudo su”

Code is as follows,

sudo nano turnserver.conf

Remove the comments from the following lines, and add any necessary information.

port details to be uncommented

Now you need to update your own public IP of the EC2 instance which you have configured.

listening ip

Here also you need to configure the same give the EC2 instance Public ip, i.e. listening ip and relay ip should be the same, but in this example it’s different since i restarted my EC2 instance.

relay ip

finger and lt cred mech

Here there is an update given by the official creator since it is 2021, they gave an update it seems

2021 update by CoTurn

Here you can see that the “no-loopback-peers” option has been replaced by “allow-loopback-peers”.

It is up to you to comment that or un-comment but I highly recommend you all to comment that line.

Once you have done all these properly then you’re good to go.

Step 6: start the coturn server

Since we have stopped our server in step 4 we need to start it up,

Code is as follows,

sudo systemctl start coturn / sudo systemctl start coturn.services

Then you need to verify it using the following code,

sudo systemctl status coturn / sudo systemctl status coturn.services

Once you hit enter you should see the out and it should show you the output as “ active (running) “.

You can check and verify this in the following website,

Here you can see the current working Stun Server i.e. the free public server which is hosted by our Uncle Google. Well, there will be an option to change the details in the ICE Server. You need to first

Reset to defaults Select and remove the details in the ICE Servers Then enter the turn server details

turn:< PUBLIC IP / EC2 Public Instance IP / Domain Name>:3478?transport=tcp example:

turn:narenltk.com:3478

4. Then given the username as “ <USERNAME> ” and password as “ <PASSWORD> “

5. Then hit “add Server” select “Get Candidates”

done message

You should always see the “Priority” column should give a result “Done” or if it is not configured properly, then you will end up with an output “Unreachable”.

But if you need to update your own uname, password or you need to update your own certificate and key with domain all can be done just proceed to scroll and look further options and you will find the following details below,

user cert pkey server-name

I will let you guys do the changes on this.

Moreover always update the Certificate and Private Key in the “ PEM “ format.

If you wanna generate your own SSL and then add it then follow the blog below,

Once you have made all the changes you to add the details in the following way to your code,

code update details

If you wish to stay connected,