قالب وردپرس درنا توس
Home / Tips and Tricks / Using Serverless Azure Features – CloudSavvy IT

Using Serverless Azure Features – CloudSavvy IT



Digital ocean logo

In the past few years, serverless has become one of the most talked about technologies. While not truly serverless, serverless options abstract administration away from the user to a managed and hosted service.

One of the most exciting skills is writing code that can then be run on serverless instances. Azure Functions offers different runtimes, languages ​​and even Docker container support. All you have to do is choose the language you want to write in, provide the code, and use the results.

Deploy the Azure Function App

First, navigate to “Function App”

; in the Azure portal. There click on the “Create function app” button.

Click on

Enter the values ​​that make the most sense for your environment and needs. In this example we are using the PowerShell Core 7.0 runtime.

  • Function App Name: lc-test-function
  • Publish: Code
  • Maturity stack: PowerShell Core
  • execution: 7.0
  • region: Eastern USA

Enter values

On the Hosting tab, we will create a new storage account that will store the output data of the function. Since this is PowerShell, the runtime only supports Windows, although PowerShell 7 is cross-platform. Finally, we will use the consumption plan for this Azure function.

Create a new storage account for the function output data

For monitoring, we create a new Application Insights configuration and enable the Monitoring option.

Create a new Application Insights configuration and enable the monitoring option

We’ll skip the tagging in this article and move on to Review and Build. Once you are here, double-check that everything looks correct, then click “Create”.

Check that everything is correct, then click

Create an Azure Function Code

This article shows how to do this with an HTTP trigger function. Navigate to your app function and click on the “Functions” area. There click on “Add” and select the HTTP trigger template.

Under

We will stick to the default values ​​for the function name and authorization level. The different authorization levels correspond to the following:

  • Function: Uses a specific function key
  • Administrator: Uses the master key for the environment
  • Anonymous: Does not require authorization, restricts the functions

Create a new function

Change function code

Click on the newly created function HttpTrigger1and click on the “Code + Test” section. Here is an inline code editor that you can use to test and run your function code. The following values ​​are the default configurations.

Click the HttpTrigger1 function and then click the

What can you do with Function Apps? It can be used for many purposes, and to demonstrate one of them, we’ll pass a specific location ID from a calling PowerShell which in turn calls the Azure function to run against a REST API and get weather data. The changes we are making to the default configuration are:

  • To change $name to $location
  • Create a Invoke-RestMethod Go to the metaweather.com API
  • Format the 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"    = ""
		"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 changes. We will test / run this code to see if it works as expected. Once you hit “Test / Run” a window will pop up where we can change the input so that it meets the code’s expectations.

{
  "location": "2379574"
}

Click on

After running the code, you’ll see the correct output on the Output tab under the content of the HTTP response.

The correct output is shown on the Output tab under the content of the HTTP response

Triggering the function via external PowerShell

First, click the Get Function URL button to see a URL that you can use to get to the Function app.

click on

Use in a PowerShell session Invoke-RestMethod Function for calling the HTTP trigger and passing the location Variable that the script expects. As you can see from the output, the weather data is returned as expected.

$Params = @{
	"URI"         = ''
  "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 for building server-less functions in multiple languages. Freeing up the need to focus on the environment and all of its dependencies, developers can quickly step through code and design to create unique functionality!


Source link