I am trying to provision a function inside azure function app using terraform and using event hub trigger as the function trigger.
this is the sample code for function_app that I am provisioning
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"
},
]
})
}
I also tried with binding as below
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
})
However as in below screenshot, still this event hub connection is not created, and I could not find any terraform resource that creates the Event Hub Connection
Is there a way to to create this event hub connection from terraform ?
The issue is with the connection string name. Make sure that the connection string key in app_settings
block matches with the config_json
connection name value.
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"
}
]
})
Note: The connection name "connectionjahn"
remains consistent in both of the above provided code snippets. Modify in your code accordingly.
Complete terraform code is given below:
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"
}
]
})
}
Deployment succeeded: