Skip to contentSkip to navigationSkip to topbar
Rate this page:
On this page

Using Serverless Toolkit with multiple Twilio Projects


Generally we recommend using Twilio Functions' "Environments" feature to deal with multiple deployments of your Functions project — for example, to verify your changes in a development or staging environment.

However, if you are using Functions in combination with Flex, for example, the chances are high that you are using independent Twilio projects with separate Account SIDs for each environment. The following guide shows you how you can make the most of this scenario.

(warning)

Warning

This guide expects you to have a minimum @twilio-labs/plugin-serverless version of 2.0.0 or twilio-run version of 3.0.0.

You can find your plugin-serverless version by running twilio plugins.

You can find your twilio-run version by running npx twilio-run --version inside your project directory.

While some of the following steps can be achieved with twilio-run, we recommend using the Twilio CLI and @twilio-labs/plugin-serverless when working with multiple projects. An exception is making deployments using a Continuous Delivery (CD) system. If you are doing that, please check out this guide instead.


Prerequisites

prerequisites page anchor

Twilio CLI Setup

twilio-cli-setup page anchor

The Twilio CLI supports using several authentication profiles. You can see which profiles you currently have by running:


_10
twilio profiles:list

Create one for each of the Twilio Projects/Accounts that you want to deploy to by running:


_10
twilio profiles:create

You'll be asked to provide a name for your profile. You can use that name later on in any Twilio CLI command to tell the CLI to use those credentials by using the -p <YOUR_PROFILE_NAME> option.

We'll use my-profile and team-profile as examples.

Setup your Twilio Serverless project

setup-your-twilio-serverless-project page anchor

If you don't already have an existing project using the Serverless Toolkit, go ahead and create one by following the instructions in our Getting Started guide. Otherwise, keep reading.


Deploying to multiple accounts

deploying-to-multiple-accounts page anchor

Once you have set up your project and your profiles, you can deploy to the respective profiles by running:


_10
twilio serverless:deploy -p <YOUR_PROFILE_NAME>

For example, I would run:


_10
twilio serverless:deploy -p my-profile

You'll see a file in your project called .twiliodeployinfo that gets updated after each deployment. This file makes sure that switching between different accounts is effortless. By default this file is added to your .gitignore file to prevent it from being tracked in your version control system.

If you are working alone on your project and you don't want different environment variables or similar configuration between the different accounts, you are set.


Configuration for multiple accounts and team members

configuration-for-multiple-accounts-and-team-members page anchor

If you are sharing your project with other developers and you all deploy to the same accounts and/or Function services, you should configure the following settings.

Start by creating a .twilioserverlessrc file at the root of your project — if you don't have one in your project already — and add this to it:


_10
{
_10
"projects": {
_10
_10
}
_10
}

Inside the projects key you'll place configuration objects specific to an individual Account SID. This is where we can specify the respective Service SIDs that map to our Account SIDs.

(information)

Info

You can actually copy the information from your .twiliodeployinfo file. Copy the file once you are done deploying for the first time, rename the copy .twilioserverlessrc, and delete the the lastBuildSid rows.

Your .twilioserverlessrc file should look something like this:


_10
{
_10
"projects": {
_10
"AC11111111111111111111111111111111": {
_10
"serviceSid": "ZS11111111111111111111111111111111"
_10
},
_10
"AC22222222222222222222222222222222": {
_10
"serviceSid": "ZS22222222222222222222222222222222"
_10
}
_10
}
_10
}

Whenever you run any twilio serverless: command it will look up the Account SID here and apply the configuration you specified for that Account.

Match environment variables to accounts

match-environment-variables-to-accounts page anchor

You might want to configure the use of different environment variables for each Account SID. You can do that by creating a configuration similar to this:


_12
{
_12
"projects": {
_12
"AC11111111111111111111111111111111": {
_12
"serviceSid": "ZS11111111111111111111111111111111",
_12
"env": ".env.dev"
_12
},
_12
"AC22222222222222222222222222222222": {
_12
"serviceSid": "ZS22222222222222222222222222222222",
_12
"env": ".env.production"
_12
}
_12
}
_12
}

In this case if I deploy using twilio serverless:deploy -p my-profile, which maps to AC11111111111111111111111111111111, it will use the .env.dev file. When I run twilio serverless:deploy -p team-profile instead, it will use my .env.production file.

There are many things you can configure here. In general you can specify in the configuration anything that you can pass as a flag to the CLI. Check out our Configuration and Meta Files documentation to learn more about it.


Rate this page: