- Consulting
- Training
- Partners
- About Us
x
Azure Function App is an app service that serves as the host construct where functions will be executed and let businesses group functions as a logic unit for easier management, deployment, and sharing of resources.
The Azure cloud provides this exciting service which is a serverless solution that allows you to write less code, maintain less infrastructure, and save on costs. Instead of worrying about deploying and maintaining servers, the cloud infrastructure provides all the up-to-date servers needed to keep your applications running.
Now let us dive deep into Azure Function Creation:
Now wait for a while to validate your function app once validation completes clicking on the ‘Create’ button, will initialize your deployment. After deployment, go to your function app, you can see configuration settings on the left side of the main page. Go to Identity and select System assigned Identity Status to On and click the save button.
A system assigned managed identity is restricted to one per resource and is tied to the lifecycle of this resource. You can grant permissions to the managed identity by using Azure role-based access control (Azure RBAC). The managed identity is authenticated with Azure AD, so you don’t have to store any credentials in code.
Now click on Azure role assignments and select the subscription (if you have many) and provide an appropriate role to the function app as shown in the below image and click the ‘save’ button.
Now go to the main page, you can see the function blade on the left side of the page click on functions -> Add -> Develop environment as Develop in the portal and select HTTP Trigger and scroll down to provide the function name and Authorization level.
Authorization level controls whether the function requires an API key and which key to use; Function uses a function key; Admin uses your master key. The function and master keys are found in the ‘keys’ management panel on the portal when your function is selected. For user-based authentication, go to Function App Settings.
Click on Add will initialize the deployment of your function.
Click on Code + test blade and replace the existing code with the below code and save it.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
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. $RgName = $Request.Query.RgName if (-not $RgName) { $RgName = $Request.Body.RgName } $VmName = $Request.Query.VmName if (-not $VmName) { $VmName = $Request.Body.VmName } $sid=Get-Content -Path D:\home\site\wwwroot\file.txt $body = "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response." if ($VmName -and $RgName) { Select-AzSubscription -SubscriptionId $sid $VmDetails=Get-AzVM -ResourceGroupName $RgName -Name $VmName | Select-Object Name,ResourceGroupName,Location,@{Name="Image"; Expression={$_.StorageProfile.ImageReference.Offer}},@{Name="Zones"; Expression={$_.Zones}},@{Name="VmSize"; Expression={$_.HardwareProfile.VmSize}},@{Name="AvailabilitySetReference"; Expression={$_.AvailabilitySetReference.Id}},@{Name="ImageSku"; Expression={$_.StorageProfile.ImageReference.Sku}},@{Name="OsDiskType"; Expression={$_.StorageProfile.OsDisk.ManagedDisk.StorageAccountType}} $body = $VmDetails } # Associate values to output bindings by calling 'Push-OutputBinding'. Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{ StatusCode = [HttpStatusCode]::OK Body = $body }) |
The above code will give you the details of the Virtual Machine, so to make the function work we need to create a Virtual Machine.
Now click Test/Run now it will popup for input, click on Add Query parameter and provide the values as below, here I have given the values of my resource group.
Now click the ‘Run’ button to run your function and wait for a while to make things visible to you about the details of your Virtual Machine in the response body of the function like in the below image.
The main highlight use of Azure Functions is that you can write the code easy to use web interfaces and build and debug them locally on your machine of choice. And it lets you leverage the same function code on other targets – IoT Edge, Azure Stack, Cloud Service, etc.
Drop a comment and share your feedback. If you have any queries, I will be happy to reply.
Voiced by Amazon Polly |
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!
Nidhi
Mar 17, 2021
Very useful. Thanks for posting.
Rohan
Mar 16, 2021
very informative
Venkatesh
Mar 15, 2021
Great information sir
U
Mar 14, 2021
Excellent sirr 👌👌👍
Click to Comment