PlagTag
PlagTag

Reputation: 6429

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.

Running terraform apply works fine and the Service Bus looks good and works. In the Azure portal the function seems to be running, but it complains that it can not find the ServiceBusConnection.

I defined it via the following Terraform declaration:

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:

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. 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. This is the correct snipped replacing the name with primary_connection_string.

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
  }

}

Upvotes: 1

Views: 1832

Answers (1)

Heye
Heye

Reputation: 623

You are setting the ServiceBusConnection value to the name of the authorization rule. 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.

Upvotes: 3

Related Questions