With the new serverless craze I decided to create myself a little challenge to explore Azure Functions. The challenge I came up with was moving my personal website from a CMS to a static site hosted by Functions. This post covers how I achieved this.
I am not going to be covering what Functions are in this post. For more information on Functions please see my previous post: Introduction To Azure Functions
The first step involved moving the content of my website away from a dated Wordpress install. I investigated various static site generators and ended up using the amazing, brilliant, fantastic (I can’t praise this thing enough…), Hugo.
The migration was surprisingly quick due to the great templating features provided by Hugo.
The content was uploaded to a blob storage container so that I can easily access it from the function.
Once the content was ready I needed to figure out a way to serve it. Enter Functions…
Serving static content with Functions is surprisingly easy. You have access to proxies which can used to proxy any request to the function through to a blob storage container. This is however not the solution I implemented as I found various problems with this approach.
Firstly, blob storage has no concept of a default document. Imagine you have the following file in blob storage:
A request made to
/content/about-me/ will not work. In a normal setup
Nginx or Apache will take care of this for us and automatically load the
The above can be fixed by adding some clever routing, but the routing setup quickly gets out of hand the deeper your folder structure goes.
The second problem, although minor, was that with the proxy approach, the 404 page needs to be hard coded in the function itself. Current me likes to save future me a lot of pain by keeping things together in a logical way. This means keeping the 404 page content together with all my other content.
The Custom “Proxy”
Keeping the above in mind I decided to find a way to get the content from the blob, but keep my routes completely dynamic with the option to display the 404 page from my blob if the original file could not be found. This is what I came up with:
The content of the function.json file is as follows:
The above creates a function with a catch-all route pattern. It gets the requested path and then does some sanity checks. It first tries to retrieve the requested file and if that fails it will retrieve the 404 page. Once it has the content it will close the current context with the raw content set to the Buffer we created by overriding the write method on our stream.
Now we have our content in a blob and a function that can return it. The next problem to solve was the contact form.
The Contact Form API
Again, this is super easy with Functions. You have access to SendGrid bindings for output in your Function.
Once you have an account set up (This can be done through the Azure Portal) all you need to do is add an application setting containing your API key. The function.json should look something like this:
And a very basic Function to use this will look something like this:
Now all that was needed was to post the content of a form via ajax to the URL for this function and viola! A serverless contact form!
The last part of the challenge was to somehow set up continuous deployment for both the function and the content.
To set up continuous delivery for the function, all I needed to do was set up the deployment options and connect it to my GitHub repo.
Now every time I push my latest changes it will automatically deploy it.
I thought the content part might be where this challenge beats me, but luckily I then remembered that Visual Studio Team Services is free for up to 5 users.
In VSTS I could set up a build pipeline that again automatically pulls my latest changes from GitHub and copies them over to the blob storage.
The most important thing to remember here is that you need to set
/SetContentType as an additional argument for the File Copy step, or
all your files will default to octet-stream content type.
A lot of research, playing and testing and I now have a completely serverless website with continuous deployment.
Another benefit of this move is the price. With Azure Functions your first 1 000 000 requests per month are free.
You can find the source code for the above Function on GitHub: https://github.com/geevcookie/functions-static-content