Reputation: 783
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
}
}
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 -1start_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:
monthDays = [-1]
to azurerm_logic_app_trigger_custom
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)))"
}]
Upvotes: 0
Views: 674
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
:
terraform plan
:
terraform apply
:
Deployed in Azure Portal:
Upvotes: 0