Azure Automation: Use a nested ARM template as an user defined function to bypass the start time issue of schedules
Lately I am busy with Azure Automation. In terms of ARM templates I run into a number of issues with this product. See also my previous blog postings about this. This time too I had to look for another workaround. In this case it concerns the start time of a schedule. This must be at least 5 minutes in the future for the product group each time you do an ARM deployment for the schedule object. This resource is also not complying to the idempotency rules of ARM template.
My workaround is to mark the date part of the startTime (for daily, weekly, montly recurring schedules) in my parameter file with 'GEN-DATE' placeholder and my ARM template will generate each time the correct date if I do the deploy. To replace the placeholder with the correct value I first thought to use an user defined function for this purpose. After reading the documentation, I could conclude that this functionality did not match what I needed. An user defined function consist of input parameters and output parameters but lacks the usage of variables. Because I have to calculate a few things to determine the correct date I need the usage of variables.
My solution is based on the usage of a nested ARM template which acts as an user defined function. The template calculates the correct start time in the variable section of the ARM template and will return the result in the outputs section. The next step in my ARM pipeline will reference that value and use it as the start time of the schedule.
The ARM template I created needs the start time as a parameter with the GEN-DATE placeholder in it. In the variable section I calculate if we can use the date of today or we need to start the schedule tommorow. Based on the current time this decession will be made. The current time will be corrected to reflect the UTC offset of the start time and we will give the script the minimal amount of 5 minutes in the future. To be save I decided to use a start time of minimal 7 minutes in the future because the deployment proces also needs some time to do the deployment. That value is configured via the default value of the addTimeForDeployment parameter.
{ "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#", "contentVersion": "1.0.0.0", "parameters": { "startTime": { "type": "string", "metadata": { "description": "The start time of the Automation schedule.", "remarks": "Add 'GEN-DATE' at the place of the date part of the schedule. For example: 'GEN-DATET16:00:00+01:00'." } }, "addTimeForDeployment": { "type": "string", "defaultValue": "PT7M", "metadata": { "description": "The number of minutes to add to the compare time to determine if we use the date of today or tommorow.", "remarks": "Azure Automation requires that the schedule time is minimal 5 minutes in the future but you wil also need some additional time for the actual deployment." } }, "currentTime": { "type": "string", "defaultValue": "[utcNow('u')]" } }, "variables": { "today": "[substring(parameters('currentTime'), 0, 10)]", "tomorrow": "[substring(dateTimeAdd(parameters('currentTime'), 'P1D'), 0, 10)]", "scheduleTime": "[if(contains(parameters('startTime'), 'GEN-DATE'), skip(parameters('startTime'), length('GEN-DATE')), skip(parameters('startTime'), 10))]", "scheduleHour": "[substring(variables('scheduleTime'), 1, 2)]", "scheduleMinutes": "[substring(variables('scheduleTime'), 4, 2)]", "scheduleOffsetSymbol": "[substring(variables('scheduleTime'), 9, 1)]", "scheduleOffsetHour": "[substring(variables('scheduleTime'), 10, 2)]", "scheduleOffsetMinutes": "[substring(variables('scheduleTime'), 13, 2)]", "hourCorrection": "[concat(if(equals(variables('scheduleOffsetSymbol'), '-'), '-', ''), 'PT', variables('scheduleOffsetHour'), 'H')]", "minutesCorrection": "[concat(if(equals(variables('scheduleOffsetSymbol'), '-'), '-', ''), 'PT', variables('scheduleOffsetMinutes'), 'M')]", "currentTimeByOffsetHour": "[dateTimeAdd(parameters('currentTime'), variables('hourCorrection'))]", "currentTimeByOffset": "[dateTimeAdd(variables('currentTimeByOffsetHour'), variables('minutesCorrection'))]", "compareTime": "[dateTimeAdd(variables('currentTimeByOffset'), parameters('addTimeForDeployment'))]", "compareHour": "[substring(variables('compareTime'), 11, 2)]", "compareMinutes": "[substring(variables('compareTime'), 14, 2)]", "isCurrentHour": "[equals(variables('scheduleHour'), variables('compareHour'))]", "isHourInFuture": "[greater(variables('scheduleHour'), variables('compareHour'))]", "isMinutesInFuture": "[greaterOrEquals(variables('scheduleMinutes'), variables('compareMinutes'))]", "date": "[if(variables('isHourInFuture'), variables('today'), if(and(variables('isCurrentHour'), variables('isMinutesInFuture')), variables('today'), variables('tomorrow')))]", "newStartTime": "[concat(variables('date'), variables('scheduleTime'))]" }, "resources": [], "outputs": { "startTime": { "value": "[if(contains(parameters('startTime'), 'GEN-DATE'), variables('newStartTime'), parameters('startTime'))]", "type": "string" } } }
In the ARM pipeline you can reference the output value via a reference like this: reference(<deploymentName>.outputs.startTime.value
Comments
Post a Comment