Create an Azure function application and deploy functions that starts or stops virtual machines in the specified resource group, subscription, or by tag on a schedule.
Before running this sample, you must have the following:
Install Azure Core Tools version 2.x
Install the Azure CLI
Run the following PowerShell command and specify the value for the function application name in the TemplateParameterObject hashtable.
New-AzResourceGroup-Name <resource group name>-Location <location>New-AzResourceGroupDeployment-ResourceGroupName <resource group name>-TemplateParameterObject @{"functionAppName"="<your function app name>"} -TemplateUri "https://raw.githubusercontent.com/Azure/azure-quickstart-templates/master/101-functions-managed-identity/azuredeploy.json"-verboseThis should create a new resource group with a function application and a managed service identity enabled. The id of the service principal for the MSI should be returned as an output from the deployment.
Example: principalId String cac1fa06-2ad8-437d-99f6-b75edaae2921
Grant the managed service identity contributor access to the subscription or resource group so it can perform actions
The below command sets the access at the subscription level.
$Context=Get-AzContextNew-AzRoleAssignment-ObjectId <principalId>-RoleDefinitionName Contributor -Scope "/subscriptions/$($Context.Subscription)"Download the repository files or clone to local machine.
Change to the PowerShell/StartStopVMOnTimer directory.
func azure functionapp fetch-app-settings <function app name>This should create a local.settings.json file in the StartStopVMOnTimer directory beside the host.json with the settings from the Azure function app.
Start the function with the following command
func startYou can then call a trigger function by performing a post against the function on the admin api. Open up another Powershell console session and run:
Invoke-RestMethod"http://localhost:7071/admin/functions/StartVMOnTimer"-Method post -Body '{}'-ContentType "application/json"Modify the values for each of the below variables in run.ps1 as needed.
# Specify the VMs that you want to stop. Modify or comment out below based on which VMs to check.$VMResourceGroupName="Contoso"#$VMName = "ContosoVM1"#$TagName = "AutomaticallyStart"Modify the start and stop time in the function.json file. They are currently set to 8am and 8pm UTC. You can change the timezone by modifying the application setting WEBSITE_TIME_ZONE. You can also pass in a parameter 'timezone' to the above ARM template that was used to create the function application if you want a different timezone so that daylight savings time will be honored.
{
"disabled": false,
"bindings": [
{
"name": "Timer",
"type": "timerTrigger",
"direction": "in",
"schedule": "0 0 20 * * *"
}
]
}func azure functionapp publish <function app name>
