March 3, 2020

Default Azure Search Service query key and Terraform

Need to get the default Azure Search Service query key into a variable to use in other parts of your terraform infrastructure? I did too. Unfortunately the docs for this were a bit vague, so through a bit of trial and error we got there


Given that these query keys are called the “default” keys, I assumed we could access this attribute by azurerm_search_service.example.query_keys.default.key. But that was not the case.

After outputting the query_key attributes during a terraform apply

output "searchkeys" {
  value = azurerm_search_service.example.query_keys

I could see that this “default” key was actually nameless

searchkeys = [
    "key" = "be2c38e286fff7df25d17e21294604a8"
    "name" = ""

More trial and error

azurerm_search_service.example.query_keys.object[0].key azurerm_search_service.example.query_keys."".key

but the winning combination was


So now you can use this in other parts of your terraform infrastructure as you normally would. For example, inside of appsettings for an Azure web app:

  app_settings = {
    "AzureSearchConfig:SearchServiceName" =
    "AzureSearchConfig:SearchServiceQueryApiKey" = azurerm_search_service.example.query_keys[0].key
    "AzureStorageConfig:ConnectionString" = azurerm_storage_account.example.primary_connection_string
    "WEBSITE_TIME_ZONE" = "AUS Eastern Standard Time"