{"id":10057,"date":"2023-11-19T22:31:26","date_gmt":"2023-11-19T22:31:26","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=10057"},"modified":"2023-11-22T16:50:34","modified_gmt":"2023-11-22T16:50:34","slug":"azure-functions","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/azure-functions\/","title":{"rendered":"Azure Functions"},"content":{"rendered":"<p>Azure functions (like AWS lambdas and GCP cloud functions) allow us to write serverless code literally just as functions, i.e. no need to fire up a web application or VM. Ofcourse just like Azure containers, there is a server component but we, the developer, need not concerns ourselves with handling configuration etc.<\/p>\n<p>Azure functions will be spun up as and when required, meaning we will only be charged when they&#8217;re used. The downside of this is they have to spin up from a &#8220;cold&#8221; state. In other words the first person to hit your function will likely incur a performance hit whilst the function is started then invoked. <\/p>\n<p>The other thing to remember is Azure functions are <em>stateless<\/em>. You might store state with a DB like CosmoDB, but essentially a function is invoked, does something then after a timeout period it&#8217;s shut back down.<\/p>\n<p>Let&#8217;s create an example function and see how things work&#8230;<\/p>\n<ul>\n<li>Create a new <em>Azure Functions<\/em> project<\/li>\n<li>When you get to the options for the Function, select <em>Http trigger<\/em> and select <em>Amonymous<\/em> Authorization level<\/li>\n<li>Complete the wizard by clicking the <em>Create<\/em> button<\/li>\n<\/ul>\n<p>The Authorization level allows the function to be triggered without providing a key. The HTTP trigger, as it sounds, means the function is triggered by an HTTP request.<\/p>\n<p>The following is basically the code that&#8217;s created from the Azure Function template<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\npublic static class ExampleFunction\r\n{\r\n  &#x5B;FunctionName(&quot;Example&quot;)]\r\n  public static async Task&lt;IActionResult&gt; Run(\r\n        &#x5B;HttpTrigger(AuthorizationLevel.Anonymous, &quot;get&quot;, &quot;post&quot;, Route = null)] HttpRequest req,\r\n        ILogger log)\r\n  {\r\n    log.LogInformation(&quot;HTTP trigger function processed a request.&quot;);\r\n\r\n    string name = req.Query&#x5B;&quot;name&quot;];\r\n\r\n    var requestBody = await new StreamReader(req.Body).ReadToEndAsync();\r\n    dynamic data = JsonConvert.DeserializeObject(requestBody);\r\n    name = name ?? data?.name;\r\n\r\n    var responseMessage = string.IsNullOrEmpty(name) \r\n      ? &quot;Pass a name in the query string or in the request body for a personalized response.&quot;\r\n            : $&quot;Hello, {name}. This HTTP triggered function executed successfully.&quot;;\r\n\r\n    return new OkObjectResult(responseMessage);\r\n  }\r\n}\r\n<\/pre>\n<p>We can actually run this and debug via Visual Studio in the normal way. We&#8217;ll get a URL supplied, something like this http:\/\/localhost:7071\/api\/Example to access our function.<\/p>\n<p>As you can see from the above code, we&#8217;ll get passed an ILogger and an HttpRequest. From this we can get query parameters, so this URL above would be used like this http:\/\/localhost:7071\/api\/Example?name=PutridParrot<\/p>\n<p>Ofcourse the whole purpose of the Azure Function is for it to run on Azure. To publish it&#8230;<\/p>\n<ul>\n<li>From Visual Studio, right mouse click on the project and select <em>Publish<\/em><\/li>\n<li>For the target, select Azure. Click <em>Next<\/em><\/li>\n<li>Select <em>Azure Function App (Windows)<\/em> or Linux if you prefer. Click <em>Next<\/em> again<\/li>\n<li>Either select a Function instance if one already exist or you can create a new instance from this wizard page<\/li>\n<\/ul>\n<p>If you&#8217;re creating a new instance, select the resource group etc. as usual and then click <em>Create<\/em> when ready.<\/p>\n<p><em>Note: I chose Consumption plan, which is the default when creating an Azure Functions instance. This is basically a &#8220;pay only for executions of your functions app&#8221;, so should be the cheapest plan.<\/em><\/p>\n<p>The next step is to <em>Finish<\/em> the publish process. If all went well you&#8217;ll see everything configures and you can close the Publish dialog.<\/p>\n<p>From the Azure dashboard you can simply type into the search textbox Function App and you should see the published function with a status of <em>Running<\/em>. If you click on the function name it will show you the current status of the function as well as it&#8217;s URL which we can access like we did with localhost, i.e. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttps:\/\/myfunctionssomewhere.azurewebsites.net\/api\/Example?name=PutridParrot\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Azure functions (like AWS lambdas and GCP cloud functions) allow us to write serverless code literally just as functions, i.e. no need to fire up a web application or VM. Ofcourse just like Azure containers, there is a server component but we, the developer, need not concerns ourselves with handling configuration etc. Azure functions will [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[96,357,3],"tags":[],"class_list":["post-10057","post","type-post","status-publish","format-standard","hentry","category-azure-2","category-azure-functions","category-c"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10057","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=10057"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10057\/revisions"}],"predecessor-version":[{"id":10185,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10057\/revisions\/10185"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=10057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=10057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=10057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}