Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

huangapple go评论63阅读模式
英文:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

问题

我正在尝试使用Terraform创建Azure逻辑应用工作流。我的模块定义如下:

resource "azurerm_logic_app_workflow" "logic_app_workflow" {
  for_each            = var.logic_app_workflows
  name                = each.value["name"]
  location            = each.value["location"]
  resource_group_name = each.value["resource_group_name"]
  enabled             = each.value["enabled"]
  workflow_parameters = { for k, v in each.value["workflow_parameters"] : k => jsonencode(v) }
  tags                = each.value["logic_app_workflow_tags"]
}

我的变量定义如下:

variable "logic_app_workflows" {
  type = map(object({
    name                    = string
    location                = string
    resource_group_name     = string
    enabled                 = bool
    workflow_parameters     = any
    logic_app_workflow_tags = map(string)
  }))
}

我的tfvars文件如下:

logic_app_workflows = {
  logic_app_workflow1 = {
    name                = "logic-app-01"
    location            = "canadacentral"
    resource_group_name = "rg-01"
    enabled             = true
    workflow_parameters = {
      defaultValue = []
      type = "Object"
    }
    logic_app_workflow_tags = {
      application = "Logic App workflow"
    }
  }
}

当我运行apply时,我收到以下错误消息:

expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

我了解到错误是由以下行引起的:

workflow_parameters = { for k, v in each.value["workflow_parameters"] : k => jsonencode(v) }

我尝试将jsonencode替换为tostring以查看是否有所帮助,但收到以下错误:

Invalid value for "v" parameter: cannot convert tuple to string.

编辑:
我尝试使用以下代码,但问题未解决。

workflow_parameters = {
  defaultValue = []
  type = {type = "Object";}
}

错误截图:
Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

编辑2:
我正在尝试导入现有的逻辑应用工作流,在将资源导入到此模块后,terraform plan的输出如下:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

编辑3:
我尝试了以下内容:

workflow_parameters = {
  connection = {
    defaultValue = {}
    type = {type = "Object";}
  }
}

但在导入后运行terraform plan时,我收到以下错误:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

英文:

I am trying to create a Azure logic App workfow using terraform.
My module is defined as below :

resource "azurerm_logic_app_workflow" "logic_app_workflow" {
  for_each            = var.logic_app_workflows
  name                = each.value["name"]
  location            = each.value["location"]
  resource_group_name = each.value["resource_group_name"]
  enabled             = each.value["enabled"]
  workflow_parameters = { for k,v in each.value["workflow_parameters"] : k =>jsonencode(v) } 
  tags               = each.value["logic_app_workflow_tags"]
}

and my variable is defined as below :

variable "logic_app_workflows" {
  type = map(object({
    name                    = string
    location                = string
    resource_group_name     = string
    enabled                 = bool
    workflow_parameters = any
    logic_app_workflow_tags = map(string)
  }))
}

My tfvars file looks like this :

logic_app_workflows = {
  logic_app_workflow1 = {
    name                = "logic-app-01"     
    location            = "canadacentral" 
    resource_group_name = "rg-01"       
    enabled             = true
    workflow_parameters = {
      defaultValue = []
      type = "Object"
    }
    logic_app_workflow_tags = {
      application = "Logic App workflow"
    }
  }
}

I am receiving the below error when I run apply:

expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

A screenshot of the same:
Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

I understand that the error is caused by

workflow_parameters = { for k,v in each.value["workflow_parameters"] : k =>jsonencode(v) } 

I tried replacing jsonencode with tostring to see if that might help but got the following error :

Invalid value for "v" parameter: cannot convert tuple to string.

Edit:
I tried using the following code but it did not fix the issue.

workflow_parameters = {
  defaultValue = []
  type = {type = "Object"}
}

Error screenshot:
Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

Edit 2: I am trying to import an existing logic app workflow, after importing the resource to this module the below screenshot is what terraform plan looks like :
Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

Edit 3: I have tried

workflow_parameters = {
  connection = {
    defaultValue = {}
    type = {type = "Object"}
  }
}

But I am getting the following error for terraform plan after importing:
Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

答案1

得分: 0

你可以在计划输出中看到问题:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

您将此数据作为对象发送,但这不是有效的 JSON 字符串,应该是这样的。

根据文档,应该是:

workflow_parameters -(可选)指定用于此 Logic App 工作流的参数定义的键值对映射。键是参数名称,值是参数定义的 JSON 编码字符串(请参阅:https://docs.microsoft.com/azure/logic-apps/logic-apps-workflow-definition-language#parameters)。

因此,最终应该像这样:

logic_app_workflows = {
  logic_app_workflow1 = {
    name                = "logic-app-01"
    location            = "canadacentral"
    resource_group_name = "rg-01"
    enabled             = true
    workflow_parameters = {
      Search_tweets = {
        type = "object"
        defaultValue = {}
      }
    }
    logic_app_workflow_tags = {
      application = "Logic App workflow"
    }
  }
}

您的代码大部分是正确的,但类型必须是 "object",defaultValue 必须是 {},这定义了一个空对象,而您使用的是 [],这是一个空数组。

使用这个 tfvars 文件,我能够创建资源:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

英文:

You can see it going wrong in the plan output:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

You are sending this data as an object, and that is not a valid JSON string as it should be.

From the docs it should be:

workflow_parameters - (Optional) Specifies a map of Key-Value pairs of the Parameter Definitions to use for this Logic App Workflow. The key is the parameter name, and the value is a JSON encoded string of the parameter definition (see: https://docs.microsoft.com/azure/logic-apps/logic-apps-workflow-definition-language#parameters).

So it needs to be something like this in the end;

logic_app_workflows = {
  logic_app_workflow1 = {
    name                = "logic-app-01"
    location            = "canadacentral"
    resource_group_name = "rg-01"
    enabled             = true
    workflow_parameters = {
      Search_tweets = {
        type = "object"
        defaultValue = {}
      }
    }
    logic_app_workflow_tags = {
      application = "Logic App workflow"
    }
  }
}

Your code is mostly correct but the type must be "object" and the defaultValue must be {} this defines an empty object and you are using [] this is an empty array

With this tfvars I am able to create the resource:

Terraform logic app throwing error : expanding `workflow_parameters`: json: cannot unmarshal array into Go value of type map[string]interface {}

huangapple
  • 本文由 发表于 2023年6月29日 03:48:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76576294.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定