Use the Cloudflare Terraform provider ↗︎ to create, update, import, and read AI Search instances as code. Namespace support for instances is available from provider v5.29.0.
To manage the namespaces themselves, refer to Namespaces with Terraform.
Use the cloudflare_ai_search_instance resource. The id argument is the instance name.
If you omit namespace, Terraform creates the instance in the default namespace.
resource "cloudflare_ai_search_instance" "docs" {
account_id = var.account_id
id = "docs"
}An instance without a data source uses built-in storage. You can upload documents to it with the Items API.
To create an instance in a custom namespace, set namespace. Reference the name of a cloudflare_ai_search_namespace resource so that Terraform creates the namespace before the instance.
resource "cloudflare_ai_search_namespace" "support" {
account_id = var.account_id
name = "support"
}
resource "cloudflare_ai_search_instance" "docs" {
account_id = var.account_id
id = "docs"
namespace = cloudflare_ai_search_namespace.support.name
}Set type and source to connect a data source. type accepts r2 or web-crawler.
resource "cloudflare_ai_search_instance" "internal_docs" {
account_id = var.account_id
id = "internal-docs"
namespace = "default"
type = "r2"
source = "my-docs-bucket"
max_num_results = 20
}For more information, refer to R2 data source and Website data source.
AI Search fills in some fields when you create an instance, for example the models (such as aisearch_model and embedding_model), chunk_size, and token_id. Terraform treats these fields as computed. If you omit them from your configuration, Terraform keeps the current value instead of planning a change.
Import an existing instance with an ID in the format <account_id>/<namespace>/<id>:
terraform import cloudflare_ai_search_instance.docs <account_id>/support/docsFor an instance in the default namespace, use default as the namespace:
terraform import cloudflare_ai_search_instance.docs <account_id>/default/docsYou can also use an import block:
import {
to = cloudflare_ai_search_instance.docs
id = "${var.account_id}/default/docs"
}Set namespace on both data sources. Use default for the default namespace.
The cloudflare_ai_search_instance data source reads one instance by id.
data "cloudflare_ai_search_instance" "docs" {
account_id = var.account_id
namespace = "support"
id = "docs"
}The cloudflare_ai_search_instances data source returns the instances in one namespace.
data "cloudflare_ai_search_instances" "support" {
account_id = var.account_id
namespace = "support"
}