How to Build an AI Agent Declaratively with Terraform
In this tutorial, you'll learn how to build and deploy AI agents declaratively using the ChatBotKit Terraform Provider. By treating your AI infrastructure as code, you gain version control, reproducibility, and automated deployments for your conversational AI solutions.
What You'll Learn
By the end of this tutorial, you will be able to:
- Install and configure the ChatBotKit Terraform Provider
- Define AI bots, datasets, and skillsets as Terraform resources
- Add abilities that give your agent tools like web search
- Deploy your AI agent to messaging platforms like Slack or Telegram
- Manage your AI infrastructure through CI/CD pipelines
Prerequisites
Before starting, make sure you have:
- A ChatBotKit account with an API key
- Terraform version 1.0 or higher installed
- Basic familiarity with Terraform concepts (providers, resources, state)
Estimated time: 20-30 minutes
Step 1: Set Up Your Project
Create a new directory for your Terraform project and initialize it:
Create a file named main.tf with the provider configuration:
Set your API key as an environment variable:
Tip: You can get your API key from the ChatBotKit Dashboard.
Initialize Terraform to download the provider:
You should see a message confirming that the ChatBotKit provider was installed successfully.
Step 2: Create a Knowledge Base
AI agents are more useful when they have access to relevant information. Let's create a dataset that serves as a knowledge base:
Add the following to your main.tf file:
The dataset acts as a retrieval-augmented generation (RAG) source. When the bot receives questions, it can search this dataset to find relevant information.
Step 3: Create a Skillset with Abilities
Skillsets give your AI agent the ability to perform actions beyond just answering questions. Let's create a skillset with web search and fetch capabilities:
These abilities use ChatBotKit's built-in templates to give your agent access to real-time web information. Templates provide pre-configured instructions for common actions, making it easy to add powerful capabilities.
Step 4: Define the AI Agent
Now let's create the main bot resource that ties everything together:
The backstory field is crucial—it defines your agent's personality, capabilities, and behavior guidelines. Think of it as the system prompt that shapes how your agent responds.
Step 5: Add an Integration
To make your agent accessible, you need to deploy it to a platform. Let's add a trigger integration that allows the bot to be invoked via webhooks:
You can also deploy to messaging platforms. Here's how to add a Slack integration:
Step 6: Add Outputs
Add outputs to easily reference the created resources:
Step 7: Deploy Your Agent
Preview the changes Terraform will make:
Review the output to ensure everything looks correct. You should see resources being created for the dataset, skillset, abilities, bot, and integration.
Apply the configuration to create your AI agent:
Type yes when prompted to confirm. Terraform will create all the resources and output their IDs.
Step 8: Test Your Agent
Once deployed, you can test your AI agent in the ChatBotKit dashboard:
- Go to ChatBotKit and navigate to Bots
- Find your "AI Assistant" bot
- Click on it to open the Colabo testing environment
- Try asking questions like:
- "What can you help me with?"
- "Search the web for the latest AI news"
- "Fetch and summarize https://example.com"
Complete Configuration
Here's the complete main.tf file for reference:
Troubleshooting
Authentication Errors
If you see Error: 401 Unauthorized, verify that:
- Your
CHATBOTKIT_API_KEYenvironment variable is set correctly - The API key has not expired
- You're using the correct API key from your account
Resource Already Exists
If Terraform reports that a resource already exists, you can import it:
State Drift
If resources were modified outside of Terraform, run terraform plan to see the differences and terraform apply to reconcile them.
Next Steps
Now that you have a working AI agent deployed with Terraform, consider exploring:
- Adding more abilities like email sending or calendar integration
- Setting up CI/CD with GitHub Actions for automated deployments
- Creating multiple environments using Terraform workspaces
- Importing existing bots to bring them under Terraform management
- Building multi-agent systems with multiple bots collaborating
By managing your AI agents as code, you can version control your configurations, collaborate with your team, and deploy consistently across environments.