0

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: enter image description here

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: enter image description here

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 : enter image description here

Edit 3: I have tried

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

But I am getting the following error for terraform plan after importing: enter image description here

lviswanath
  • 21
  • 4
  • The error message is indeed accurate that `[]` cannot be encoded into JSON because it is not a valid type for JSON format. However, it is unclear what the objective is here. Please elaborate on what you are trying to accomplish so that we can assist. – Matthew Schuchard Jun 28 '23 at 20:42
  • @MattSchuchard I am trying to import an existing logic app workflow and link it to this module , I have added in an edit with the same information. – lviswanath Jun 29 '23 at 14:43

1 Answers1

0

You can see it going wrong in the plan output:

plan output

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:

tfvars complete

alex
  • 240
  • 6