0

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
      
    }
  }
  • I have tried to set on_these_days property because "weekDays": [] is generated by this property but this also throw error. Error: expected schedule.0.on_these_days.0 to be one of [Monday Tuesday Wednesday Thursday Friday Saturday Sunday], got -1
  • I also know that start_time should be the last day of next month, but I have concerns about following runs because it doesn't guarantee that it will trigger only the last day of the current month. (details)

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

Péter Hidvégi
  • 743
  • 6
  • 21

1 Answers1

0

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

Jahnavi
  • 3,076
  • 1
  • 3
  • 10
  • Could you share the generated code from "logic app code view" menu? (Under the designer.) I tried to apply your suggested code but 'on_these_days = ["LastDayOfMonth"]' part has generated nothing. Did you make sure that the required code part has generated? I am not convinced yet :/ – Péter Hidvégi Feb 15 '23 at 15:35
  • I have worked with the sample code with a frequency of 1 week as attached the output screen in the above answer. Can you let me know what is the error you are getting? – Jahnavi Feb 16 '23 at 04:41
  • Theres is no error during the compiling but it does not mean that its going to work as we expected. I see your result, but i'm still not convinced about the last day concept will work as we expect. I see that It will trigger every week, every quarter of an hour between 7 and 9 AM from 2023-03-01. It should trigger once a month at the last day of the month. – Péter Hidvégi Feb 16 '23 at 08:54
  • Have you tried with [custom logic app trigger](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/logic_app_trigger_custom) anytime? – Jahnavi Feb 16 '23 at 09:05
  • Yep, I have already implemented a workaround with custom_trigger. Im going to update the post but its just a workaround. – Péter Hidvégi Feb 16 '23 at 09:18