Build the future of communications.
Start building for free

Setting up Elastic SIP Trunking with Asterisk on AWS in Minutes

Twilio_Blog_SIP-Trunking

Have you thought about deploying a telecommunication solution to the AWS cloud using Twilio and Asterisk and just don’t know where to begin? Well, you have found your starting point!

When we first began to deploy our VoIP solution in 2010 with AWS and Twilio, we learned how cumbersome it can be to integrate AWS with Asterisk and Twilio. So, along with the folks at Twilio, we wanted to make your life easier by providing an instance that is plug and play, as well as a tutorial (with screenshots) on how to get it done!

In this tutorial, you will learn how to create and configure a Twilio SIP Trunk with Asterisk on AWS in just minutes. And, once your done, you should be able to place and receive calls on Twilio’s amazing global network.

Asterisk on AWS with Twilio Tutorial Requirements

To successfully work through this tutorial, you will need the following:

Now that you have the basic requirements, let’s get started. We have separated this tutorial into five sections. Here they are:

  1. AWS - How to Spin Up your AWS-Twilio Instance from the AWS Marketplace
  2. Twilio - Creating and Configuring a Twilio SIP Trunk for Termination (sending calls from your AWS Asterisk instance to Twilio’s network)
  3. Twilio - Creating and Configuring a Twilio SIP Trunk for Origination (sending calls from Twilio’s network to your Asterisk instance)
  4. AWS - Setting Up Your Instance’s Asterisk/Twilio Configuration
  5. Testing your set-up
  6. Throwing a Party!

AWS - How to Spin Up your AWS-Twilio Instance

In this section, we will cover how to subscribe to your instance via the AWS Marketplace, configure your options (including Elastic IPs), launch your instance, and assign your new IP to your new Asterisk-Twilio Instance.

Launching Instance

  1. Go to the AWS Marketplace and subscribe to the instance - https://aws.amazon.com/marketplace/pp/B07KWHFP7H;
  2. Once there, click on ‘Continue to Subscribe’ - http://drops.tiltx.com/eNrmgp;
  3. Review the terms and click on ‘Accept Terms’ - http://drops.tiltx.com/MmIRt7;
  4. When you click on ‘Accept Terms’, AWS will begin to process your request and the button ‘Continue to Configuration’ will be grayed-out - http://drops.tiltx.com/VFjXAV;
  5. Once AWS has processed the request, the ‘Continue to Configuration’ will be enabled and you should click it to start launching your instance - http://drops.tiltx.com/vgD0EU;
  6. On the following screen, select your preferred region and click ‘Click to Launch’ - http://drops.tiltx.com/kYn3yq
  7. On the screen that follows, select ‘Launch from Website’ (http://drops.tiltx.com/HBRKtF), configure your options, and click ‘Launch’ - http://drops.tiltx.com/lYdkuv;

Now that you have your instance running, you will want to visit your EC2 console and write down your public IP.

Viewing Public IP

  1. EC2 Console: https://console.aws.amazon.com/ec2/v2/home?;
  2. Click on ‘Running Instances’ - http://drops.tiltx.com/aWFrJ1;
  3. Once there, select your new instance and write down your ‘IPv4 Public IP’ address - http://drops.tiltx.com/RInSc7.
  4. If you do not see an IP address in ‘IPv4 Public IP’, then see the two selections below, otherwise, you can skip it!

If you have launched your instance, but you do not see an IP in the ‘IPv4 Public IP’ and your EC2 console looks more like this http://drops.tiltx.com/y3q5mN, then you need to get an Elastic IP to your instance. Here’s how:

Getting an Elastic IP from AWS

  1. Click on ‘Elastic IPs’ under the ‘Network & Security’ - http://drops.tiltx.com/Yx5qbt;
  2. Click on ‘Allocate new address’ - http://drops.tiltx.com/UGev7b;
  3. On the next screen, click on ‘Allocate’ - http://drops.tiltx.com/dXNU4g
  4. Once you click on ‘Allocate’, AWS will provide you with your new Elastic IP;
  5. Write down this IP address (you will need it later to create the Twilio SIP trunk) and click the ‘Close’ button - http://drops.tiltx.com/jAvPlq.

Next, we need to assign our new Elastic IP. Follow these steps.

Assign the Elastic IP to your new Asterisk-Twilio Instance:

  1. Select the new Elastic IP address created above - http://drops.tiltx.com/qy1eiu;
  2. Click on ‘Actions’ and select ‘Associate Address’ - http://drops.tiltx.com/jqajCT
  3. On the next screen, select ‘Instance’ as the ‘Resource Type’ and click on the ‘Instance’ drop-down - http://drops.tiltx.com/0HlE9a
  4. Select your new instance;
  5. You may leave the ‘Private IP’ with the default value, ‘Select a private IP’;
  6. Click on Associate - http://drops.tiltx.com/cNfFgF.
  7. On the next screen, you should now see a confirmation that your Elastic IP was successfully assigned - http://drops.tiltx.com/1TlfhT.

This next step is very important; you must reboot your instance.

Rebooting Your Instance

  1. Go to your EC2 Console, https://console.aws.amazon.com/ec2/v2/home?;
  2. Click on ‘Running Instances’ - http://drops.tiltx.com/aWFrJ1;
  3. Select your new instance - http://drops.tiltx.com/BSFVVa;
  4. Select ‘Actions’ and click ‘Instance State’ and ‘Reboot’ - http://drops.tiltx.com/gIdtVR

And that’s it! Your Twilio-Asterisk instance is ready!

Twilio - Creating and Configuring a Twilio SIP Trunk for Termination (sending calls from your Asterisk instance to Twilio)

In this section, you will learn about Termination Endpoints and how to create a SIP trunk and an endpoint.

When creating a SIP trunk on Twilio, keep this in mind - Twilio has a global network. So, when you see the phrase ‘Termination Endpoint’, we simply mean the data center closest to your server. This is where you will send any outbound calls to Twilio. Here’s a list of these endpoints.

Now let’s start creating the endpoint on Twilio. We will use this in our Asterisk setup later. Here are the steps with screenshots.

Creating your SIP Trunk

  1. Log into the Twilio Console - https://www.twilio.com/login;
  2. From the main Dashboard on the left side of the screen, select (...) for All Products &
    Services- http://drops.tiltx.com/PnOTHZ;
  3. Scroll down to ‘Elastic SIP Trunking’ and click it - http://drops.tiltx.com/tTBUbN;
  4. From the Elastic SIP Trunking dashboard, click the ‘Getting Started’ button - http://drops.tiltx.com/nOPmw9;
  5. From the Getting Started with Elastic SIP Trunking page, Click the ‘Create a SIP Trunk’ - http://drops.tiltx.com/nwRqwx;
  6. Enter a Friendly Name; for this tutorial, I used "AsteriskPBX". Click ‘Create’ - http://drops.tiltx.com/00pVwk;
  7. On the "AsteriskPBX" General Settings screen, note the settings that apply to this newly created truck;
  8. From here, On the left side of the screen under your "AsteriskPBX" trunk name, choose Termination, http://drops.tiltx.com/ILItW1;
  9. In the Termination SIP URI edit box, enter a unique termination URI (we use "tiltx-asterisk-outbound");
  10. On the right side, you will see if the trunk name you entered is available. If it is, great! If not, go ahead and enter a different name;
  11. If available, it should look like this - http://drops.tiltx.com/N0ySro;
  12. Remember what I said about ‘Termination Endpoints’? Well, now that you have created your SIP trunk, you will see your personalized SIP Termination Endpoint list by clicking the ‘Show Localized URIs’ link - http://drops.tiltx.com/akBMTF;
  13. When you click ‘Show Localized URIs’, you will see something like this - http://drops.tiltx.com/NF83xP.

Now, we need to setup Authentication. There are two ways to set up Authentication - IP Access Control List and via Credentials List - http://drops.tiltx.com/KSG5B5. In this tutorial, we will cover both:

IP Access Control List

  1. If you plan to use IP Authentication, we highly recommend assigning an Elastic IP to your AWS instance (http://drops.tiltx.com/peHX1G). This way, in the event your instance is shutdown and re-started, your instance’s public IP will not change and you will be able to continue placing calls via Twilio;
  2. Click the red plus sign by the ‘IP Access Control List’ text box - http://drops.tiltx.com/m9F3On;
  3. When you click the red plus sign, you will see a window that asks you for a ‘Friendly Name’ as well as IP address and it should look something like this - http://drops.tiltx.com/3QKtfm;
  4. You may find your instance’s public IP address in your AWS EC2 console - http://drops.tiltx.com/Ylvw7B
  5. Once you have entered your information, click the red ‘Create ACL’ button - http://drops.tiltx.com/eUJzqr.

Credentials List

  1. Click the red plus sign by the ‘Credentials List’ text box - http://drops.tiltx.com/SULM00;
  2. When you click on the red plus sign, you will see a window that asks for you a ‘Friendly Name’, Username, and Password - http://drops.tiltx.com/jlLNXJ.
  3. Write down your username and password; you will need this later when configuring your Asterisk instance.
  4. Once you have completed the information, click the red ‘Create’ button - http://drops.tiltx.com/yfTXHq.
  5. Now, click the ‘Save’ button towards the bottom, left side and we will move on! Here’s a screenshot of the ‘Save’ button - http://drops.tiltx.com/7ADqiK.
  6. Let’s stay on this web page. In the next section, we will work with Origination.

Twilio - Creating and Configuring a Twilio SIP Trunk for Origination (sending calls from Twilio to your Asterisk instance)

In this section, we will cover how to configure Origination (calls going from Twilio to your AWS Asterisk instance) with the IP address of your new instance, as well as how to assign telephone numbers to your Origination trunk.

Creating Orignation URI

  1. Click on ‘Origination’ - http://drops.tiltx.com/LSnG7b;
  2. Click the "Add new Origination URI" button, where we will define how calls are sent from
    Twilio to your AWS Asterisk instance - http://drops.tiltx.com/5PdZjt;
  3. Now, stay with me. This part may look complicated, but it is straightforward:
  4. When you click on the ‘"Add new Origination URI’, you will see the following pop-up - http://drops.tiltx.com/DvqVoc;
  5. In the ‘Origination SIP URI’ box, enter the public IP address of your AWS instance using the following format - sip:AWS-PUBLIC-IP-ADDRESS;region=TWILIO-REGION;
  6. For example, if your AWS public IP address is 1.2.3.4 and your Twilio region is us1, then you will enter sip:1.2.3.4;region=us1;
  7. Here’s a sample of what it should look like - http://drops.tiltx.com/hbmZvJ.
  8. Just in case you need it, here’s a list of the Twilio regions - https://www.twilio.com/docs/sip-trunking#OriginationURI-region.
  9. Now that we have created that, let’s assign a telephone number to our trunk and test it. After that, you are on your way to becoming a VoIP genius!

Assigning Telephone Numbers to Your Trunk

  1. On the left side of the screen under your "AsteriskPBX" Trunk Name, choose ‘Numbers’ - http://drops.tiltx.com/8bNaUV;
  2. Click the ‘+’ to buy a new number or click ‘Add Existing Number’. In our example, we have already purchased a number that meets our needs. Therefore, we will click on ‘Add Existing Number’ - http://drops.tiltx.com/ZKtU6x.
  3. If you need help buying a new number, here’s how to do it - https://support.twilio.com/hc/en-us/articles/223135247-How-to-Search-for-and-Buy-a-Twilio-Phone-Number-from-Console.
  4. When the ‘Add Existing Number’ box pops-up, select the number you want to add by selecting the box on the right-hand side - http://drops.tiltx.com/fSgFrh;
  5. Once you have selected the number(s) you would like to add, click on ‘Add Selected’ - http://drops.tiltx.com/YleXxr;
  6. You will now see your number added to your Origination Trunk - http://drops.tiltx.com/H2a1z0.
  7. And at this point, your Twilio configuration is all set up! Congrats! Let’s move on to configuring Asterisk.

AWS - Setting Up Your Instance’s Asterisk/Twilio Configuration

In this section, we will cover how to set-up your Asterisk instance to work with your Twilio configuration so you may send calls (termination) to Twilio. We will also review your EC2 security group settings so you may customize it to your region.

Configuring Asterisk

  1. Login via SSH to the IP address in from the ‘IPv4 Public IP’ with the certificate created during instance setup. You will need the certificate you created when you launched the instance.
  2. If you do not have an SSH client, you can download Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
  3.  The login name is ec2-user: http://drops.tiltx.org/g01ZQF
  4. You may be prompted for a password. This is the password you created on your PPK file from Putty. Otherwise, simply proceed.
  5. Once logged in, you will want to issue a ‘sudo’ command to enable superuser access. Type in the following: sudo -s http://drops.tiltx.com/QUbJQf
  6. This step is very important. Once you are in as sudo -s, you will need to modify the sip.conf file and add your username and password from your Authentication, http://drops.tiltx.com/jlLNXJ. Most importantly, you will need to modify the entry(ies) at the very bottom of the file.
  7. Issue the following command – vi /etc/asterisk/sip.conf and press enter;
  8. Go to the bottom file (if you’d prefer, press Shift+G at the same to go to the bottom).
  9. At the bottom of the file, you will see the [TWILIO-OUTBOUND] context- http://drops.tiltx.com/qD8yc4;
  10. Here, you will need to modify your username and password from the Authentication Credentials List (http://drops.tiltx.com/jlLNXJ):
  11. Press ‘i’ to modify the file. The ‘defaultuser’ will be the ‘Username’ from the above screenshot and the ‘secret’ will be the ‘Password’ from the above screenshot. Press the ‘ESC’ key to escape from edit mode;
  12. Press ‘:wq’ to leave the file, http://drops.tiltx.com/FX10mR.
  13. Once you have modified SIP.conf, you may edit extensions.conf to suit your needs. This file is located at /etc/asterisk/extensions.conf. Issue the following command – vi /etc/asterisk/extensions.conf and press enter;
  14. Go to the bottom file (iff you’d prefer, press Shift+G at the same to go to the bottom).
  15. At the bottom of the file, you will see the [TWILIO-OUTBOUND] context- http://drops.tiltx.com/KoMQVS. From here, you can modify the file to meet your needs.
  16. Now, you are all set and you can send and receive calls from your AWS-Twilio-Asterisk instance. By default, SIP and Media ports are open to the world. We recommend you view the EC2 Security Group associated with your instance and modify to your needs based on Twilio’s SIP Whitelist - https://www.twilio.com/docs/sip-trunking#whitelist
  17. The ports for SIP and RTP traffic are SIP: UDP 5060 - 5061 and RTP: UDP 10000 - 20000
  18. That’s it! You are done!

Congratulations! You have done it! You can now leverage Twilio’s extensive and incredible telecommunications network on the AWS cloud. 

Now, we know how important help is and we all want to see you succeed. Like Twilio says, “We can’t wait to see what you build.” And that is very true. If you get stuck, or need help, just reach out to us. Our basic support email is aws-twilio@tiltx.com. And remember, we offer a free 30 day trial on the AWS Marketplace so you may test drive it.

I’d love to hear from you or collaborate on different projects. You can always reach me via email at Orlando@tiltx.com.

Authors
Sign up and start building
Not ready yet? Talk to an expert.