{"id":10243,"date":"2023-12-03T16:24:09","date_gmt":"2023-12-03T16:24:09","guid":{"rendered":"https:\/\/putridparrot.com\/blog\/?p=10243"},"modified":"2023-12-03T16:24:09","modified_gmt":"2023-12-03T16:24:09","slug":"azure-functions-aws-lambda-functions-google-cloud-functions","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/azure-functions-aws-lambda-functions-google-cloud-functions\/","title":{"rendered":"Azure Functions, AWS Lambda Functions, Google Cloud Functions"},"content":{"rendered":"<p>Some companies, due to regulatory requirements, a desire to not get locked into one cloud vendor or the likes, look towards a multi-cloud strategy. With this in mind this post is the first of a few showing some of the same functionality (but with different names) across the top three cloud providers, Microsoft&#8217;s Azure, Amazon&#8217;s AWS and Google Cloud.<\/p>\n<p>We&#8217;re going to start with the serverless technology known as Lambda Functions (in AWS, and I think they might have been the first), Azure Functions and the Google cloud equivalent Google Cloud Functions. Now, in truth the three may not be 100% compatible in terms of their API but they&#8217;re generally close enough to allow us to worry about the request and response only and keep the same code for the specific function. Ofcourse if your function uses DB&#8217;s or Queues, then you&#8217;re probably starting to get tied more to the vendor than the intention of this post.<\/p>\n<p>I&#8217;ve already covered Azure Functions in the past, but let&#8217;s revisit, so we can compare and contrast the offerings. <\/p>\n<p>I&#8217;m not too interested in the code we&#8217;re going to deploy, so we&#8217;ll stick with JavaScript for each provider and just write a simple echo service, i.e. we send in a value and it responds with the value preceded by &#8220;Echo: &#8221; (we can look at more complex stuff in subsequent posts).<\/p>\n<p><em>Note: We&#8217;re going to use the UI\/Dashboard to create our functions in this post. <\/em><\/p>\n<p><strong>Azure Functions<\/strong><\/p>\n<p>From Azure&#8217;s Dashboard<\/p>\n<ul>\n<li>Type <em>Function App<\/em> into the search box or select it from your Dashboard if it&#8217;s visible<\/li>\n<li>From the Function App page click the <em>Create Function App<\/em> button<\/em><\/li>\n<li>From the Create Function App screen\n<ul>\n<li>Select your subscription and resource group OR create a new resource group<\/li>\n<li>Supply a <em>Function app name<\/em>. This is essentially our apps name, as the Function app can hold multiple functions. The name must be unique across Azure websites<\/em>\n<li>Select <em>Code<\/em>. So we&#8217;re just going to code the functions in Azure not supply an image<\/em>\n<li>Select a runtime stack, let&#8217;s choose Node.js<\/li>\n<li>Select the version (I&#8217;m sticking with the default)<\/li>\n<li>Select the region, look for the region closest to you<\/li>\n<li>Select the <em>Operating System<\/em>, I&#8217;m going to leave this as the default Windows<\/li>\n<li>I&#8217;ve left the <em>Hosting<\/em> to the default <em>Consumption (Serverless)<\/em><\/li>\n<\/ul>\n<\/li>\n<li>Click Review + create<\/li>\n<li>If you&#8217;re happy, now click <em>Create<\/em><\/li>\n<\/ul>\n<p>Once Azure has done it&#8217;s stuff, we&#8217;ll have a resource and associated resources created for our functions. <\/p>\n<ul>\n<li><em>Go to resource<\/em> or type in <em>Function App<\/em> to the search box and navigate there via this option.<\/li>\n<li>You should see your new function app. with the <em>Status<\/em> running etc.<\/li>\n<li>Click on the app name and you&#8217;ll navigate to the apps. page<\/li>\n<li>Click on the <em>Create in Azure portal<\/em> button. You could choose VS Code Desktop or set up your own editor if you prefer<\/li>\n<li>We&#8217;re going to create an HTTP trigger, which is basically a function which will start-up when an HTTP request comes in for the function, so click <em>HTTP trigger<\/em>\n<ul>\n<li>Supply a <em>New Function<\/em>, I&#8217;m naming mine <em>Echo<\/em><\/li>\n<li>Leave Authorization level as <em>Function<\/em> OR set to <em>Anonymous<\/em> for a public API. Azure&#8217;s security model for functions is nice and simple, so I&#8217;ve chosen <em>Function<\/em> for this function, but feel free to change to suite<\/li>\n<li>When happy with your settings, click <em>Create<\/em><\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p>If all went well you&#8217;re now looking at the <em>Echo<\/em> function page.<\/p>\n<ul>\n<li>Click <em>Code + Test<\/em><\/li>\n<li>The default .js code is essentially an echo service, but I&#8217;m going to change it slightly to the following\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nmodule.exports = async function (context, req) {\r\n  const text = (req.query.text || (req.body &amp;&amp; req.body.text));\r\n  context.log('Echo called with ' + text);\r\n  const responseMessage = text\r\n    ? &quot;Echo: &quot; + text\r\n    : &quot;Pass a POST or GET with the text to echo&quot;;\r\n\r\n  context.res = {\r\n    body: responseMessage\r\n  };\r\n}\r\n<\/pre>\n<\/ul>\n<p>Let&#8217;s now test this function. The easiest way is click the <em>Test\/Run<\/em> option <\/p>\n<ul>\n<li>Change the Body to\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{&quot;text&quot;:&quot;Scooby Doo&quot;}\r\n<\/pre>\n<\/li>\n<li>Click <em>Run<\/em> and if all went well you&#8217;ll see <em>Echo: Scooby Doo<\/em><\/li>\n<li>To test from our browser, let&#8217;s get the URL for our function by clicking on the <em>Get function URL<\/em><\/li>\n<li>The URL will be in the following format and we&#8217;ve added the query string to use with it\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttps:\/\/your-function-appname.azurewebsites.net\/api\/Echo?code=your-function-key&text=Shaggy\r\n<\/pre>\n<\/li>\n<\/ul>\n<p>If all went well you&#8217;ll see <em>Echo: Shaggy<\/em> and we&#8217;ve basically created our simple Azure Function.<\/p>\n<p><em>Note: Don&#8217;t forget to delete your resources when you&#8217;ve finished testing this OR use it to create your own code<\/em><\/p>\n<p><strong>AWS Lamba<\/strong><\/p>\n<p>From the AWS Dashboard<\/p>\n<ul>\n<li>Type <em>Lambda<\/em> into the search box<\/li>\n<li>Click the button <em>Create function<\/em><\/li>\n<li>Leave the default as <em>Author from scratch<\/em><\/li>\n<li>Enter the function name. <em>echo<\/em> in my case<\/li>\n<li>Leave the runtime (this should be Node), architecture etc. as the default<\/li>\n<li>Click <em>Create function<\/em><\/li>\n<\/ul>\n<p>Once AWS has done it&#8217;s stuff let&#8217;s look at the code file index.mjs and change it to <\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nexport const handler = async (event, context) =&gt; { \r\n  console.log(JSON.stringify(event));\r\n  const response = {\r\n    statusCode: 200,\r\n    body: JSON.stringify('Echo: ' + event.queryStringParameters.text),\r\n  };\r\n  return response;\r\n};\r\n<\/pre>\n<p>You&#8217;ll need to <em>Deploy<\/em> the function before it updates to use the latest code but you&#8217;ll find that, at this time, you&#8217;ll probably get errors use the <em>Test<\/em> option. One thing we haven&#8217;t yet done is supply trigger. <\/p>\n<ul>\n<li>Either click <em>Add trigger<\/em> or from the <em>Configuration<\/em> tab click <em>Add trigger<\/em><\/li>\n<li>Select <em>API Gatewway<\/em> which will add an API to create a HTTP endpoint for REST\/HTTP requests<\/li>\n<li>If you&#8217;ve not created a existing API then select <em>Create a new API<\/em>\n<ul>\n<li>We&#8217;ll select HTTP API from here<\/li>\n<li>I&#8217;m not going to create JWT authorizer, so for <em>Security<\/em> for now, select <em>Open<\/em><\/li>\n<li>Click the <em>Add<\/em> button<\/li>\n<\/ul>\n<\/li>\n<p>From the <em>Configuration<\/em> tab you&#8217;ll see an API endpoint, in your browser paste the endpoint url and add the query string so it looks a bit like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttps:\/\/end-point.amazonaws.com\/default\/echo?text=Scooby%20Doo\r\n<\/pre>\n<p><em>Note: Don&#8217;t forget to delete your functions when you&#8217;ve finished testing it OR use it to create your own code<\/em><\/p>\n<p><strong>Google Cloud Function<\/strong><\/p>\n<p>From the Google Cloud dashboard<\/p>\n<ul>\n<li>Type <em>Cloud Functions<\/em> into the search box<\/li>\n<li>From the Functions page, click <em>Create Function<\/em><\/li>\n<li>If the <em>Enable required APIs<\/em> popup appears you&#8217;ll need to click <em>ENABLE<\/em> to ensure all APIs are enabled<\/li>\n<\/ul>\n<p><\/br><br \/>\nFrom the Configuration page<\/p>\n<ul>\n<li>Set to <em>Environment<\/em> if required, mine&#8217;s defaulted to 2nd gen which is the latest environment<\/li>\n<li>Supply the function name, mine&#8217;s again <em>echo<\/em><\/li>\n<li>Set the region to one near your region<\/li>\n<li>The default trigger is <em>HTTPS<\/em>, so we won&#8217;t need to change this<\/li>\n<li>Just to save on having to setup authentication let&#8217;s choose the <em>Allow unauthenticated invocations<\/em> i.e. making a public API<\/li>\n<li>Let&#8217;s also copy the URL for now which ill be something like\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttps:\/\/your-project.cloudfunctions.net\/echo\r\n<\/pre>\n<\/li>\n<li>Clich the <em>Next<\/em> button<\/li>\n<\/ul>\n<p><\/br><br \/>\nThis defaulted to creating a Node.js runtime. Let&#8217;s change our code to the familiar echo code<\/p>\n<ul>\n<li>The code should look like the following\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nconst functions = require('@google-cloud\/functions-framework');\r\n\r\nfunctions.http('helloHttp', (req, res) =&gt; {\r\n  res.send(`Echo: ${req.query.text || req.body.text}`);\r\n});\r\n<\/pre>\n<\/li>\n<li>Click the <em>Test<\/em> button and GCP will create the container etc.<\/li>\n<\/ul>\n<p><\/br><br \/>\nOnce everything is deployed then change the test payload to <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;text&quot;: &quot;Scooby Doo&quot;\r\n}\r\n<\/pre>\n<p>and click <em>Run Test<\/em>. If all went well you&#8217;ll see the Echo response in the GCF Testing tab.<\/p>\n<p>Finally, when ready click <em>Deploy<\/em> and then we can test our Cloud function via the browser, using the previously copied URL, like this<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\nhttps:\/\/your-project.cloudfunctions.net\/echo?text=Scooby%20Doo\r\n<\/pre>\n<p><em>Note: Don&#8217;t forget to delete your function(s) when you&#8217;ve finished testing this OR use it to create your own code<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some companies, due to regulatory requirements, a desire to not get locked into one cloud vendor or the likes, look towards a multi-cloud strategy. With this in mind this post is the first of a few showing some of the same functionality (but with different names) across the top three cloud providers, Microsoft&#8217;s Azure, Amazon&#8217;s [&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":[700,705,96,357,706,235],"tags":[],"class_list":["post-10243","post","type-post","status-publish","format-standard","hentry","category-aws","category-aws-lambda","category-azure-2","category-azure-functions","category-google-cloud-functions","category-google-cloud-platform"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10243","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=10243"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10243\/revisions"}],"predecessor-version":[{"id":10262,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/10243\/revisions\/10262"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=10243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=10243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=10243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}