azureterraformazure-cognitive-search

How do I get an API key from an Azure AI Search service created in Terraform in order to use it in another resource?


I am following this guide in order to create Azure AI Search indexes, indexers, and datasources: https://medium.com/expert-thinking/mastering-azure-search-with-terraform-a-how-to-guide-7edc3a6b1ee3

I want to be able to create an Azure AI Search service with Terraform and then use its created API key to use in my restapi provider.

Using:

terraform v.1.7.1

hashicorp/azurerm v3.95.0

mastercard/restapi v1.19.0

environment/dev/main.tf

module "main" {
  source                  = "../.."
  environment             = var.environment
  az_subscription_id      = var.az_subscription_id
  other_modules_vars      = .....
  azure_search_api_key    = [how to get this after creating the azure search?]
}

main.tf:

module "az-search" {
  source                      = "./modules/az-search"
  environment                 = var.environment
  az_resource_group_name      = var.az_resource_group_name
  az_resource_group_location  = var.az_resource_group_location
  providers = {
    azurerm = azurerm.sub
  }
}

providers.tf:

provider "azurerm" {
  alias           = "sub"
  subscription_id = var.az_subscription_id
  features {}
}

provider "restapi" {
  uri                   = "https://some-search.search.windows.net"
  write_returns_object  = true
  debug                 = true

  headers = {
    "api-key"       = [how do I get this from the created resource?]
    "Content-Type"  = "application/json"
  }

  create_method   = "POST"
  update_method   = "PUT"
  destroy_method  = "DELETE"
}

az-search/main.tf

resource "azurerm_search_service" "search" {
  name                          = "some-search"
  resource_group_name           = var.az_resource_group_name
  location                      = var.az_resource_group_location
  sku                           = var.sku
}

resource "restapi_object" "create_datasource" {
  path          = "/datasources"
  query_string  = "api-version=2023-10-01-Preview"
  data          =  file(var.datasource_path)
  id_attribute  = "name"
}

Am I able to get the API key from the created resource and then insert it into the restapi provider? If more info is needed, please let me know, I'm just starting out terraform and adding to previous code.


Solution

  • How do I get an API key from an Azure AI Search service created in Terraform in order to use it in another resource?

    Firstly, below are the two API_keys access keys, one is primary, and the other one is secondary for API access control. Either of the ways will be useful for accessing the service.

    enter image description here

    So, there are two specific attributes available in terraform for primary_APIkey as well as for secondary_APIkey.

    enter image description here

    Use outputs block to retrieve the API key from Azure AI search service to be provided in rest API provider as shown below.

    output "rest_api_key"{
    value = azurerm_search_service.example.primary_key
    sensitive = true
    

    main.tf:

    terraform {
      required_providers {
        azurerm = {
          source = "hashicorp/azurerm"
          version = "3.108.0"
        }
      }
    }
    provider "azurerm"{
    features{}
    }
    data "azurerm_resource_group" "example" {
      name     = "asha"
    }
    
    resource "azurerm_search_service" "example" {
      name                = "newsearchservicej"
      resource_group_name = data.azurerm_resource_group.example.name
      location            = data.azurerm_resource_group.example.location
      sku                 = "standard"
    }
    output "rest_api_key"{
    value = azurerm_search_service.example.primary_key
    sensitive = false
    }
    

    Note: Access the API key from output block and pass it in the code wherever it is required accordingly.

    Output:

    enter image description here