The Twilio provider is used to interact with the resources supported by Twilio. The provider needs to be configured with the proper credentials before it can be used.
The following arguments are supported:
account_sid
- (Required) Twilio Account SID. This can also be set via theTWILIO_ACCOUNT_SID
environment variable.auth_token
- (Required) Auth token for the account. This can also be set via theTWILIO_AUTH_TOKEN
environment variable.subaccount_sid
- (Optional) Twilio Subaccount SID. This can also be set via theTWILIO_SUBACCOUNT_SID
environment variable.edge
- (Optional) The Edge location to be used by the Twilio client. This can also be set via theTWILIO_EDGE
environment variable.region
- (Optional) The Region to be used by the Twilio client. This can also be set via theTWILIO_REGION
environment variable.
terraform {
required_providers {
twilio = {
source = "twilio/twilio"
version = ">=0.4.0"
}
}
}
# Configure the Twilio provider. Credentials can be found at www.twilio.com/console
provider "twilio" {
// username defaults to TWILIO_API_KEY with TWILIO_ACCOUNT_SID as the fallback env var
// password defaults to TWILIO_API_SECRET with TWILIO_AUTH_TOKEN as the fallback env var
}
# Create a new API key resource
resource "twilio_api_accounts_keys" "key_name" {
friendly_name = "terraform key"
}
output "messages" {
value = twilio_api_accounts_keys.key_name
}
then execute the following in your terminal to initialize and apply the changes to your Twilio infrastructure:
# Initialize a new or existing Terraform working directory
$ terraform init
# Generate a new infrastructure plan and create or update infrastructure accordingly
$ terraform apply
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 takes precedence.
provider "twilio" {
// username defaults to TWILIO_API_KEY with TWILIO_ACCOUNT_SID as the fallback env var
// password defaults to TWILIO_API_SECRET with TWILIO_AUTH_TOKEN as the fallback env var
region = "au1"
edge = "sydney"
}
Setting this configuration will result in the Twilio client 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.
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"
}
Alternatively, you can specify the subaccount to use at the resource level:
resource "twilio_api_accounts_keys" "key_name" {
path_account_sid = "AC00112233445566778899aabbccddeeff"
friendly_name = "subaccount key"
}
resource "twilio_api_accounts_keys" "key_name" {
friendly_name = "terraform key"
}
output "messages" {
value = twilio_api_accounts_keys.key_name
}
To delete a specific key in your terraform infrastructure you can use the command:
terraform destroy -target twilio_api_keys.<resource name>
To delete the terraform key created in this example, use:
terraform destroy -target twilio_api_keys.key_name
sid
- The unique string that that we created to identify the Key resource.friendly_name
- The string that you assigned to describe the resource.
For more information see the API Key documentation.
resource "twilio_api_accounts_incoming_phone_numbers" "phone_number" {
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"
}
resource "twilio_api_accounts_incoming_phone_numbers" "imported_phone_number" {
phone_number = "+14444444444"
}
resource "twilio_studio_flows_v2" "studio_flow" {
commit_message = "first draft"
friendly_name = "terraform flow"
status = "draft"
definition = jsonencode({
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 guidance on how to import resources from an existing Flex project, please reference our Flex example documentation.