3
votes

How to build serverless web site on Azure, as "auto-scaling, pay-per-execution, event-driven app"?

There are tons of good examples how to build serverless-arhitecture web site on AWS Amazon, e.g. https://zanon.io/posts/building-serverless-websites-on-aws-tutorial It consumes S3 for HTML and JS, Lambda for REST API, Simple DB for data.

Microsoft has Azure Functions that is analogue for AWS Lambda, but it is "serverless computing", not "serverless web site". I can create "serverless REST API" with Azure Functions, but what about HTML, JS, CSS for web site, database, etc. ?

I tried Azure App Service, but it lacks "pay only for what you use" option, as all plans have Monthly payments, as well as Azure SQL for database. And App Service doesn't seem to be constructed to host serverless-architecture web sites, more for classic ASP.NET web sites that you can easily deploy there.

Also, there is popular library https://github.com/serverless/serverless and they even mentioned Azure: "using AWS Lambda, Azure Functions, Google CloudFunctions & more", but there is no a single example how to use it for Azure and all Docs are for Amazon AWS.

Thanks!

3
You can refer to the following document to compare AWS to Azure services and find the proper match: docs.microsoft.com/en-us/azure/guidance/…Simon Luckenuik

3 Answers

4
votes

This is an interesting question, and the answer is that you can certainly build a website using Azure functions. You can create a http trigger that returns a static html file that you include in your function. If you want, you can add Javascript or jQuery logic to that file. For even more functionality, you can create more functions that will serve as an API, which you can query from your html file. The API could e.g. utilize Azure storage, where you can cheaply store data in tables or blobs.

Using this pattern, you could in theory build complex web apps on a serverless architecture. I would guess, however, that you would probably find it a bit cumbersome to work with, as the tooling is not really great yet. On the other hand, you would never need to think about hosting/scaling, so that is a big plus.

Just to prove my theory, I implemented a small function app with two c# http trigger functions: One that serves an html file, another one for the api that returns a number. It is just thrown together quickly to prove the point, so I apologize if the code is messy.

HttpTriggerCSharp1:

using System.Net;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.IO;

public static async Task<HttpResponseMessage> Run(HttpRequestMessage req,    TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");

var response = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(@"d:\home\site\wwwroot\HttpTriggerCSharp1\index.html", FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentType = new    MediaTypeHeaderValue("text/html");
return response;
}

HttpTriggerCSharp2:

using System.Net;

public static async Task<int> Run(HttpRequestMessage req, TraceWriter log)
{
    return 42;
}

index.html: (Note that you will have to add the index.html file to your function files collection)

<html>
<head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
</head>
<body>
<script type="text/javascript">
    $(document).ready(function() {
        var original = $("#testDiv").text();
        $("#testDiv").text(original + " - jQuery added this!");
        $("#testButton").click(ButtonClick);
    });

    var i = 0;
    function ButtonClick() {
        i++;
        var result = i + i;
        $.ajax("https://testfunctionwebsite.azurewebsites.net/api/HttpTriggerCSharp2").done(function(a,b) {
            var queryResult = a;
            $("#ajaxResult").text("Result from api:" + queryResult);
        });
        $("#ajaxResult").text(result);

    }
</script>
<div id="testDiv">
Hello functional world!
</div>

<div>
<button id="testButton">Get some values</button><br>
<div id="ajaxResult"></div>
</div>
</body>
</html>

You can try it out at: https://testfunctionwebsite.azurewebsites.net/api/HttpTriggerCSharp1

1
votes

For storing static files, you can use Azure Storage, that is comparable to AWS S3: https://blogs.msdn.microsoft.com/make_it_better/2016/08/09/simple-websites-using-azure-storage-blob-service/

For storing data - there are many options, but if you are looking for NoSQL style DB (implied by you reference of AWS SimpleDB) then you can use Azure DocumentDB: https://docs.microsoft.com/en-us/azure/documentdb/documentdb-introduction

Both components are "server-less" and pay as you go style billing.

0
votes

Azure storage accounts now support hosting static websites (currently in preview) to provide serverless websites. This works for static websites like Angular and React SPA applications but does not provide any server side compute like ASP.NET MVC. It can be used with Azure Functions Proxies for additional features. I wrote a post highlight the some features and how to set one up. http://www.deliveron.com/blog/serverless-websites-azure-new-storage-account-static-websites/