Go Serverless with Azure Functions
Azure Functions are a serverless compute service.
From Wikipedia, Serverless is an acronym that indicates:
Serverless computing is a cloud-computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources.
It is an abstraction provided to the developer that allows them to ignore the existence of a virtual machine.
Why they are cool
First, their billing comes at consumption. Compared to app services, this means being charged only on the time the function is running rather than on a fixed monthly quota. Azure functions require an Azure Storage Account to be created and charge for that won’t be included.
Second, they integrate seamlessly with other Azure services and they can react to other resources’ events.
Create an Azure Function App
Create Azure Functions is simple. They run into an Azure Function App that similarly to what happened with App Service Plans determines basic settings and billing.
I will outline the major steps in here.
Search for a Function App
Add the basic info
The following information is required:
- An App Name, it will produce an URL that will identify the App Service. I will choose “iloveazurefunctions” and this will generate “iloveazurefunction.azurewebsites.net”
- A subscription that will be billed
- A resource group
- A hosting plan, there will be a choice available between a “Consumption Plan” or reusing an existing “App Service Plan”
- A location
- A runtime stack, currently .NET, JavaScript, Java and PowerShell are available
- An Azure Storage Account will be created. A similar folder to the one used by app services will be created and will be used to store diagnostic files and the executables
Settings
Azure Function Apps share similar settings to App Services.
Each setting will affect all the functions that run in the same app.
Create An Azure Function in the portal
Creating a function is very easy. Click at the “New Function” button
The portal will prompt to choose a template between those available. I will try an “HTTP Trigger”. The dialogue prompts for an API Key to be used in order to make calls.
The function is contained inside a .csx file, formerly “C# Scripting”.
The code accepts a parameter from the query string labelled as “name” or picks it from a JSON.
A convenient way to test the function is using the aside form:
Integrate the function
Inside the integrate tab is possible to define the function inputs and the function output.
Define the HTTP Request
It will be possible to define the HTTP method of choice, parameter name, routing and the type of authorization key needed to invoke the script.
After changing the name of the request, you will need to manually change your script or the function won’t compile.
Define the output
Add Inputs
Other inputs can be added at the occurrence, generally to integrate the function with other Azure services.
Run the function from Postman
I will copy the function URL
I will add my function predefined authorization key as “x-function-key” header:
I will define a body:
Conclusions
Azure functions are very easy to create and run. They provide a cost-effective integrated environment where to run code without the hassle of handling laborious deployments