简体   繁体   English

azure function 通过 terraform:如何连接到服务总线

[英]azure function via terraform: how to connect to service bus

I am stuck when trying to deploy an Azure function via Azure DevOps pipelines and Terraform.我在尝试通过 Azure DevOps 管道和 Z3603E96F8ZAEB776 部署 Azure function 时卡住了

Running terraform apply works fine and the Service Bus looks good and works.运行terraform apply工作正常,服务总线看起来很好并且工作正常。 In the Azure portal the function seems to be running, but it complains that it can not find the ServiceBusConnection .在 Azure 门户中, function 似乎正在运行,但它抱怨找不到ServiceBusConnection

I defined it via the following Terraform declaration:我通过以下 Terraform 声明定义了它:

resource "azurerm_resource_group" "rg" {
  name     = "rg-sb-westeurope"
  location = "westeurope"


}

resource "azurerm_servicebus_namespace" "sb" {
  name                = "ns-sb"
  location            = azurerm_resource_group.rg.location
  resource_group_name = azurerm_resource_group.rg.name
  sku                 = "Standard"

}

resource "azurerm_servicebus_queue" "sbq" {
  name                = "servicebusqueue"
  resource_group_name = azurerm_resource_group.rg.name
  namespace_name      = azurerm_servicebus_namespace.sb.name

  enable_partitioning = true
}

resource "azurerm_servicebus_namespace_authorization_rule" "sb-ar" {
  name                = "servicebus_auth_rule"
  namespace_name      = azurerm_servicebus_namespace.sb.name
  resource_group_name = azurerm_resource_group.rg.name

  listen = false
  send   = true
  manage = false
}

In the function app i declare:在 function 应用程序中,我声明:

resource "azurerm_function_app" "fa" {
  name                       = "function-app"
  location                   = azurerm_resource_group.rg.location
  resource_group_name        = azurerm_resource_group.rg.name
  app_service_plan_id        = azurerm_app_service_plan.asp.id
 
  storage_account_name       = azurerm_storage_account.sa.name
  storage_account_access_key = azurerm_storage_account.sa.primary_access_key
  app_settings = {
      ServiceBusConnection    = azurerm_servicebus_namespace_authorization_rule.sb-ar.name
  }

}

This tf.这个 tf. will not work out of the box as i have not copied here the full declaration.不能开箱即用,因为我没有在这里复制完整的声明。 I think I am setting the connection environment vars wrong but have no idea on how to do it correctly.我想我设置的连接环境变量错误,但不知道如何正确地做到这一点。

EDIT With the hint from @Heye I got it working.编辑在@Heye 的提示下,我得到了它的工作。 This is the correct snipped replacing the name with primary_connection_string .这是用primary_connection_string替换name的正确片段。

resource "azurerm_function_app" "fa" {
  name                       = "function-app"
  location                   = azurerm_resource_group.rg.location
  resource_group_name        = azurerm_resource_group.rg.name
  app_service_plan_id        = azurerm_app_service_plan.asp.id
 
  storage_account_name       = azurerm_storage_account.sa.name
  storage_account_access_key = azurerm_storage_account.sa.primary_access_key
  app_settings = {
      ServiceBusConnection    = azurerm_servicebus_namespace_authorization_rule.sb-ar.primary_connection_string
  }

}

You are setting the ServiceBusConnection value to the name of the authorization rule.您将ServiceBusConnection值设置为授权规则的name However, you probably want to set it to the primary_connection_string , as that contains the key along with all the information needed to connect to the Service Bus.但是,您可能希望将其设置为primary_connection_string ,因为它包含密钥以及连接到服务总线所需的所有信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM