Azure vs GCP part 13: Functions (Azure)
Tags: azure, gcp, cloud
Published: 2018-03-18 17:35:03

---
title: "Azure vs GCP part 13: Functions (Azure)"
published: true
description: In this series, I compare Azure and GCP as C# developer
tags: azure,gcp,cloud
cover_image: https://thepracticaldev.s3.amazonaws.com/i/m57mmv4hgz9xrskaital.PNG
---

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](https://docs.microsoft.com/en-us/azure/azure-functions/supported-languages) for detail.
- Support multiple triggers: HTTP request, timer, several web service webhook, several storage triggers. See [here](https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview#what-can-i-do-with-functions) for detail.
- Scaling: It's support multiple scaling options. See [here](https://docs.microsoft.com/en-us/azure/azure-functions/functions-scale) for detail.
- Flexible pricing: See [here](https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview#pricing) for detail.
- Integrate with platform: Security, Monitoring, Other services, etc. See [here](https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview#integrations) 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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-runtime-overview) 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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-versions)

### 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](https://docs.microsoft.com/en-us/azure/azure-functions/functions-triggers-bindings#supported-bindings)

# Write a code

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

### Basic function

1\. Go to [Azure Portal](https://portal.azure.com) and create new resource. Select Function App and create.
![function](https://thepracticaldev.s3.amazonaws.com/i/81ht8rjlgsn7cjqk2tb1.PNG)

2\. Enter unique app name, and select OS. I select "Windows" this time. Click "Create".
![funciton](https://thepracticaldev.s3.amazonaws.com/i/9sgsk9xitallzirzh419.PNG)

3\. Once it's created, go to the resource. Select "Fucntios" and click "+", then click "New function". 
![function](https://thepracticaldev.s3.amazonaws.com/i/w50mchognsbwc6v5yjw0.PNG)

4\. Select trigger and language you want to try. I use HTTP trigger and C# here.
![function](https://thepracticaldev.s3.amazonaws.com/i/bo1lhbd5q068cby6q4d9.PNG)

5\. Click "Create" with default values. Maybe changing name is good idea though.
![function](https://thepracticaldev.s3.amazonaws.com/i/8zsfu1uvh69sgp3kc9l5.PNG)

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](https://thepracticaldev.s3.amazonaws.com/i/nzs0m7vld3no17qpr33l.PNG)

7\. Hit the URL via tool such as postman or curl. Add name parameter.
![function](https://thepracticaldev.s3.amazonaws.com/i/bum05yfbwknjb8zme7yj.PNG)

### 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](https://thepracticaldev.s3.amazonaws.com/i/7anqfovwnnrzn14ooc6e.PNG)

2\. Click "New Input" and select "Azure Blob Storage". Then click "Select".
![input](https://thepracticaldev.s3.amazonaws.com/i/491zh3meob5xdozb746w.PNG)

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](https://thepracticaldev.s3.amazonaws.com/i/sqjdgp1bmc9b3jrmevfb.PNG)

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

```csharp
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.");
}

```

5\. Click "Test" on the right and add parameter "name" with some name.
![input](https://thepracticaldev.s3.amazonaws.com/i/8plr85gpbfz78z2qt51i.PNG)

6\. Click "Save and Run"


### Developer experience

As I said, there are several tools.

#### Visual Studio extension

[Azure Functions and Web Jobs Tools ](https://marketplace.visualstudio.com/items?itemName=VisualStudioWebandAzureTools.AzureFunctionsandWebJobsTools)

It provides several things:

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

#### Visual Studio Code extension

[Azure Functions](https://marketplace.visualstudio.com/items?itemName=ms-azuretools.vscode-azurefunctions)

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](https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-overview) contains useful scenario where this helps. 

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

# Reference
[Azure Functions](https://docs.microsoft.com/en-us/azure/azure-functions/functions-overview)
[Durable Functions](https://docs.microsoft.com/en-us/azure/azure-functions/durable-functions-overview)