在 Terraform 中使用函数应用程序创建事件中心触发器时的 Azure 事件中心连接

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

我正在尝试使用 terraform 并使用事件中心触发器作为函数触发器在 azure 函数应用程序内配置函数。

这是我正在配置的 function_app 的示例代码

  resource "azurerm_service_plan" "example-linux-serviceplan" {
      name                = "linux-service-plan"
      resource_group_name = azurerm_resource_group.azure-xxxxx.name
      location            = azurerm_resource_group.azure-xxxxxx.location
      os_type             = "Linux"
      sku_name            = "S1"
    }
    
    
    resource "azurerm_linux_function_app" "example-linux-app" {
      name                = "example-linux-app"
      resource_group_name = azurerm_resource_group.azure-xxxxx.name
      location            = azurerm_resource_group.azure-xxxxxx.location
      service_plan_id     = azurerm_service_plan.example-linux-serviceplan.id
    
      storage_account_name       = azurerm_storage_account.azure-xxxxx.name
      storage_account_access_key = azurerm_storage_account.azure-xxxxx.primary_access_key

  app_settings = {
    "connection_name" = azurerm_eventhub_namespace.azure-xxxx.default_primary_connection_string
  }
    
      site_config {
        application_stack {
          node_version = "18"
        }
      }
    }
    
    resource "azurerm_function_app_function" "example" {
          name            = "example-function-app-function"
          function_app_id = azurerm_linux_function_app.example-linux-app.id
          test_data = jsonencode({
            "name" = "Azure"
          })
          config_json = jsonencode({
            "bindings" = [
              {
                "authLevel" = "function"
                "direction" = "in"
                "name"       = "req"
                "type"       = "eventHubTrigger"
                "connection" = "connection_name"
              },
            ]
          })
        }

我也尝试过如下绑定

  config_json = jsonencode({
"bindings" : [
  {
    "name" : "eventHubMessages",
    "type" : "eventHubTrigger",
    "direction" : "in",
    "eventHubName" : azurerm_eventhub.azure-xxxx.name,
    "connection" : azurerm_eventhub_namespace.azure-xxxxx.default_primary_connection_string,
    "cardinality" : "many",
    "dataType" : "",
    "consumerGroup" : "$Default"
  }
],
"disabled" : false

})

但是,如下面的屏幕截图所示,仍然没有创建此事件中心连接,并且我找不到任何创建

Event Hub Connection

的 terraform 资源

有没有办法从 terraform 创建此事件中心连接?

azure terraform azure-eventhub
1个回答
0
投票

问题出在连接字符串名称上。确保

app_settings
块中的连接字符串键与
config_json
连接名称值匹配。

 app_settings = {
    "connectionjahn" = azurerm_eventhub_namespace.example.default_primary_connection_string
  }
config_json = jsonencode({
            "bindings" = [
              {
                "authLevel" = "function"
                "direction" = "in"
                "name"       = "req"
                "type"       = "eventHubTrigger"
                "connection" = "connectionjahn"
              }
            ]
          })

注意:连接名称

"connectionjahn"
在上面提供的两个代码片段中保持一致。相应地修改您的代码。

完整的terraform代码如下:

provider "azurerm"{
    features{}
}
data "azurerm_resource_group" "example" {
  name     = "xxxx"
  }
resource "azurerm_storage_account" "example" {
  name                     = "functionsapptejstsa"
  resource_group_name      = data.azurerm_resource_group.example.name
  location                 = data.azurerm_resource_group.example.location
  account_tier             = "Standard"
  account_replication_type = "LRS"
}
 resource "azurerm_service_plan" "example-linux-serviceplan" {
      name                = "linux-service-planj"
      resource_group_name = data.azurerm_resource_group.example.name
      location            = data.azurerm_resource_group.example.location
      os_type             = "Linux"
      sku_name            = "S1"
    }
 resource "azurerm_eventhub_namespace" "example" {
    name                = "examplejanamespace"
    location            = data.azurerm_resource_group.example.location
    resource_group_name = data.azurerm_resource_group.example.name
    sku                 = "Standard"
    capacity            = 2
 }
 
 resource "azurerm_linux_function_app" "example-linux-app" {
      name                = "example-linux-appja"
      resource_group_name = data.azurerm_resource_group.example.name
      location            = data.azurerm_resource_group.example.location
      service_plan_id     = azurerm_service_plan.example-linux-serviceplan.id
      storage_account_name       = azurerm_storage_account.example.name
      storage_account_access_key = azurerm_storage_account.example.primary_access_key
 
  app_settings = {
    "connectionjahn" = azurerm_eventhub_namespace.example.default_primary_connection_string
  }
      site_config {
        application_stack {
          python_version = "3.11"
        }
      }
    }
    resource "azurerm_function_app_function" "example" {
          name            = "example-function-apjahfunction"
          function_app_id = azurerm_linux_function_app.example-linux-app.id
          language        = "Python"
          test_data = jsonencode({
            "name" = "Azure"
          })
          config_json = jsonencode({
            "bindings" = [
              {
                "authLevel" = "function"
                "direction" = "in"
                "name"       = "req"
                "type"       = "eventHubTrigger"
                "connection" = "connectionjahn"
              }
            ]
          })
        }

部署成功:

enter image description here

enter image description here

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.