In this post, I am going to show you a quick example working with Azure Functions and Docker Containers. We are going to code our function in C# with DotNet Core using Visual Studio Code as our IDE. This is a great oportunity to dive into the new flexibility that came up with DotNet Core platform, which alows us to code, compile and run .NET software beyond Microsoft enviroments.
We are going to implement an azure function, run it locally and interetact with it using Post Man. After that, we are going to create a Docker image with our Azure Function, then again,we will run and interact locally, now with our Docker image running.
Brief tech context:
- Azure Functions and Serverless
- Docker containers
Setting up our enviroment:
- Install Dotnet sdk for Ubuntu
- Install Azure Functions Tools
- Install Docker
Let’s do this!
- Business context for our Azure Function
- Create and run our Azure Function
- Dockerize and run
Azure Functions and Serverless
Azure functions is one of the newest features of Azure Cloud Platform services. It comes with the idea of Faas (Function as a service) and serverless. This allows us, as programmers, to only worry about the code, delegating all environment setting tasks to Azure platform.
Azure Functions gives programmers the oportunity to write their pieces of code in serveral languages and deploy it easily, getting rid of the efforts that come with building and publishing servers . You can find more information about this service here.
At some point in the history of software developing, we had to deal with the scalability issue. So we needed to deploy several instances of our applications. By then we took the decision of building virtual machines, and scale our application using those tools. It worked well for a while and it was also cheaper than buying and deploying new physical servers, it seems to be a right desision, we did not want to get involve in troubles with the coexistence of the several dependencies of our applications, but.. was it worth ? We were replicating at least 100 GB of space and other resources (Operative Sistem and all random and needless software that came with them) that a virtual machines needs only for our 2 GB weight application.
What about if we can just freeze all our application and its dependencies in an image. Then we can replicate and run it in as many instances we want, and all of them coexisting in the same operative system. Yes, we want to encapsulate our applications in a something like containers, of course Docker Containers.
Setting up our enviroment
Install Dotnet sdk for Ubuntu
Well, as we are going to work with Linux Ubuntu enviroment and .Net Core application, we need to install DotNet Core SDK. In my case, while I was installing it in my Ubuntu 18.04, I got some dependencies troubles, so I needed to install DotNetCore 2.1 first. If you got this problem, you should follow this first before installing DotNetCore SDk 2.2.
Install Azure Functions Tools
You will be able to get Docker tools installed following this Docs
Let’s do this!
Business context for our Azure Function
Now, we will get hands on building our azure function. Azure functions are a geat solution for that cases where whe want to encapsulate some behavior we don’t want to scatter all over lots of applications, in this case we want to develop the person admission for some feature.
We are going to develop an azure function that is triggered by HttpRequest and recieve in the request body a person and then we are going to return a json with the data we got and adding a property which means if that person is admitted or not. In order to keep this implementation easy, we are just going to check the age of the person we received. If the person age is greater or equal than 18 we return admitted = true, otherwise it will be false.
Create and run our Azure Function
After getting our enviroment set up , we are going to create the folder where we want to set our azure function and in a terminal we need to run this command:
After that, we will run this command in order to get our first function initialized:
Then if we open our folder in Visual Studio Code, we will notice all the files that azure tools have created for us. It creates a standard example function:
Now, if we just run our code using Visual Studio Code Tools we will got the Azure Function running locally and the command line will provide us an endpoint to call for it.
Okey, now we will test our Azure Function using Post Man:
We can notice we send an object which represent for us a person, with 2 properties (name and age), and as the age is greater than 18 the azure funcion returns a json with the same data, adding the property admitted = true.
Dockerize and run
Cool, we got our Azure Function running. Now it is time to freeze it in a docker image and then run it.
First, we need to make some changes in our code. We need to change the authorization level for HttpTriger Atribute. It works fine when we are running azure function locally, but it will return us a 401 request error when whe run our Azure Function inside a Docker Container. We need to set AuthorizationLevel to Anonymous.
After have made this changes, we need to build our Docker image:
Once we got our Docker image built, we can check it:
And then, we can run it:
Now again, we are going to test it with Post Man:
Also You can check you Containers running:
And turn them down:
We had developed an Azure Function and combined it with Docker Containers, this are two pretty good tools now we have to develop our applications. Easily we can develop endpoints which can work out some stuff and get rid of all the heavy work that setting up servers demands.
Nervertheless I can’t avoid telling you about some awkward issues that I found while I was diving into Azure Function stuff:
If you took a read the documentation tha Azure Functions provides, you can easily notice that this feature has the advantage of scaling limitless. But you can bump into some problems with it:
At developer side:
Azure Functions can scale limitless but it does not necessarily mean that your third party server will. So the limitless scalability of azure functions can make troubles with the third party server you are using (ie: APIs, DataBases servers)
At Azure Cloud Platform side:
Your Azure Functions instances are limitless, right, but the sockets that azure provides to you are not. So if you are requesting third party servers with several instances in your Azure Function you can run out of the available sockets that the platform provides you.
All Links mentioned in this post :
Serverless architectures replace a managed server with a collection of third party services and FaaS
Function as a service - Wikipedia
Function as a service ( FaaS) is a category of cloud computing services that provides a platform allowing customers to…
Azure Functions Overview
Understand how to use Azure Functions to optimize asynchronous workloads in minutes.
What is a Container
Following this evolution, Docker continues to give back with the containerd project, which Docker donated to the Cloud…
Install .NET Core SDK on Linux Ubuntu 18.04 x64 | .NET
Instructions for installing .NET Core SDK on Linux Ubuntu 18.04 using a package manager.
Installing DotNet Core in Ubuntu 18.04
Carlos Fabara Apr 28 '18Since the time this article was published Microsoft has released the official way of installing…
How to install Node.js and npm on Ubuntu 18.04
Get Docker CE for Ubuntu
To get started with Docker CE on Ubuntu, make sure you meet the prerequisites, then install Docker. Prerequisites…
Contribute to mmiguenz/azure-function-example development by creating an account on GitHub.
Join our community Slack and read our weekly Faun topics ⬇