DEV Community

Cover image for Azure vs GCP part 13: Functions (Azure)
Kenichiro Nakamura
Kenichiro Nakamura

Posted on

Azure vs GCP part 13: Functions (Azure)

In this article, I look into Functions for Azure and GCP. Why function matters? There are tons of answers for that question and I won't explain it in this article as I just want to compare Azure and GCP as C# developer.

Azure

Azure offers Function App to support function technology.

  • Support multiple languages: C#, F#, Javascript, Java, etc. See here for detail.
  • Support multiple triggers: HTTP request, timer, several web service webhook, several storage triggers. See here for detail.
  • Scaling: It's support multiple scaling options. See here for detail.
  • Flexible pricing: See here for detail.
  • Integrate with platform: Security, Monitoring, Other services, etc. See here for detail.
  • IDE support: Visual Studio and Visual Studio Code extension, local run-time for local debugging, etc.
  • On-premise support: This is interesting that Microsoft offers on-premise run-time for Function App and offer same experience across cloud and on-premise. See here for mode detail.

1.x and 2.x

At this moment, there are two major versions for Function App.

  • 1.x: Production ready. It's runs on Full .NET
  • 2.x: Still preview. It's runs on .NET core, means it's cross platform.

See more detail at Compare 1.x and 2.x

Binding

One of the important concept is binding. I can "bind" many things to my function and treat them in same manner. There are several binding types.

  • Trigger: I can bind trigger type to "trigger" my function. For example, the function can be triggered when a object is created in a blob storage, or when an event received in Event Grid.
  • Input: I can integrate with services as input to get data, such as Microsoft Graph, One Drive, Blob, Auth token, etc.
  • Output: Similar to input but I can output the result to the output binding.

See the Supported bindings here

Write a code

There are so many features, so let's do it one by one.

Basic function

1. Go to Azure Portal and create new resource. Select Function App and create.
function

2. Enter unique app name, and select OS. I select "Windows" this time. Click "Create".
funciton

3. Once it's created, go to the resource. Select "Fucntios" and click "+", then click "New function".
function

4. Select trigger and language you want to try. I use HTTP trigger and C# here.
function

5. Click "Create" with default values. Maybe changing name is good idea though.
function

6. At this point, the function is triggered via http and return http response as output. Click "Get function URL" and copy the address.
function

7. Hit the URL via tool such as postman or curl. Add name parameter.
function

Input Binding

Now, let's try integration. As I already have storage account which is auto created when I created Function App, I use it as my input and output.

1. Select Integration. I can see HTTP as trigger and output, and no input.
input

2. Click "New Input" and select "Azure Blob Storage". Then click "Select".
input

3. Click "new" next to storage account connection. Select the storage account to connect to. When wizard close, click "Save". The input name is "inputBlob" and path is "incontainer". I use container level for this function so I didn't specify object level path.
input

4. Go back to the function and update the code with following.

using System.Net;
using System.IO;
using Microsoft.Azure;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;

public static async Task<HttpResponseMessage> Run(
    HttpRequestMessage req, 
    CloudBlobContainer inputBlob,
    TraceWriter log)
{
    log.Info("C# HTTP trigger function processed a request.");
    log.Info($"Container name: {inputBlob.Name}");
        // parse query parameter
    string name = req.GetQueryNameValuePairs()
        .FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
        .Value;
    await inputBlob.CreateIfNotExistsAsync();
    CloudBlockBlob blob = inputBlob.GetBlockBlobReference(name);
    int i = 0;
    try
    {
        i = int.Parse(await blob.DownloadTextAsync());
    }
    catch(Exception ex)
    {
        // Do nothing
    }
    i++;

    await blob.UploadTextAsync((i).ToString());
    return name == null
        ? req.CreateResponse(HttpStatusCode.BadRequest, "Please pass a name on the query string or in the request body")
        : req.CreateResponse(HttpStatusCode.OK, $"Hello {name}. It's {i} times.");
}

Enter fullscreen mode Exit fullscreen mode

5. Click "Test" on the right and add parameter "name" with some name.
input

6. Click "Save and Run"

Developer experience

As I said, there are several tools.

Visual Studio extension

Azure Functions and Web Jobs Tools

It provides several things:

  • Project template
  • Run-time
  • Local debug environment
  • Deploy to Azure

Visual Studio Code extension

Azure Functions

It provides same capabilities as above.

Durable Functions

I need another article to explain this, but it basically orchestrates multiple functions, when you have many functions which requires to execute certain orders.

Documentation contains useful scenario where this helps.

In the next article, I look into function option of GCP.

Reference

Azure Functions
Durable Functions

Top comments (0)