A Clockwork Engineer

Azure Functions

Azure Functions

Architect serverless solutions in Azure

Serverless, as the name suggests, is computing without managing a server. You would not care about any server specification to process your data. The server should already be provided by a cloud platform as a PaaS (platform as a service) or as a FaaS (function as a service). The two common approaches on Azure are Logic Apps and Functions.

Is serverless computing right for my business needs?

No, it is not. You need to create clear requirements for your business needs then develop your application. You can answer this question only by analyzing your telemetry results from production. The serverless computing is a technical solution for a better infrastructure allocation and event driven design. There is not any engineer who can calculate necessacities without any data.

For example, functions have a timeout of 10 minutes maximum. If it is triggered by an HTTP request then it lowers to 2.5 minutes. Even if it responds in seconds, there is still a probability that it is executed continuously. In this case, it would be more expensive than having an app service or a VM.

How serverless can help me?

You will be working only on your business logic code in the technology stack of your choice (.NET Core, Node.js, Python, Java and PowerShell Core). No more hassle about infrastucture, scaling up or down. Most importantly, you are charged based on what is used.

Bindings

You can declare bindings to connect to other services so you do not need to maange connections in your code. We want to work only on our business logic which is special for our needs.

Go to Azure documents for an up-to-date list of triggers and bindings to connect to various storage and messaging services.

{
  "bindings": [
    {
      "name": "order",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "MY_STORAGE_ACCT_APP_SETTING"
    },
    {
      "name": "$return",
      "type": "table",
      "direction": "out",
      "tableName": "outTable",
      "connection": "MY_TABLE_STORAGE_ACCT_APP_SETTING"
    }
  ]
}

A sample function.json file to configure our function to be triggered when a message is added to myqueue-items queue and then the return value is written to the outTable table in Azure Table Storage.

Triggers

A trigger is a special type of input binding that has the additional capability of initiating execution. Functions can be triggered by those services below;

  • Blob storage
  • Azure Cosmos DB
  • Event Grid
  • HTTP
  • Microsoft Graph Events
  • Queue storage
  • Service Bus
  • Timer

Create a function app

A function app is a way to organize and collectively manage your functions.

  1. Sign into the Azure portal
  2. From the portal menu, select Create a resource.
  3. Select Compute > Function App.
  4. Select a resource group.
  5. Give a globally unique function app name. This will serve as the base URL of your service.
  6. Select Node.js as runtime stack.
  7. By default selections; Windows as OS, Consumption Plan as plan type, Azure Application Insights is ON and a new storage account will be created to store your code, queue or tables.
  8. Click Create. Deployment may take several minutes. When it is complete, you will be notified, then you can continue to create a function.

Create a function

  1. Go to All resources page and select your function with the lightning bolt Function icon.
  2. Select ➕ (Create new) next to Functions.

Azure Function Create new

  1. On the quickstart page, select In-portal and then Continue below.
  2. On the CREATE A FUNCTION step, select More templates… and then click Finish and view templates.
  3. Select HTTP Trigger.
  4. Enter a name for your trigger and click Create. Leave the Authorization level as Function, we will come to this later.
  5. When it is complete, the index.js file will be opened in an online editor. The code in the file has a simple logic which expects a name parameter as part of query string or request body, then responds with a message like Hello {name}.
  6. If you select View files tab on the right hand side, you seen function.json file there. You can also go to Integrate page of that function to see the bindings in a better UI.
// Default template for a function triggered by an HTTP request
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

Test a function

We can use Test tab on the right hand side menu but let’s test the function on a separate window.

  1. On the function editor page, select </> Get function URL.

Azure Function Get function URL

  1. This will create a URL with default key. We left the Authorization level as Function so the default is funtion-specific API key. Copy the URL.
  2. Open a new browser and paste the URL.
  3. Add name parameter at the end of the URL &name=olcay so the URL is like https://{functionAppName}.azurewebsites.net/api/{functionName}?code={APIKey}&name=olcay
  4. The result will be Hello olcay

The API key can also be provided as a HTTP header named x-functions-key instead of code parameter in the query string. These keys can be managed on Manage page.

Monitor a function

If you go Monitor page of a function on the left hand side menu, you can see the history of function executions. Since we had a log statement in our code as below, it would also be visible in the details of executions.

context.log('JavaScript HTTP trigger function processed a request.');

Cover photo by Michael Rogers on Unsplash

The other posts in the series "Architect serverless solutions in Azure"
cloud, azure, function Azure

Author: Olcay Bayram

A software enthusiast; currently a .NET Developer of an e-commerce Scrum team. Apart from the BSc, he holds a masters in information technologies.