Setup "monthDays" property in case of trigger recurrence Logic App with terraform

I would like to define with terraform code a logic app trigger definition so that the trigger runs only once on the last day of each month.

I have found the following code snippet on related Microsoft site.

"triggers": {
    "Recurrence": {
        "recurrence": {
            "frequency": "Month",
            "interval": 1,
            "schedule": {
                "monthDays": [-1]
            }
        },
        "type": "Recurrence"
    }
}

If I type it manually into Logic app recurrent step, Its working without error. It should look like this code snippet but I cannot find the equivalent in terraform.

resource "azurerm_logic_app_trigger_recurrence" "trigger_recurrence" {
  name         = "run_only_last_day"
  logic_app_id = azurerm_logic_app_workflow.example.id
  frequency    = "Month"
  interval     = 1
  start_time   = var.start_time
  time_zone    = var.time_zone

  dynamic "schedule" {
    for_each = var.use_advanced_scheduling ? [true] : []
    content {
      on_these_days    = var.on_these_days
      at_these_hours   = var.at_these_hours
      at_these_minutes = var.at_these_minutes
      
    }
  }

Is it possible that monthDays property has been forgotten to be implemented for azurerm provider by Terraform hashicorp dev team? I didn't find this property in schedule section in the documentation.

UPDATE: As I suspected, the monthDays property hasn't been implemented by azurerm_logic_app_trigger_recurrence yet.

Here is an example workaround until monthDays is ready to use. I have created a custom common template to be able to configure all recurrent related options.

    resource "azurerm_logic_app_trigger_custom" "conditional_trigger_recurrence" {
      name         = "conditional_custom_recurrent_trigger"
      logic_app_id = azurerm_logic_app_workflow.example.id
   
     body = jsonencode({
     conditions = var.condition_expressions,
     recurrence = merge({
      frequency = var.frequency
      interval  = var.interval
      },
      coalesce(var.use_advanced_scheduling ? {
        schedule = merge(
          coalesce(length(var.at_these_hours) > 0 ? {
            hours = var.at_these_hours
          } : null, {}),
          coalesce(length(var.at_these_minutes) > 0 ? {
            minutes = var.at_these_minutes
          } : null, {}),
          coalesce(length(var.on_these_days) > 0 ? {
            weekDays = var.on_these_days
          } : null, {}),
          coalesce(length(var.on_what_day) > 0 ? {
            monthDays = var.on_what_day
          } : null, {})
        )
      } : null, {}),
      coalesce(var.start_time != null ? {
        startTime = var.start_time
      } : null, {}),
      coalesce(var.time_zone != null ? {
        timeZone = var.time_zone
      } : null, {}))
      type = "Recurrence"
     })
   }

Lets use azurerm_logic_app_trigger_custom instead of azurerm_logic_app_trigger_recurrence.

There are two possible solutions available:

  1. Add monthDays = [-1] to azurerm_logic_app_trigger_custom
  2. Add Trigger condition to recurrent logic app action.

The provided template contains both approachs.

Some extra comments to second approach: Body of azurerm_logic_app_trigger_custom can be customized. In this way, You can add Trigger condition to your recurrent based Logic App. There are multiple way to check the current day is the same as the day you are looking for (in our case, this is the last day of the current month.)

Here is an example to check that today is the last day of the month with logic app built-in functions:

condition_expressions = [{
    expression = "@equals(dayOfMonth(utcNow()), dayOfMonth(addDays(startOfMonth(addDays(startOfMonth(utcNow()),31)),-1)))"
}] 

The result is the following: enter image description here

Upvotes: 0

Views: 674

Answers (1)

Jahnavi
Jahnavi

Reputation: 8018

Yes, the months property is currently unavailable in azurerm_logic_app_trigger_recurrence. And start time should be the last day of the coming month as you mentioned.

Set on_these_days = ["LastDayOfMonth"].

So, if you set the on_these_days property to ["LastDayOfMonth"], then it will only trigger on the last day of the current month.

I made a few modifications to your dynamic block and was able to deploy successfully.

dynamic "schedule" {
    for_each = var.use_advanced_scheduling ? [true] : []
    content {
      on_these_days    = ["LastDayOfMonth"]
      at_these_hours   = ["8","9"]
      at_these_minutes = [0,15,30...]
      
    }

I tried sample script with a week frequency including a dynamic schedule block for your reference and it worked as shown:

resource "azurerm_logic_app_workflow" "example" {
  name                = "<workflow>"
  location            = azurerm_resource_group.example.location
  resource_group_name = azurerm_resource_group.example.name
}
provider "azurerm"{
features{}
}

resource "azurerm_logic_app_trigger_recurrence" "trigger_recurrence" {
  name         = "xxxx"
  logic_app_id = azurerm_logic_app_workflow.example.id
  frequency    = "Week"
  interval     = 1
  start_time   = "2023-03-01T00:00:00Z"
  dynamic "schedule" {
    for_each = var.use_advanced_scheduling ? [true] : []
    content {
      on_these_days    = ["Monday","Tuesday"]
      at_these_hours   = ["8","9"]
      at_these_minutes = [0,15,30]
      }
    }
  }

terraform init:

enter image description here

terraform plan:

enter image description here

terraform apply:

enter image description here

Deployed in Azure Portal:

enter image description here

enter image description here

Upvotes: 0

Related Questions