Azure Terraform:外部数据库产生奇怪的错误

r8xiu3jd  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(155)

main.tf

  1. terraform {
  2. required_version = ">= 1.5.5"
  3. required_providers {
  4. azurerm = {
  5. source = "hashicorp/azurerm"
  6. version = ">=3.69.0"
  7. }
  8. }
  9. }
  10. provider "azurerm" {
  11. features {}
  12. skip_provider_registration = true
  13. subscription_id = "xxxx-xxxxx-xxxxx-xxxxx"
  14. }
  15. provider "azurerm" {
  16. features {}
  17. alias = "sub-fits-55070-dev-01"
  18. skip_provider_registration = true
  19. subscription_id = "xxxx-xxxxx-xxxxx-xxxxx"
  20. }
  21. module "function_app_zis" {
  22. # checkov:skip=CKV_TF_1:Skipped "Ensure Terraform module sources use a commit hash"
  23. providers = {
  24. azurerm = azurerm.sub-fits-55070-dev-01
  25. }
  26. source = "./.."
  27. resource_group_name = "resourcegroup238495723985"
  28. function_app_name = "myfunctionzis238495723985"
  29. location = "westeurope"
  30. use_event_hub_trigger = false
  31. subnet_id = "/subscriptions/xxxx-xxxxx-xxxxx-xxxxx/resourceGroups/rg-network/providers/Microsoft.Network/virtualNetworks/vnet-001/subnets/snet-002"
  32. zip_file_name = "./functionapp/httptrigger/functionapp.zip"
  33. }
  34. resource "azurerm_resource_group" "this" {
  35. provider = azurerm.sub-fits-55070-dev-01
  36. name = "rg-fixture"
  37. location = "westeurope"
  38. }
  39. data "external" "function_key" {
  40. program = ["bash", "${path.module}/get_function_key.sh"]
  41. count = module.function_app_zis.function_app_name != "" ? 1 : 0
  42. depends_on = [module.function_app_zis]
  43. }
  44. resource "azurerm_monitor_action_group" "example" {
  45. provider = azurerm.sub-fits-55070-dev-01
  46. name = "test1mag"
  47. resource_group_name = azurerm_resource_group.this.name
  48. short_name = "auntmary"
  49. email_receiver {
  50. name = "test1email"
  51. email_address = "[email protected]"
  52. use_common_alert_schema = true
  53. }
  54. azure_function_receiver {
  55. name = "test1function"
  56. function_app_resource_id = module.function_app_zis.function_app_id
  57. http_trigger_url = "https://${module.function_app_zis.trigger_url}/api/httptrigger?code=${data.external.function_key[0].result["function_key"]}"
  58. function_name = "httptrigger"
  59. use_common_alert_schema = true
  60. }
  61. depends_on = [data.external.function_key]
  62. }
  63. output "function_app_http_trigger_url" {
  64. value = "https://${module.function_app_zis.trigger_url}/api/httptrigger?code=${data.external.function_key[0].result["function_key"]}"
  65. }

字符串

get_function_key.sh

  1. #!/bin/bash
  2. APP_NAME="func-myfunctionzis238495723985"
  3. RESOURCE_GROUP="resourcegroup238495723985"
  4. SUBSCRIPTION="sub-fits-55070-dev-01"
  5. FUNCTION_NAME="httptrigger"
  6. # Check if the function app is deployed
  7. for i in {1..30}; do
  8. FUNCTION_KEY=$(az functionapp function keys list --subscription $SUBSCRIPTION --resource-group $RESOURCE_GROUP --name $APP_NAME --function-name $FUNCTION_NAME --query default --output tsv)
  9. if [ "$FUNCTION_KEY" != "" ]; then
  10. echo "{\"function_key\": \"$FUNCTION_KEY\"}"
  11. break
  12. fi
  13. done


