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:
- Add
monthDays = [-1]
toazurerm_logic_app_trigger_custom
- 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)))"
}]