Skip to content

Terraform

Last updated View as MarkdownAgent setup

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.

Create an instance

Use the cloudflare_ai_search_instance resource. The id argument is the instance name.

Default namespace

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.

Custom namespace

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
}

Instance with a data source

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.

Computed values

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.

Changes that replace the instance

Import an instance

Import an existing instance with an ID in the format <account_id>/<namespace>/<id>:

terraform import cloudflare_ai_search_instance.docs <account_id>/support/docs

For an instance in the default namespace, use default as the namespace:

terraform import cloudflare_ai_search_instance.docs <account_id>/default/docs

You can also use an import block:

import {
  to = cloudflare_ai_search_instance.docs
  id = "${var.account_id}/default/docs"
}

Data sources

Set namespace on both data sources. Use default for the default namespace.

Read one instance

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"
}

List instances

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"
}

Was this helpful?