X
Popular Searches

How to Use Serverless Azure Functions

Digital Ocean logo

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.

Click on "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

Enter in values

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.

Create a new storage account to hold the function's output data

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

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.”

Verify everything is accurate, then click "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.

Under "Functions," click "Add" and choose 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

Create a new functi

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.

Click on the function HttpTrigger1, then click on the "Code + Test" section

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"
}

Click on "Run" to verify the code works

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

The correct output is shown 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.

Click on the "Get Function" URL button

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=

The weather data is returned

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!

Adam Bertram Adam Bertram
Adam Bertram is a 20+ year veteran of IT and an experienced online business professional. He’s a consultant, Microsoft MVP, blogger, trainer, published author and content marketer for multiple technology companies. Catch up on Adam’s articles at adamtheautomator.com, connect on LinkedIn, or follow him on Twitter at @adbertram. Read Full Bio »

The above article may contain affiliate links, which help support CloudSavvy IT.