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’s Azure, Amazon’s AWS and Google Cloud.
We’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’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’s or Queues, then you’re probably starting to get tied more to the vendor than the intention of this post.
I’ve already covered Azure Functions in the past, but let’s revisit, so we can compare and contrast the offerings.
I’m not too interested in the code we’re going to deploy, so we’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 “Echo: ” (we can look at more complex stuff in subsequent posts).
Note: We’re going to use the UI/Dashboard to create our functions in this post.
Azure Functions
From Azure’s Dashboard
- Type Function App into the search box or select it from your Dashboard if it’s visible
- From the Function App page click the Create Function App button
- From the Create Function App screen
- Select your subscription and resource group OR create a new resource group
- Supply a Function app name. This is essentially our apps name, as the Function app can hold multiple functions. The name must be unique across Azure websites
- Select Code. So we’re just going to code the functions in Azure not supply an image
- Select a runtime stack, let’s choose Node.js
- Select the version (I’m sticking with the default)
- Select the region, look for the region closest to you
- Select the Operating System, I’m going to leave this as the default Windows
- I’ve left the Hosting to the default Consumption (Serverless)
- Click Review + create
- If you’re happy, now click Create
Once Azure has done it’s stuff, we’ll have a resource and associated resources created for our functions.
- Go to resource or type in Function App to the search box and navigate there via this option.
- You should see your new function app. with the Status running etc.
- Click on the app name and you’ll navigate to the apps. page
- Click on the Create in Azure portal button. You could choose VS Code Desktop or set up your own editor if you prefer
- We’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 HTTP trigger
- Supply a New Function, I’m naming mine Echo
- Leave Authorization level as Function OR set to Anonymous for a public API. Azure’s security model for functions is nice and simple, so I’ve chosen Function for this function, but feel free to change to suite
- When happy with your settings, click Create
If all went well you’re now looking at the Echo function page.
Let’s now test this function. The easiest way is click the Test/Run option
If all went well you’ll see Echo: Shaggy and we’ve basically created our simple Azure Function.
Note: Don’t forget to delete your resources when you’ve finished testing this OR use it to create your own code
AWS Lamba
From the AWS Dashboard
- Type Lambda into the search box
- Click the button Create function
- Leave the default as Author from scratch
- Enter the function name. echo in my case
- Leave the runtime (this should be Node), architecture etc. as the default
- Click Create function
Once AWS has done it’s stuff let’s look at the code file index.mjs and change it to
export const handler = async (event, context) => {
console.log(JSON.stringify(event));
const response = {
statusCode: 200,
body: JSON.stringify('Echo: ' + event.queryStringParameters.text),
};
return response;
};
You’ll need to Deploy the function before it updates to use the latest code but you’ll find that, at this time, you’ll probably get errors use the Test option. One thing we haven’t yet done is supply trigger.
- Either click Add trigger or from the Configuration tab click Add trigger
- Select API Gatewway which will add an API to create a HTTP endpoint for REST/HTTP requests
- If you’ve not created a existing API then select Create a new API
- We’ll select HTTP API from here
- I’m not going to create JWT authorizer, so for Security for now, select Open
- Click the Add button
From the Configuration tab you’ll see an API endpoint, in your browser paste the endpoint url and add the query string so it looks a bit like this
https://end-point.amazonaws.com/default/echo?text=Scooby%20Doo
Note: Don’t forget to delete your functions when you’ve finished testing it OR use it to create your own code
Google Cloud Function
From the Google Cloud dashboard
- Type Cloud Functions into the search box
- From the Functions page, click Create Function
- If the Enable required APIs popup appears you’ll need to click ENABLE to ensure all APIs are enabled
From the Configuration page
This defaulted to creating a Node.js runtime. Let’s change our code to the familiar echo code
Once everything is deployed then change the test payload to
{
"text": "Scooby Doo"
}
and click Run Test. If all went well you’ll see the Echo response in the GCF Testing tab.
Finally, when ready click Deploy and then we can test our Cloud function via the browser, using the previously copied URL, like this
https://your-project.cloudfunctions.net/echo?text=Scooby%20Doo
Note: Don’t forget to delete your function(s) when you’ve finished testing this OR use it to create your own code