×
  1. Home
  2. Knowledge Base
  3. Automation
  4. Automation Course Part 6 – Azure Functions

Automation Course Part 6 – Azure Functions

Welcome to the next article in our API development series! In this article, we will discuss how to build a Timer-Triggered Azure Function App to execute an API call on a recurring basis using PowerShell.

Azure Functions is a serverless compute service that enables you to run code on-demand without having to provision or manage infrastructure. One of the key features of Azure Functions is the ability to trigger code based on a timer, which can be useful for executing API calls on a recurring basis.
Here are the steps to build a Timer-Triggered Azure Function App to execute an API call:

Step 1: Create an Azure Function App

The first step is to create an Azure Function App. This can be done in the Azure Portal or using PowerShell. For example, to create an Azure Function App in PowerShell, you can use the following command: note that they will need a subscription first. If the organization you work for is a Microsoft Partner, you may be eligible for a Microsoft Visual Studio Subscription that includes Azure credit.

New-AzFunctionApp -ResourceGroupName <resource-group-name> -Name <function-app-name> -StorageAccountName <storage-account-name> -ConsumptionPlanLocation <location>

Step 2: Create a Timer Triggered Function

Next, create a Timer Triggered Function in the Azure Function App. This can be done in the Azure Portal or using PowerShell. For example, to create a Timer Triggered Function in PowerShell, you can use the following command:

New-AzFunction -Name <function-name> -ResourceGroupName <resource-group-name> -FunctionAppName <function-app-name> -TemplateFile TimerTrigger -Schedule "0 */5 * * * *" -Language PowerShell

This will create a Timer Triggered Function that executes every 5 minutes. Explain cron/link

Step 3: Add API Call to Function

Now, add the API call that you want to execute to the Function. This can be done using PowerShell. For example, to execute a GET request to an API endpoint, you can use the following command:

$uri = " https://api.example.com/data"
$headers = @{ "Authorization" = "Bearer <access-token>" }
$response = Invoke-RestMethod -Uri $uri -Headers $headers -Method Get

This code will execute a GET request to the API endpoint and store the response in the $response variable.

Step 4: Schedule the Function to Execute

Finally, schedule the Function to execute on a recurring basis. This can be done using PowerShell. For example, to schedule the Function to execute every 5 minutes, you can use the following command:

Set-AzFunctionAppTrigger -Name <function-name> -ResourceGroupName <resource-group-name> -FunctionAppName <function-app-name> -Schedule "0 */5 * * * *"

This will schedule the Function to execute every 5 minutes.

It’s important to note that you should never store secrets or sensitive information in plain text in your code. There are ways to secure the secrets, such as using Azure Key Vault, and we recommend that you explore those options to ensure that your application is secure.

Related Articles