如何从 Terraform 中创建的 Azure AI 搜索服务获取 API 密钥,以便在其他资源中使用它?

问题描述 投票:0回答:1

我按照本指南创建 Azure AI 搜索索引、索引器和数据源:https://medium.com/expert-thinking/mastering-azure-search-with-terraform-a-how-to-guide -7edc3a6b1ee3

我希望能够使用 Terraform 创建 Azure AI 搜索服务,然后使用其创建的 API 密钥在我的 Restapi 提供程序中使用。

使用:

terraform v.1.7.1

hashicorp/azurerm v3.95.0

万事达卡/restapi v1.19.0

环境/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"
}

我是否能够从创建的资源中获取 API 密钥,然后将其插入到 restapi 提供程序中?如果需要更多信息,请告诉我,我刚刚开始使用 terraform 并添加到之前的代码中。

azure terraform azure-cognitive-search
1个回答
0
投票

如何从 Terraform 中创建的 Azure AI 搜索服务获取 API 密钥,以便在其他资源中使用它?

首先,下面是两个

API_keys
访问键,一个是
primary
,另一个是
secondary
用于API访问控制。这两种方式都有助于访问服务。

enter image description here

因此,terraform 中有两个特定的 attributes 可用于

primary_APIkey
以及
secondary_APIkey

enter image description here

使用 outputs 块从 Azure AI 搜索服务检索要在 Rest API 提供程序中提供的 API 密钥,如下所示。

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
}

注意:从

output
块访问API密钥,并将其传递到代码中相应需要的地方。

输出

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.