Search code examples
azure-logic-appsterraform-provider-azure

terraform azure logic app not importing workflow_parameters that has a $ in it


I am trying to import an 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                = "test-logic"    
    location            = "canadacentral" 
    resource_group_name = "test-rg"       
    enabled             = true
    workflow_parameters = {
      connection = {
        defaultValue = {}
        type         = "Object" 
      }
    }
    logic_app_workflow_tags = {
      applicationName = "Logic App workflow"
    }
  }
}

I am getting the following output when I run terraform plan after importing the resource, I want my tfvars to able to not create/destroy/change anything, I am trying to import Parameter $connection , the input I am giving doesn't import it but instead destroys and creates a new parameter connection? enter image description here


Solution

  • I tried to import the $connections using ".tfvars" and I successfully got the output you're looking for.

    To achieve this configuration my terraform files go with two cases.

    • with lifecycle module.
    • without a lifecycle module.

    Let's check one by one each!

    1. Without a life cycle module

    my main.tf:

    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"]
    
    }
    

    my "vars" file goes as follows.

    ".tfvars"

    logic_app_workflows = {
      "logic_app_workflow1" = {
        name                = "test-logicvk"    
        location            = "canadacentral" 
        resource_group_name = "v-bolliv"       
        enabled             = true
        workflow_parameters = {
          "$connections" = {
            defaultValue = {}
            type         = "Object" 
          }
          
          "connection" = {
            defaultValue = {}
            type         = "Object" 
          }
        }
        logic_app_workflow_tags = {
          applicationName = "Logic App workflow"
        }
      }
    }
    

    for the script mentioned the output is

    Output:

    while doing `terraform apply

    enter image description here

    now I make some changes in ".tfvars" and we can see the changes takes place in the state file as follows.

    Updated "tfvars"

    logic_app_workflows = {
      "logic_app_workflow1" = {
        name                = "test-logicvk"    
        location            = "canadacentral" 
        resource_group_name = "v-bolliv"       
        enabled             = true
        workflow_parameters = {  
          "connection" = {
            defaultValue = {}
            type         = "Object" 
          }
        }
        logic_app_workflow_tags = {
          applicationName = "Logic App workflow"
        }
      }
    }
    

    Output:

    This is when I Execute the terraform plan

    enter image description here

    This is the error mentioned in the query. In order to overcome it while tracking the changes in the state file and also want changes to be implemented in the resource we need to use "lifecycle_module" in main.tf

    1. With lifecycle module:

    my main.tf goes as follows for the same changes mentioned above in updated "tfvars".

    main.tf

    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"]
    
     lifecycle {
        ignore_changes = [
          workflow_parameters,
          tags,
        ]
      }
    }
    

    now with the updated "tfvars" mentioned above the output will go as follows.

    Output:

    This is when I run terraform plan & terraform apply enter image description here

    enter image description here

    Here the resources are provisioned with necessary changes but not tracked in the state file.

    This is how we can pass "$connections" parameters and by adding lifecycle modules in the main.tf we can skip changes in the state file and will be able to make parameter changes in resources.