How to Use Serverless Azure Functions

0
283

In recent years, serverless has become one of the most talked-about technologies. Though not truly serverless, serverless options abstract the management away from the user to a managed and hosted service.

One of the most exciting abilities is that of writing code to then run on serverless instances. Azure Functions offers several runtimes, languages, and even Docker container support. You merely pick the language you want to write in, deploy the code, and consume the results.

Provisioning Azure Function App

First, navigate to “Function App” within the Azure Portal. Once there, click on the “Create Function App” button.

Enter in the values that make the most sense for your environment and needs. In this example, we are going to use the PowerShell Core 7.0 runtime.

  • Function App name: lc-test-function
  • Publish: Code
  • Runtime stack: PowerShell Core
  • Version: 7.0
  • Region: East US

On the Hosting tab, we are going to create a new storage account to hold the output data from the function. Because this is PowerShell, the runtime only supports Windows, despite PowerShell 7 being cross-platform. Finally, we are using the Consumption plan for this Azure Function.

For Monitoring, we are going to create a new Application Insights configuration and enable the Monitoring option.

We are going to skip tagging in this article, and move on to Review & Create. Once here, verify that everything looks accurate, then click on “Create.”

Creating Azure Function Code

For this article, we are going to demonstrate how this works with an HTTP trigger function. Navigate to your App Function and click on the “Functions” pane. Once there, click on “Add” and choose the HTTP trigger template.

We are going to stick with the default values for the function name and the Authorization Level. The different levels for authorization correspond to the following:

  • Function: Uses a specific function key
  • Admin: Uses the master key for the environment
  • Anonymous: Needs no authorization, limits capabilities

Modifying Function Code

Click on the newly created function HttpTrigger1, and click on the “Code + Test” section. Here you will find an inline code editor that will allow you to test and run your function code. The values below are the default configurations.

So, what can you do with Function Apps? There are many uses, and to demonstrate one of them, we are going to pass in a specific location ID from an invoking PowerShell call, which in turn will call the Azure Function to run against a REST API to get Weather data. The modifications we are making to the default configuration are:

  • Modify $name to $location
  • Create an Invoke-RestMethod call to the metaweather.com API
  • Format results to be sent as a string in the body

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host “PowerShell HTTP trigger function processed a request.”

# Interact with query parameters or the body of the request.
$location = $Request.Query.Location
if (-not $location ) {
$location = $Request.Body.Location
}

$body = “This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.”

if ($location ) {
$Params = @{
“URI” = “<https://www.metaweather.com/api/location/$location/>”
“Method” = ‘GET’
}

$Result = Invoke-RestMethod @Params
$Body = $Result.consolidated_weather | Select-Object applicable_date, weather_state_name, the_temp, humidity
}

# Associate values to output bindings by calling ‘Push-OutputBinding’.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
StatusCode = [HttpStatusCode]::OK
Body = ($Body | Out-String)
})

Click “Save” to make the changes. We will Test/Run this code to verify that it works as expected. Once you click on “Test/Run,” we will be presented with a window that allows us to modify the input to correspond to what the code expects.

{
“location”: “2379574”
}

After running the code, you can see the correct output in the Output tab under the HTTP response content.

Triggering the Function via External PowerShell

First, click on the “Get Function” URL button, which will present you with an URL that you can use to call the Function app.

In a PowerShell session, use the Invoke-RestMethod function to call the HTTP trigger and pass in the location variable that the script is expecting. As you can see from the output, the weather data is returned as expected.

$Params = @{
“URI” = ‘<https://lc-test-function.azurewebsites.net/api/HttpTrigger1?code=Gvm5JRCrd0pPRyqhq2uA83PLg1zFocDI52aBLuCe9APXuPWlpPGHpA==>’
“Method” = ‘POST’
“ContentType” = ‘application/json’
“Body” = (@{
“location” = “2379574”
} | ConvertTo-JSON)
}

Invoke-RestMethod @Params=

Conclusion

Azure Functions is a powerful tool to create serverless functions in several languages. By freeing up a developer to not have to focus on the environment and all of the dependencies, they can quickly iterate on code and design to create unique functionality!