Terraform Twilio provider
Перейти к файлу
Elmer Thomas af85868047 update slack token 2021-06-22 13:21:24 -07:00
.github chore: update template files 2021-06-07 23:30:43 +00:00
client feat: add subaccount support for v2010 apis (#23) 2021-05-19 15:16:58 -07:00
examples fix: clean up json serialization logic and update examples (#41) 2021-06-22 11:41:22 -07:00
githooks chore: add linting (#24) 2021-05-19 08:38:54 -07:00
twilio fix: clean up json serialization logic and update examples (#41) 2021-06-22 11:41:22 -07:00
.gitignore chore: migrate to flex common code (#20) 2021-04-26 13:53:20 -07:00
.travis.yml update slack token 2021-06-22 13:21:24 -07:00
CHANGES.md feat: generate from librarian (#28) 2021-06-17 09:53:43 -07:00
CODE_OF_CONDUCT.md chore: update template files 2021-06-07 23:30:43 +00:00
ISSUE_TEMPLATE.md chore: update template files 2021-06-07 23:30:43 +00:00
LICENSE chore: update template files 2021-06-07 23:30:43 +00:00
Makefile feat: generate from librarian (#28) 2021-06-17 09:53:43 -07:00
PULL_REQUEST_TEMPLATE.md chore: update template files 2021-06-07 23:30:43 +00:00
README.md docs: fix link in Readme 2021-06-22 12:48:50 -07:00
go.mod chore: refactoring generator (#37) 2021-06-21 10:14:29 -05:00
go.sum feat: add cluster tests (#38) 2021-06-21 08:58:33 -07:00
main.go feat: add subaccount support for v2010 apis (#23) 2021-05-19 15:16:58 -07:00
usage.md docs: Add basic usage documentation (#21) 2021-05-13 15:58:30 -06:00

README.md

Twilio Terraform Provider

⚠️ Note: Issues are currently closed on this repo as we are not quite ready for feedback. Thanks!

Requirements

  • Terraform v0.15.x
  • Go 1.15+ (to build the provider plugin)

Building The Provider

Clone repository:

$ git clone git@github.com:twilio/terraform-provider-twilio

Enter the provider directory and build the provider:

$ make build

To start using the Twilio Terraform Provider follow the documentation under installing and using the provider.

Installing and Using the Provider

  1. Run make install to install and build the twilio-terraform-provider.
  2. Configure the Twilio provider with your twilio credentials in your Terraform configuration file (e.g. main.tf). These can also be set via TWILIO_ACCOUNT_SID and TWILIO_AUTH_TOKEN environment variables.
  3. Add your resource configurations to your Terraform configuration file (e.g. main.tf).
terraform {
    required_providers {
        twilio = {
            source  = "twilio.com/twilio/twilio"
            version = "0.2.0"
        }
    }
}

# Credentials can be found at www.twilio.com/console.
provider "twilio" {
  account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  auth_token  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

resource "twilio_api_accounts_keys_v2010" "key_name" {
    account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    friendly_name = "terraform key"
}

output "messages" {
    value = twilio_api_accounts_keys_v2010.key_name
}
  1. Run terraform init and terraform applyto initialize and apply changes to your twilio infrastructure.

Create and Delete API Keys

resource "twilio_api_accounts_keys_v2010" "key_name" {
  account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  friendly_name = "terraform key"
}

output "messages" {
    value = twilio_api_accounts_keys_v2010.key_name
}

To delete a specific key in your terraform infrastructure you can use the command terraform destroy -target twilio_api_keys_v2010.<resource name>. To delete the terraform key created in this example use terraform destroy -target twilio_api_keys_v2010.key_name.

Buy and Configure a Phone Number

resource "twilio_api_incoming_phone_numbers_v2010" "buy_phone_number" {
    account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    area_code = "415"
    friendly_name = "terraform phone number"
    sms_url = "https://demo.twilio.com/welcome/sms/reply"
    voice_url = "https://demo.twilio.com/docs/voice.xml"
}

Import a Twilio Phone Number

resource "twilio_api_accounts_incoming_phone_numbers_v2010" "import_purchased_number" {
    account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    phone_number = "+14444444444"
}

Define a Studio Flow

resource "twilio_studio_flows_v2" "studio_flow" {
    commit_message = "first draft"
    friendly_name = "terraform flow"
    status = "draft"
    definition = "{\"description\": \"A New Flow\", \"states\": [{\"name\": \"Trigger\", \"type\": \"trigger\", \"transitions\": [], \"properties\": {\"offset\": {\"x\": 0, \"y\": 0}}}], \"initial_state\": \"Trigger\", \"flags\": {\"allow_concurrent_calls\": true}}"
}

After creating a studio flow, you can make changes to your infrastructure by changing the values in your configuration file. Run terraform apply to apply these changes to your infrastructure.

For more examples checkout the documentation in the usage.md and the examples folder.

Specify a Region and/or Edge

You can define the Edge and/or Region by setting the environment variables TWILIO_EDGE and/or TWILIO_REGION. However, the resource configuration in your Terraform configuration file take precedence.

provider "twilio" {
    account_sid = "ACxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    auth_token  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
    region = "au1"
    edge = "sydney"
}

This will result in the hostname transforming from api.twilio.com to api.sydney.au1.twilio.com.

A Twilio client constructed without these parameters will also look for TWILIO_REGION and TWILIO_EDGE variables inside the current environment.

Specify subaccount for v2010 APIs

You can specify a subaccount to use with the provider by either setting the TWILIO_SUBACCOUNT_SID environment variable or explicitly passing it to the provider like so:

provider "twilio" {
  //  account_sid defaults to TWILIO_ACCOUNT_SID env var
  //  auth_token  defaults to TWILIO_AUTH_TOKEN env var
  //  subaccount_sid  defaults to TWILIO_SUBACCOUNT_SID env var
}
provider "twilio" {
    account_sid    = "AC00112233445566778899aabbccddeefe"
    auth_token    = "12345678123456781234567812345678"
    subaccount_sid = "AC00112233445566778899aabbccddeeff"
}

Developing the Provider

The boilerplate includes the following:

  • Makefile contains helper functions used to build, package and install the Twilio Terraform Provider. It's currently written for MacOS Terraform provider development, but you can change the variables at the top of the file to match your OS_ARCH.

    The install function is configured to install the provider into the ~/.terraform.d/plugins/ folder.

  • examples contains sample Terraform configuration that can be used to test the Twilio provider

  • twilio contains the main provider code. This will be where the provider's resources and data source implementations will be defined.

If you wish to work on the provider, you'll first need Go installed on your machine (version 1.15+ is required).

To compile the provider, run make build. This will build the provider and put the provider binary in the $GOPATH/bin directory.

$ make build
...
$ $GOPATH/bin/terraform-provider-twilio
...

In order to run the full suite of Acceptance tests, run make testacc. Provide your Account SID and Auth Token as environment variables to properly configure the test suite.

Note: Acceptance tests create real resources, and often cost money to run.

$  make testacc ACCOUNT_SID=YOUR_ACCOUNT_SID AUTH_TOKEN=YOUR_AUTH_TOKEN

You can also specify a particular suite to run like so:

$  make testacc TEST=./twilio/ ACCOUNT_SID=YOUR_ACCOUNT_SID AUTH_TOKEN=YOUR_AUTH_TOKEN

An example test file can be found here.

Debugging

First:

$ export TF_LOG=TRACE

then refer to the Terraform Debugging Documentation.