调试输出
第1次运行输出:

  1. Error: Unexpected External Program Results
  2. with data.external.function_key[0],
  3. on main.tf line 138, in data "external" "function_key":
  4. 138: program = ["bash", "${path.module}/get_function_key.sh"]
  5. The data source received unexpected results after executing the program.
  6. Program output must be a JSON encoded map of string keys and string values.
  7. If the error is unclear, the output can be viewed by enabling Terraform's
  8. │ logging at TRACE level. Terraform documentation on logging:
  9. │ https://www.terraform.io/internals/debugging
  10. │ Program: /usr/bin/bash
  11. │ Result Error: unexpected end of JSON input


第2次运行输出:

  1. Error: Unexpected External Program Results
  2. with data.external.function_key[0],
  3. on main.tf line 138, in data "external" "function_key":
  4. 138: program = ["bash", "${path.module}/get_function_key.sh"]
  5. The data source received unexpected results after executing the program.
  6. Program output must be a JSON encoded map of string keys and string values.
  7. If the error is unclear, the output can be viewed by enabling Terraform's
  8. │ logging at TRACE level. Terraform documentation on logging:
  9. │ https://www.terraform.io/internals/debugging
  10. │ Program: /usr/bin/bash
  11. │ Result Error: invalid character '{' after top-level value


第3次运行输出:无错误
我正在通过Azure DevOps / terraform在Azure中部署一个函数应用程序。
由于资源azurerm_Linux_function_app没有函数的函数键的输出,这是操作组中所需要的,因为否则函数不会通过操作组触发,我必须使用一个技巧将函数键放入操作组。
正如你所看到的,我尝试用az查询获取功能键,并使用此输出将其传递到操作组中。
当我运行这个时,在第一次运行中,我得到了上面显示的输出,这表明没有返回JSON内容。
在第二次运行时,我在shell脚本中添加了两行:

  1. az login --service-principal -u ... -p ... --tenant ...
  2. az account set --subscription "sub-fits-55070-dev-01"


当我运行这个的时候,我在运行2中得到了上面显示的错误消息。这向我表明,有一个输出**,但是terraform不能正确处理这个输出。
在第三次运行中,我将shell脚本更改回运行1时的脚本,这意味着再次删除运行2时添加的两行代码。
现在,第三次运行给了我正确的输出,并创建了操作组。
这种行为很奇怪,因为在第三次运行中,我运行的文件与第一次运行中使用的文件完全相同。但是如果没有az login命令,它就不起作用。
这是怎么了?
除此之外,除了我正在使用的方法之外,还有其他方法可以将功能键作为输出吗?

zysjyyx4

zysjyyx41#

看起来问题在于如何在bash循环中返回JSON负载。(您需要将其作为单个JSON对象返回,而不是多个对象)。如果您简化了get_function_key. sh的返回,则可以复制此脚本.如果我们将该脚本更改为:

  1. #!/bin/bash
  2. echo "{\"function_key\": \"BATMAN\"}"
  3. echo "{\"function_key2\": \"BATMAN\"}"

字符串
脚本输出如下所示:

  1. #sh get_function_key.sh
  2. {"function_key": "BATMAN"}
  3. {"function_key2": "BATMAN"}


这将返回与您看到的相同的错误消息:

  1. on main.tf line 2, in data "external" "function_key":
  2. 2: program = ["bash", "get_function_key.sh"]
  3. The data source received unexpected results after executing the program.
  4. Program output must be a JSON encoded map of string keys and string values.
  5. If the error is unclear, the output can be viewed by enabling Terraform's logging at TRACE level. Terraform documentation on logging:
  6. │ https://www.terraform.io/internals/debugging


如果您更改脚本以更改返回JSON数据的方式,例如:

  1. #!/bin/bash
  2. echo "{\"function_key1\": \"BATMAN\", \"function_key2\": \"ROBIN\"}"


输出如下:

  1. sh get_function_key.sh
  2. {"function_key1": "BATMAN", "function_key2": "ROBIN"}


Terraform对您返回的有效负载非常满意,您可以利用它。因此,请修改脚本中构建返回JSON对象的方式-不要像现在这样返回多个对象,构建单个对象。
祝你好运!

展开查看全部

相关问题