This Twilio product is currently available as a Public Beta release. Some features are not yet implemented and others may be changed before the product is declared as Generally Available. Beta products are not covered by a Twilio SLA . Learn more about beta product support.
Functions is a service that lets you run Node.js code without provisioning or managing your own servers, while also providing convenient integrations with the Twilio SDK. Just as Functions lets you host code, Assets allows you to host your static files without needing to think about the underlying infrastructure. With these services, you're free to focus on writing your application, and Twilio will handle the details such as scaling, hosting, and maintenance.
Functions and Assets now support the following Regions:
Note that during this initial phase of the rollout of Twilio Regions, Twilio does not guarantee that all data will remain within your selected Region.
A Service's domain includes its respective Edge and Region. Existing domains that don't include Region and Edge are implicitly set to the US1 Region and Ashburn Edge.
With that in mind, the pattern for Functions URLs with regional support is:
<unique-name>-<random-four-digits>[-<optional-environment-suffix>].<edge>.<region>.twil.io
Examples:
ahoy-1234.dublin.ie1.twil.io
owls-3423-stage.sydney.au1.twil.io
If you want to host a Service in multiple Regions, you must deploy individually to each desired Region. There is currently no Console support for deploying a Service to every desired Region. You could, however, write a script that calls the Serverless Toolkit deploy
command for each desired Region, or do so with the Serverless API directly depending on the specifics of your use case.
Deployments that use dependencies will still contact the npm registry for dependencies. We do not have control over whether npm will fetch those dependencies from the US or not.
Because of regional isolation, failures in one Region will not result in traffic being redirected to another Region that is still available (automatic failover). This might result in lower availability that is outside our control in some cases.
For example, if your application is deployed to both US1 and AU1, and there is an infrastructure outage in AU1, traffic intended for AU1 will not reroute to US1 instances.
Serverless Functions by default provide your Auth Token as an Environment Variable, accessible via context.AUTH_TOKEN
, and use it to initialize the built-in Twilio REST Client which is accessible via context.getTwilioClient()
. This Auth Token is specific to the region in which the Function is deployed.
This means that your default Twilio client will only be capable of making requests to Twilio products and services in the same Region. Likewise, your Auth Token will only be valid for interacting with regionalized APIs in the same Region.
In order for your Function to use products that are not available in the same Region, you'll have to break out of regional isolation (which is not recommended unless absolutely necessary). You can do so by manually providing credentials for the alternative Region with support for the service that you wish to interact with.
Protected Functions and Assets use signature validation to protect them from requests that don't originate from Twilio products and services (or your own code that generates secure signatures).
Your application auth token, which is used to generate these signatures, is different from Region to Region. This means that Functions and Assets in one Region may only be triggered by Twilio products in that same Region.
You may also send requests to a Protected Function or Asset in a different Region, but you will need to generate a valid X-Twilio-Signature
header based on credentials from that same Region.
Go to the Explore page, click the caret on the Functions and Assets card, and select your Region of choice
The Console navigation bar will update and show a new section dedicated to the Region that you selected.
If you expand the newly added Region sub-nav, you will see a new entry for Functions and Assets. When viewing any portion of the Functions and Assets UI for this Region, you'll see a Region indicator along the top of the Console to help you identify which Region you are editing.
Create a new Service the same way you would normally with Functions and Assets
You'll see that your URL now includes the region you chose
Make sure that you have the Twilio CLI and version 3.1.2 or newer of the Serverless Toolkit plugin installed
Create a new regional profile:
1# For example, if you are in the au1 region2TWILIO_EDGE=sydney twilio login --region=au13# For example, if you are in the ie1 region4TWILIO_EDGE=dublin twilio login --region=ie1
Create and develop a new Service if you don't already have one. (If you do, skip to the next step)
When deploying your Service with the Serverless Toolkit, specify the profile associated with your intended Region: twilio serverless:deploy -p <YOUR_REGIONAL_PROFILE_NAME>
If you use twilio-run
, or if you want to pass the intended credentials directly into your deployment command, you can use the --region
flag.
Example 1:
1twilio serverless:deploy \2--region=ie1 \3--username=<YOUR_TWILIO_REGIONAL_API_KEY> \4--password=<YOUR_TWILIO_REGIONAL_API_SECRET>
Example 2:
1twilio-run deploy \2--region=au1 \3--username=<YOUR_TWILIO_REGIONAL_API_KEY> \4--password=<YOUR_TWILIO_REGIONAL_API_SECRET>
To use different environment variables per Region, you can modify your .twilioserverlessrc
configuration file by adding your Account SID and respective Region to the projects
section that the settings should be applied to. This works with any configurable setting available in .twilioserverlessrc
.
For example:
1{2"projects": {3"AC11111111111111111111111111111111:us1": {4"env": ".env"5},6"AC11111111111111111111111111111111:ie1": {7"env": ".env.ie1"8}9}10}
The Serverless API works the same way as other Twilio APIs do across Regions, for example, serverless.twilio.com
automatically maps to the US Region and Ashland Edge.
If interacting with the Serverless API in a non-US region, be sure to use the appropriate base URL in your HTTP requests:
serverless.dublin.ie1.twilio.com
serverless.sydney.au1.twilio.com
If you're making requests to Twilio in a non-US region with a Twilio Helper Library, you must initialize that SDK's Twilio client with the appropriate values for Region and Edge. This guide goes into detail about how to do so.
For the @twilio-labs/serverless-api library, you can pass region
and edge
when initializing the client.
The Runtime Handler provides Environment Variables to help determine where a given Function is being executed. You may use these in logs or any other logic:
context.TWILIO_REGION
context.TWILIO_EDGE
If context.TWILIO_REGION
is undefined
in a deployed Function, you can assume that it is running in the US Region.
There are currently no other Edges available other than the default ones for each region. The default Edges for each Region are:
us1
→ ashburn
ie1
→ dublin
au1
→ sydney
If your Function interacts with Twilio services in a Region other than where your Function is deployed, the data for those requests will be transmitted to and processed/stored in that Region.
As mentioned earlier, Functions by default are only provided with credentials for and access to Twilio APIs in the same Region. This means that if you deploy to IE1, your Twilio client can only communicate with APIs that are also in IE1.
To create a Twilio client that is capable of communicating with Twilio products in a separate Region:
Create a new API Key SID/Secret pair in the Console
Add the API Key SID and Secret as environment variables to your Service, for example: TWILIO_US1_API_KEY_SID
and TWILIO_US1_API_KEY_SECRET
In your Function, create a new Twilio client like so:
1const clientUs1 = new Twilio(2context.TWILIO_US1_API_KEY_SID,3context.TWILIO_US1_API_KEY_SECRET,4{5region: 'us1',6edge: 'ashburn',7accountSid: context.TWILIO_ACCOUNT_SID8});
Use this US-specific client to make calls to Twilio services in the US, despite your Function operating in another Region (such as Ireland or Australia):
1clientUs1.messages.create({2from: context.US_PHONE_NUMBER,3to: 'some other US number',4body: 'Ahoy from another region!',5})6.then((message) => {7console.log({ callSid: message.sid });8return callback();9})10.catch((error) => {11console.error(error);12return callback(error);13});