Menu

Expand
Rate 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.

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

Twilio CLI Setup

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

twilio profiles:list

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

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

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

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

twilio serverless:deploy -p <YOUR_PROFILE_NAME>

For example, I would run:

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

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:

{
  "projects": {

  }
}

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.

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:

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

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

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:

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

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:

Need some help?

We all do sometimes; code is hard. Get help now from our support team, or lean on the wisdom of the crowd by visiting Twilio's Community Forums or browsing the Twilio tag on Stack Overflow.

        
        
        

        Thank you for your feedback!

        We are always striving to improve our documentation quality, and your feedback is valuable to us. How could this documentation serve you better?

        Sending your feedback...
        🎉 Thank you for your feedback!
        Something went wrong. Please try again.

        Thanks for your feedback!

        Refer us and get $10 in 3 simple steps!

        Step 1

        Get link

        Get a free personal referral link here

        Step 2

        Give $10

        Your user signs up and upgrade using link

        Step 3

        Get $10

        1,250 free SMSes
        OR 1,000 free voice mins
        OR 12,000 chats
        OR more