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:
- A Twilio Account
- An AWS Account
- An SSH client such as Putty (Putty is free to download and use)
- A basic understanding of Linux.
Now that you have the basic requirements, let’s get started. We have separated this tutorial into five sections. Here they are:
- AWS - How to Spin Up your AWS-Twilio Instance from the AWS Marketplace
- Twilio - Creating and Configuring a Twilio SIP Trunk for Termination (sending calls from your AWS Asterisk instance to Twilio’s network)
- Twilio - Creating and Configuring a Twilio SIP Trunk for Origination (sending calls from Twilio’s network to your Asterisk instance)
- AWS - Setting Up Your Instance’s Asterisk/Twilio Configuration
- Testing your set-up
- 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.
- Go to the AWS Marketplace and subscribe to the instance - https://aws.amazon.com/marketplace/pp/B07KWHFP7H;
- Once there, click on ‘Continue to Subscribe’ - http://drops.tiltx.com/eNrmgp;
- Review the terms and click on ‘Accept Terms’ - http://drops.tiltx.com/MmIRt7;
- 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;
- 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;
- On the following screen, select your preferred region and click ‘Click to Launch’ - http://drops.tiltx.com/kYn3yq
- 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
- EC2 Console: https://console.aws.amazon.com/ec2/v2/home?;
- Click on ‘Running Instances’ - http://drops.tiltx.com/aWFrJ1;
- Once there, select your new instance and write down your ‘IPv4 Public IP’ address - http://drops.tiltx.com/RInSc7.
- 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
- Click on ‘Elastic IPs’ under the ‘Network & Security’ - http://drops.tiltx.com/Yx5qbt;
- Click on ‘Allocate new address’ - http://drops.tiltx.com/UGev7b;
- On the next screen, click on ‘Allocate’ - http://drops.tiltx.com/dXNU4g
- Once you click on ‘Allocate’, AWS will provide you with your new Elastic IP;
- 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:
- Select the new Elastic IP address created above - http://drops.tiltx.com/qy1eiu;
- Click on ‘Actions’ and select ‘Associate Address’ - http://drops.tiltx.com/jqajCT
- On the next screen, select ‘Instance’ as the ‘Resource Type’ and click on the ‘Instance’ drop-down - http://drops.tiltx.com/0HlE9a
- Select your new instance;
- You may leave the ‘Private IP’ with the default value, ‘Select a private IP’;
- Click on Associate - http://drops.tiltx.com/cNfFgF.
- 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
- Go to your EC2 Console, https://console.aws.amazon.com/ec2/v2/home?;
- Click on ‘Running Instances’ - http://drops.tiltx.com/aWFrJ1;
- Select your new instance - http://drops.tiltx.com/BSFVVa;
- 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
- Log into the Twilio Console - https://www.twilio.com/login;
- From the main Dashboard on the left side of the screen, select (...) for All Products &
- Scroll down to ‘Elastic SIP Trunking’ and click it - http://drops.tiltx.com/tTBUbN;
- From the Elastic SIP Trunking dashboard, click the ‘Getting Started’ button - http://drops.tiltx.com/nOPmw9;
- From the Getting Started with Elastic SIP Trunking page, Click the ‘Create a SIP Trunk’ - http://drops.tiltx.com/nwRqwx;
- Enter a Friendly Name; for this tutorial, I used "AsteriskPBX". Click ‘Create’ - http://drops.tiltx.com/00pVwk;
- On the "AsteriskPBX" General Settings screen, note the settings that apply to this newly created truck;
- From here, On the left side of the screen under your "AsteriskPBX" trunk name, choose Termination, http://drops.tiltx.com/ILItW1;
- In the Termination SIP URI edit box, enter a unique termination URI (we use "tiltx-asterisk-outbound");
- 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;
- If available, it should look like this - http://drops.tiltx.com/N0ySro;
- 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;
- 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
- 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;
- Click the red plus sign by the ‘IP Access Control List’ text box - http://drops.tiltx.com/m9F3On;
- 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;
- You may find your instance’s public IP address in your AWS EC2 console - http://drops.tiltx.com/Ylvw7B
- Once you have entered your information, click the red ‘Create ACL’ button - http://drops.tiltx.com/eUJzqr.
- Click the red plus sign by the ‘Credentials List’ text box - http://drops.tiltx.com/SULM00;
- 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.
- Write down your username and password; you will need this later when configuring your Asterisk instance.
- Once you have completed the information, click the red ‘Create’ button - http://drops.tiltx.com/yfTXHq.
- 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.
- 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
- Click on ‘Origination’ - http://drops.tiltx.com/LSnG7b;
- 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;
- Now, stay with me. This part may look complicated, but it is straightforward:
- When you click on the ‘"Add new Origination URI’, you will see the following pop-up - http://drops.tiltx.com/DvqVoc;
- 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;
- For example, if your AWS public IP address is 22.214.171.124 and your Twilio region is us1, then you will enter sip:126.96.36.199;region=us1;
- Here’s a sample of what it should look like - http://drops.tiltx.com/hbmZvJ.
- Just in case you need it, here’s a list of the Twilio regions - https://www.twilio.com/docs/sip-trunking#OriginationURI-region.
- 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
- On the left side of the screen under your "AsteriskPBX" Trunk Name, choose ‘Numbers’ - http://drops.tiltx.com/8bNaUV;
- 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.
- 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.
- 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;
- Once you have selected the number(s) you would like to add, click on ‘Add Selected’ - http://drops.tiltx.com/YleXxr;
- You will now see your number added to your Origination Trunk - http://drops.tiltx.com/H2a1z0.
- 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.
- 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.
- If you do not have an SSH client, you can download Putty: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
- The login name is ec2-user: http://drops.tiltx.org/g01ZQF
- You may be prompted for a password. This is the password you created on your PPK file from Putty. Otherwise, simply proceed.
- 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
- 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.
- Issue the following command – vi /etc/asterisk/sip.conf and press enter;
- Go to the bottom file (if you’d prefer, press Shift+G at the same to go to the bottom).
- At the bottom of the file, you will see the [TWILIO-OUTBOUND] context- http://drops.tiltx.com/qD8yc4;
- Here, you will need to modify your username and password from the Authentication Credentials List (http://drops.tiltx.com/jlLNXJ):
- 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;
- Press ‘:wq’ to leave the file, http://drops.tiltx.com/FX10mR.
- 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;
- Go to the bottom file (iff you’d prefer, press Shift+G at the same to go to the bottom).
- 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.
- 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.
- The ports for SIP and RTP traffic are SIP: UDP 5060 - 5061 and RTP: UDP 10000 - 20000
- 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 email@example.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.