{"id":8010,"date":"2021-03-11T10:26:55","date_gmt":"2021-03-11T10:26:55","guid":{"rendered":"https:\/\/blog.cloudthat.com\/?p=8010"},"modified":"2024-06-25T11:08:43","modified_gmt":"2024-06-25T11:08:43","slug":"serverless-compute-with-azure-function-apps","status":"publish","type":"blog","link":"https:\/\/www.cloudthat.com\/resources\/blog\/serverless-compute-with-azure-function-apps","title":{"rendered":"Serverless Compute with Azure Function Apps"},"content":{"rendered":"<p><strong><span class=\"TextRun SCXW105106862 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW105106862 BCX0\">Azure\u00a0<\/span><\/span><span class=\"TextRun SCXW105106862 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW105106862 BCX0\">Function App<\/span><\/span><\/strong><span class=\"TextRun SCXW105106862 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW105106862 BCX0\">\u00a0is an app service that serves\u00a0<\/span><\/span><span class=\"TextRun SCXW105106862 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"none\"><span class=\"NormalTextRun SCXW105106862 BCX0\">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.<\/span><\/span><span class=\"EOP SCXW105106862 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<h2><b><span data-contrast=\"auto\">Why Function App?<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h2>\n<p><span data-contrast=\"auto\">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.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">Now let us dive deep into\u00a0<\/span><span data-contrast=\"auto\">Azure F<\/span><span data-contrast=\"auto\">unction<\/span><span data-contrast=\"auto\">\u00a0Creation:<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"auto\">Login to Azure with your credentials<\/span><span data-ccp-props=\"{&quot;134233279&quot;:true,&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"auto\">Search for Function App\u00a0<\/span><span data-ccp-props=\"{&quot;134233279&quot;:true,&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" aria-setsize=\"-1\" data-aria-posinset=\"3\" data-aria-level=\"1\"><span data-contrast=\"auto\">Click the\u00a0<\/span><span data-contrast=\"auto\">\u2018<\/span><span data-contrast=\"auto\">Add<\/span><span data-contrast=\"auto\">\u2019<\/span><span data-contrast=\"auto\">\u00a0button to create a new function\u00a0app<\/span><span data-ccp-props=\"{&quot;134233279&quot;:true,&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<\/ul>\n<ul>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" aria-setsize=\"-1\" data-aria-posinset=\"1\" data-aria-level=\"1\"><span data-contrast=\"auto\">Now fill in the details of Subscription, Resource group name, Function app name<\/span><span data-ccp-props=\"{&quot;134233279&quot;:true,&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/li>\n<li data-leveltext=\"\uf0b7\" data-font=\"Symbol\" data-listid=\"2\" aria-setsize=\"-1\" data-aria-posinset=\"2\" data-aria-level=\"1\"><span data-contrast=\"auto\">Select Code in Publish field and select Runtime Stack, version, Region as shown\u00a0<\/span><span data-contrast=\"auto\">below and click Review and\u00a0create<br \/>\n<a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA1.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8011 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA1.png\" alt=\"Azure Function App\" width=\"711\" height=\"369\" \/><\/a><\/span><\/li>\n<\/ul>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA2.png\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-8012 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA2.png\" alt=\"Azure Function App\" width=\"714\" height=\"433\" \/><\/a><\/p>\n<p><span data-contrast=\"auto\">Now wait for a while to validate your function app once validation completes clicking on\u00a0<\/span><span data-contrast=\"auto\">the \u2018C<\/span><span data-contrast=\"auto\">reate<\/span><span data-contrast=\"auto\">\u2019<\/span><span data-contrast=\"auto\">\u00a0button, will initialize your deployment.<\/span><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">After deployment<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\">\u00a0go to your function app, you can see configuration settings on the left side of the main page.<\/span><span data-contrast=\"auto\">\u00a0Go to Identity and select System assigned Identity Status to\u00a0<\/span><span data-contrast=\"auto\">On and click the save button<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"auto\">\u00a0<\/span><span data-contrast=\"auto\">A system assigned managed identity is restricted to one per resource and is tied to the lifecycle<\/span><span data-contrast=\"auto\">\u00a0of 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\u2019t have to store any credentials in code.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA3.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8013 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA3.png\" alt=\"Azure Function App\" width=\"650\" height=\"184\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW153661811 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW153661811 BCX0\">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 &#8216;save&#8217; button.<\/span><\/span><span class=\"EOP SCXW153661811 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA4.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8014 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA4.png\" alt=\"Azure Function App\" width=\"643\" height=\"242\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">Now go to the main page<\/span><span data-contrast=\"auto\">,<\/span><span data-contrast=\"auto\">\u00a0you can see the function blade on the left side of the page click on functions -&gt; Add -&gt;<\/span><span data-contrast=\"auto\">\u00a0Develop environment as Develop in the portal and select HTTP Trigger and scroll down to provide the function name and\u00a0<\/span><span data-contrast=\"none\">Authorization level<\/span><span data-contrast=\"auto\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><span data-contrast=\"none\">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 &#8216;keys&#8217; management panel on the portal when your function is selected. For user-based authentication, go to Function App Settings.<\/span><\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA5.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8015 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA5.png\" alt=\"Azure Function App\" width=\"631\" height=\"342\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA6.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8017 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA6.png\" alt=\"Azure Function App\" width=\"626\" height=\"268\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW71744973 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW71744973 BCX0\">Click on Add will initialize the deployment of your function.<\/span><\/span><span class=\"EOP SCXW71744973 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA7.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8018 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA7.png\" alt=\"Azure Function Apps\" width=\"599\" height=\"257\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW45359561 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW45359561 BCX0\">Click on Code + test blade\u00a0<\/span><\/span><span class=\"TextRun SCXW45359561 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW45359561 BCX0\">and replace the existing code with the below code<\/span><\/span><span class=\"TextRun SCXW45359561 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW45359561 BCX0\">\u00a0and save it<\/span><\/span><span class=\"TextRun SCXW45359561 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW45359561 BCX0\">.<\/span><\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<pre class=\"lang:default decode:true \">using namespace\u00a0System.Net\u00a0\r\n\r\n\u00a0\r\n\r\n# Input bindings are passed in via param block.\u00a0\r\n\r\nparam($Request, $TriggerMetadata)\u00a0\r\n\r\n\u00a0\r\n\r\n# Write to the Azure Functions log stream.\u00a0\r\n\r\nWrite-Host \"PowerShell HTTP trigger function processed a request.\"\u00a0\r\n\r\n\u00a0\r\n\r\n# Interact with query parameters or the body of the request.\u00a0\r\n\r\n$RgName\u00a0= $Request.Query.RgName\u00a0\r\n\r\nif (-not $RgName) {\u00a0\r\n\r\n\u00a0\u00a0\u00a0 $RgName\u00a0= $Request.Body.RgName\u00a0\r\n\r\n}\u00a0\r\n\r\n$VmName\u00a0= $Request.Query.VmName\u00a0\r\n\r\nif (-not $VmName) {\u00a0\r\n\r\n\u00a0\u00a0\u00a0 $VmName\u00a0= $Request.Body.VmName\u00a0\r\n\r\n}\u00a0\r\n\r\n$sid=Get-Content -Path D:\\home\\site\\wwwroot\\file.txt\u00a0\r\n\r\n$body = \"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.\"\u00a0\r\n\r\n\u00a0\r\n\r\nif ($VmName\u00a0-and $RgName) {\u00a0\r\n\r\n\u00a0\u00a0\u00a0 Select-AzSubscription\u00a0-SubscriptionId\u00a0$sid\u00a0\r\n\r\n\u00a0\u00a0\u00a0 $VmDetails=Get-AzVM\u00a0-ResourceGroupName\u00a0$RgName\u00a0-Name $VmName\u00a0| Select-Object\u00a0Name,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}}\u00a0\r\n\r\n\u00a0\u00a0\u00a0 $body = $VmDetails\u00a0\r\n\r\n}\u00a0\r\n\r\n\r\n\r\n# Associate values to output bindings by calling 'Push-OutputBinding'.\u00a0\r\n\r\nPush-OutputBinding\u00a0-Name Response -Value ([HttpResponseContext]@{\u00a0\r\n\r\n\u00a0\u00a0\u00a0\u00a0StatusCode\u00a0= [HttpStatusCode]::OK\u00a0\r\n\r\n\u00a0\u00a0\u00a0 Body = $body\u00a0\r\n\r\n})<\/pre>\n<p>&nbsp;<\/p>\n<p><span data-contrast=\"auto\">The above code will give you the details of the Virtual Machine, so to make the function work we need to create a V<\/span><span data-contrast=\"auto\">irtual Machine<\/span><span data-contrast=\"none\">.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:2,&quot;335559739&quot;:160,&quot;335559740&quot;:285}\">\u00a0<\/span><\/p>\n<p><span data-ccp-props=\"{&quot;201341983&quot;:2,&quot;335559739&quot;:160,&quot;335559740&quot;:285}\">\u00a0<\/span><span data-contrast=\"none\">Now click Test\/Run now it will\u00a0popup\u00a0for input, click on Add Query parameter and provide\u00a0<\/span><span data-contrast=\"none\">the values as below, here I have given the values of my resource\u00a0group.<\/span><\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA8.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8019 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA8.png\" alt=\"Azure Function Apps \" width=\"659\" height=\"349\" \/><\/a><\/p>\n<p><span class=\"TextRun SCXW67364537 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW67364537 BCX0\">Now click the &#8216;<\/span><\/span><span class=\"TextRun SCXW67364537 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW67364537 BCX0\">R<\/span><\/span><span class=\"TextRun SCXW67364537 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW67364537 BCX0\">un&#8217; button to run your function\u00a0<\/span><\/span><span class=\"TextRun SCXW67364537 BCX0\" lang=\"EN-US\" xml:lang=\"EN-US\" data-contrast=\"auto\"><span class=\"NormalTextRun SCXW67364537 BCX0\">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.<\/span><\/span><span class=\"EOP SCXW67364537 BCX0\" data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559685&quot;:360,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/p>\n<p>&nbsp;<\/p>\n<p><a href=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA9.png\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-8020 aligncenter\" src=\"https:\/\/content.cloudthat.com\/resources\/wp-content\/uploads\/2022\/11\/FA9.png\" alt=\"Azure Function Apps \" width=\"616\" height=\"311\" \/><\/a><\/p>\n<h3><b><span data-contrast=\"auto\">Conclusion:\u00a0<\/span><\/b><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:259}\">\u00a0<\/span><\/h3>\n<p><span data-contrast=\"none\">The main highlight use of <strong>Azure Functions<\/strong> is that you can write the code\u00a0<\/span><span data-contrast=\"none\">easy to use<\/span><span data-contrast=\"none\">\u00a0web\u00a0<\/span><span data-contrast=\"none\">interfaces and build and debug them locally on your machine of choice. And it lets you\u00a0<\/span><span data-contrast=\"none\">leverage the same function code on other targets &#8211; IoT Edge, Azure Stack, Cloud Service, etc.<\/span><span data-ccp-props=\"{&quot;201341983&quot;:0,&quot;335559739&quot;:160,&quot;335559740&quot;:257}\">\u00a0<\/span><\/p>\n<p>Drop a comment and share your feedback. If you have any queries, I will be happy to reply.<\/p>\n","protected":false},"author":238,"featured_media":8072,"parent":0,"comment_status":"open","ping_status":"open","template":"","blog_category":[3608,3607],"user_email":"shaikb@cloudthat.com","published_by":"324","primary-authors":"","secondary-authors":"","acf":[],"_links":{"self":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/8010"}],"collection":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog"}],"about":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/types\/blog"}],"author":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/users\/238"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/comments?post=8010"}],"version-history":[{"count":1,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/8010\/revisions"}],"predecessor-version":[{"id":42783,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog\/8010\/revisions\/42783"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/media?parent=8010"}],"wp:term":[{"taxonomy":"blog_category","embeddable":true,"href":"https:\/\/www.cloudthat.com\/resources\/wp-json\/wp\/v2\/blog_category?post=8010"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}