قالب وردپرس درنا توس
Home / Tips and Tricks / How to use serverless Azure features – CloudSavvy IT

How to use serverless Azure features – CloudSavvy IT



Digital Ocean logo

In recent years, serverless has become one of the most talked about technologies. Even if it is not really serverless, serverless options remove the lead from the user to a managed and hosted service.

One of the most exciting abilities is to write code and then run on serverless instances. Azure Functions offers multiple operating times, languages ​​and even Docker container support. You just select the language you want to write in, distribute the code and consume the results.

Provisioning Azure Function App

First navigate to the “Function App” in the Azure Portal. Once you are there, click on the “Create function app”

; button.

click

Enter the values ​​that are most meaningful to your environment and your needs. In this example, we will use the PowerShell Core 7.0 runtime.

  • Function App name: lc-test-function
  • Publish: Code
  • Runtime stack: PowerShell Core
  • Version: 7.0
  • Area: Eastern United States

Enter values

On the Hosting tab, we will create a new storage account to keep output from the function. Because this is PowerShell, runtime only supports Windows even though PowerShell 7 is a platform platform. Finally, we use the consumption plan for this Azure feature.

Create a new storage account to keep the function's output

For monitoring, we will create a new Application Insights configuration and activate the monitoring option.

Create a new Application Insights configuration and enable the monitoring option

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

Make sure everything is correct, then click

Create Azure feature code

For this article, we will demonstrate how this works with an HTTP trigger feature. Navigate to your app function and click on the “Functions” box. Once there, click “Add” and select the HTTP trigger template.

During

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

  • Work: Uses a specific function key
  • Administration: Uses the master key for the environment
  • Anonymous: Needs no permit, limits capacity

Create a new function

Change function code

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

Click the HttpTrigger1 function, and then click

So, what can you do with Function Apps? There are many uses, and to show one of them we will send a specific location ID from a calling PowerShell call, which in turn will call the Azure feature to run against a REST API to get weather data. The changes we make in the default configuration are:

  • Change $name to $location
  • Create an Invoke-RestMethod calls 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"    = ""
		"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 test / run this code to verify that it works as expected. When you click on “Test / Run” we will get a window that allows us to change the input so that it corresponds to what the code expects.

{
  "location": "2379574"
}

click

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

The correct output is displayed on the Output tab below the HTTP response content

Triggers the function via external PowerShell

First, click the “Get Function” URL button, which displays a URL that you can use to call the Function app.

click

Use one in a PowerShell session Invoke-RestMethod function to call the HTTP trigger and send location variable as 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=

Weather data is returned

Conclusion

Azure Functions is a powerful tool for creating serverless features in multiple languages. By releasing a developer so that they do not have to focus on the environment and all dependencies, they can quickly iterate on code and design to create unique functionality!


Source link