0

I have 10+ actions in my Azure Logic app. I keep the action definitions in a JSON file as the content is pretty long, 1K+ lines.

Most actions depend on each other. So, I need to create the actions in a specific order so that Terraform won't fail. I know that I can use as many azurerm_logic_app_action_custom resources as need and use depends_on argument. However, this is a lot of repeating the same lines.

So, I am wondering if there a way to create the actions using a loop, perhaps for_each or count, to avoid repeating the same code.

I tried both for_each and count but the problem is depends_on doesn't take dynamic resource names.

Update#1:

resource "azurerm_logic_app_action_custom" "this" {
  for_each = local.actions

  name         = each.key
  logic_app_id = azurerm_logic_app_workflow.this.id
  body         = jsonencode(each.value)

  depends_on = [ 
    // here I need to pass the logic app actions that this one
    // depends on, perhaps can use actions in runAfter prop
  ]
}
cahitihac
  • 1
  • 3

1 Answers1

0

To create the multiple actions, you can use a for_each loop and the specify the order of execution using runAfter property existed in azurerm_logic_app_action_custom body.

Modify your code as below:

locals 
{ 
 actions = { 
    action1 = { },
    },
 ....action n
 }
data "azurerm_resource_group" "main"{
name = "Jahnavi"
}
resource "azurerm_logic_app_workflow" "this" {
  name                = "workflowsample"
  location            = data.azurerm_resource_group.main.location
  resource_group_name = data.azurerm_resource_group.main.name
}
resource "azurerm_logic_app_action_custom"  "this" 
{ 
for_each = local.actions name = each.key 
logic_app_id = azurerm_logic_app_workflow.this.id 
body = jsonencode(each.value) 
"runAfter" = [for action in local.actions: azurerm_logic_app_action_custom.this[action].name] 
}

I checked the above code with some random actions and the configuration was validated successfully.

enter image description here

Jahnavi
  • 3,076
  • 1
  • 3
  • 10