Managing Dialogflow CX Agents with Terraform
Kristopher Overholt
Developer Advocate, Google Cloud
Dialogflow CX is a powerful tool in Google Cloud that you can use to design conversational agents powered by Natural Language Understanding (NLU) to transform user requests into actionable data. You can integrate voice and/or chat agents in your app, website, or customer support systems to determine user intent and interact with users.
If you've ever wanted to get started with Dialogflow CX, you might have seen or ran through the quickstart steps to build a shirt ordering agent that you can ask for the store location, get store hours, or make a shirt order.
While going through the quickstart steps, you might find yourself wanting to codify all of the Dialogflow CX components and settings, which would help you quickly spin up agents and manage their configuration programmatically. In fact, you might already be using infrastructure as code tooling and best practices to manage virtual machines in Compute Engine, Kubernetes clusters in GKE, or topics and subscriptions in Pub/Sub. You can also use the same infrastructure as code approach with your Dialogflow CX agents: Terraform and Google Cloud to the rescue!
You can use the Terraform modules for Dialogflow CX along with the sample Terraform + Dialogflow CX configuration files to reproduce the chatbot/agent described in the "build a shirt ordering agent" quickstart. Try them out and spin up a Dialogflow CX agent with a single command in your own Google Cloud account!
Setup
There are a few things that you'll need to set up before you run the sample Terraform configuration files for Dialogflow CX.
Register for a Google Cloud account.
Enable the Dialogflow CX API.
Install and initialize the Google Cloud CLI.
Install Terraform.
Usage
Once you've completed the setup on your local machine, you're ready to spin up your own fully-configured Dialogflow CX agent in seconds:
Clone the CCAI samples repository and cd into the
dialogflow-cx/shirt-order-agent/
directory.Edit the values in
variables.tf
to specify your Google Cloud project ID along with your desired region and zone.Run
terraform init
to initialize the directory that contains the Terraform configuration files.Run
terraform apply
, the command that spins everything up!
Once you run terraform apply
and confirm the proposed plan, you'll see messages about all of the components that were provisioned, including the agent, pages, intents, flows, and more:
Now that you’ve provisioned your agent in Dialogflow CX, you're ready to view and test your agent in the Dialogflow CX Console!
How it works
We're using the Terraform modules for Dialogflow CX to define a conversational agent and all of its components. We've reproduced the agent described in the build a shirt ordering agent quickstart.
All of the agent's associated entity types, flows, intents, and pages are created and managed with Terraform, so you can edit your Terraform configuration files to change certain parameters, run terraform apply
, and see your changes instantly reflected in the Dialogflow CX console.
You might notice that the flows.tf file actually uses a local-exec
command within a null_resource
block to make a REST API call instead of using a Terraform resource for Dialogflow CX to define the flow. This approach was used since Dialogflow CX creates a default start flow when the agent is created rather than being created and managed by Terraform. As a result, we can use a REST API call to PATCH the default start flow and then modify its messages and routes. We can still use Terraform to templatize and trigger the REST API command, which means that you can manage any setting that is also available in the Dialogflow CX REST API, or even add custom callbacks to other Google Cloud services if needed.
Summary
It's convenient to be able to manage conversational agents as code using Terraform in Google Cloud. We get all of the benefits of Dialogflow CX with the convenience of Terraform to manage everything in a stateful and version-control friendly way.
Now that you've captured all of your Dialogflow CX agent settings and configuration in Terraform, you are ready to check your Terraform scripts into version control, spin up and destroy agents as you please using terraform apply
and terraform destroy
, or even store remote Terraform state in Google Cloud using the GCS backend.
Take a look at the Terraform + Dialogflow CX sample code along with the Terraform modules for Dialogflow CX so you can spin up your own Dialogflow CX agents with a single command. If you found this Terraform code sample useful, be sure to star, watch, or ask questions in our CCAI samples repository on GitHub!