azure - HTTP 401 Azure Terraform 操作组向 Azure 函数发送日志搜索警报

标签 azure function terraform

我有一个奇怪的问题:

我在 Azure 中有一个操作组,是通过以下 terraform 代码部署的:

resource "azurerm_monitor_action_group" "example" {
  name                = "test1mag"
  resource_group_name = azurerm_resource_group.this.name
  short_name          = "blabla"

  email_receiver {
    name                    = "test1email"
    email_address           = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="385d55595154595c5c4a5d4b4b785d555951544a5d5b5d514e5d4a165b5755" rel="noreferrer noopener nofollow">[email protected]</a>"
    use_common_alert_schema = true
  }
  azure_function_receiver {
    name                     = "test1function"
    function_app_resource_id = module.function_app_zis.function_app_id
    http_trigger_url         = "https://function4711.azurewebsites.net"
    function_name            = "httptrigger"
    use_common_alert_schema  = true
  }
  depends_on = [module.function_app_zis]
}

部署正常,当我进入 Azure 门户并单击“编辑操作组”中的“测试操作组”时,我收到一封电子邮件,这部分绝对没问题。

在函数方面我没有收到错误,它看起来像这样

enter image description here

但是函数应用程序未触发。它什么也没做。

有趣的是:当我在门户中编辑操作类型“功能”的设置时,只需单击“确定”并保存更改,再试一次,它就可以工作了!!!

我实际上没有更改任何内容,只是在门户中确认了 terraform 已经部署的内容。

注意一点,因为我猜这就是问题所在:这个 functionapp 中的函数不是由 terraform 代码部署的,它是通过包含 python 代码的 zip 文件传递​​的。因此,函数的名称(在本例中为“httptrigger”)是 zip 文件中 .py 和 .json 文件所在文件夹的名称。

这也是我在代码中使用这一行的原因

function_name = "httptrigger"

虽然我肯定喜欢将函数名称作为变量而不是字符串传递,但我实际上不知道该怎么做......

我怀疑函数的名称或其他任何内容是通过我手动重新确认操作组中的设置在 Azure 中确认的,并且之前未被正确接受。

有人可以帮忙吗?

我期望函数应用程序是从操作组触发的,但如果使用我的 terraform 代码部署,情况并非如此。

最佳答案

To hand over the function name as a variable instead of a string: -

您可以将 function_name 存储在变量文件中,然后在操作组资源中的 azure_function_receiver block 的 function_name 属性中使用它.

var.tf:

variable "function_name" {
   type=String
   default = "function name"
}

并在 resource block 下的 main.tf 中以 function_name = var.function_name 的形式访问它。

或者在我的环境中,我尝试在代码中使用data block 来访问已经部署的function_app并且它工作得很好。

provider "azurerm" {
    features {}
}
data "azurerm_resource_group" "example"{
    name = "<resourcegroup name>"
}
data "azurerm_windows_function_app" "example" {
    name = "fnappuovh4pk4fceb6"
    resource_group_name = data.azurerm_resource_group.example.name
}
resource "azurerm_monitor_action_group" "example" {
  name                = "test1mag"
  resource_group_name = data.azurerm_resource_group.example.name
  short_name          = "p0action"

  email_receiver {
    name                    = "test1email"
    email_address           = "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="641c1c1c1c1c240309050d084a070b09" rel="noreferrer noopener nofollow">[email protected]</a>"
    use_common_alert_schema = true
  }
  azure_function_receiver {
    name                     = "test1function"
    function_app_resource_id = data.azurerm_windows_function_app.example.id
    http_trigger_url         = "https://fnappuovh4pk4fceb6.azurewebsites.net"
    function_name            = "HttpTrigger1"
    use_common_alert_schema  = true
  }
}

已执行 terraform 初始化、规划并应用,如图所示:

enter image description here

enter image description here

enter image description here

操作组部署成功:

enter image description here

enter image description here

关于azure - HTTP 401 Azure Terraform 操作组向 Azure 函数发送日志搜索警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/77156499/

相关文章:

git - 如何使用 terraform 通过 git 在 azure 中自动部署

google-cloud-platform - 将多个聚合添加到 google_monitoring_alert_policy

c# - ResolveEventHandler只调用一次,成功或失败

powershell - Move-Azure 部署交换服务器花费的时间太长?

c - C中是否允许这种函数调用

function - 在 Powershell 中,如何在函数内设置变量值并使该值在父作用域中可用?

terraform - 不使用 Terraform 文件配置器将本地文件部署到实例

azure - 在 Windows Azure 中扩展 IO 性能

Powershell显示存储帐户不存在

c++ - 如何在类函数中使用 "..."作为变量