I do not this this question needs to be Azure resource-specific, but I will provide my background. I currently have Terraform code that creates an Azure AI Search service, disables public network access, and connects it to a private endpoint. This private endpoint is connected to the same virtual network and subnet as a CosmosDB Mongo resource, which holds the data I want to index from.
There is not a way to create indexes/indexers/datasources in Terraform through the hashicorp/azurerm/azurerm_search_service
, so I am using mastercard/restapi
to do so. I am following this guide as well: https://medium.com/expert-thinking/mastering-azure-search-with-terraform-a-how-to-guide-7edc3a6b1ee3
I’m starting by creating a datasource to connect the Azure Search and CosmosDB resources.
providers.tf
provider "restapi" {
uri = "https://example-search.search.windows.net"
write_returns_object = true
debug = true
headers = {
"api-key" = module.az-search.azure_search_api_key
"Content-Type" = "application/json"
}
create_method = "POST"
update_method = "PUT"
destroy_method = "DELETE"
}
az-search/main.tf
...
# Create the Datasource for the Product Collection in DB
resource "restapi_object" "create_product_datasource" {
path = "/datasources"
query_string = "api-version=2024-05-01-Preview"
data = jsonencode(local.product_datasource_json)
id_attribute = "name"
}
locals {
product_datasource_json = {
description = "Links Azure Search with the Product Collection within CosmosDB"
type = "cosmosdb"
subtype = "MongoDb"
credentials = {
connectionString = "AccountEndpoint=https://${azurerm_search_service.search.name}.documents.azure.com:443/;AccountKey=${var.cosmosdb_readonly_key};Database=DB;ApiKind=MongoDB"
}
container = {
name = "Product"
query = null
}
name = "product-datasource"
identity = null
}
}
The above code will create a datasource correctly when the Azure AI Search and CosmosDB resources are publicly accessible, however, I am not sure how to create this restapi_object when both of the resources are private. Both of the resources are within the same vnet and subnet and have a private endpoint.
Here is the error I get when applying:
│ Error: Post "example-search/datasources?api-version=2024-05-01-Preview": unsupported protocol scheme ""
│
│ with module.main.module.az-search.restapi_object.create_product_datasource,
│ on ../../modules/az-search/main.tf line 52, in resource "restapi_object" "create_product_datasource":
│ 52: resource "restapi_object" "create_product_datasource" {
|
I am also open to using a different provider other than mastercard/restapi if needed, though the guide was very helpful since I am not familiar with it. Let me know if any more tf files are needed to answer this question.