Let’s build our first Azure Function!
Before we proceed, I would like to point out that the code of the
Azure Function that we’re going to build is available here for you to take a look if you want to.
Then, we must set Nightly as our default distribution channel:
At the moment of writing this post, it seems that
Azure Functions doesn’t support Rust at all.
For a complete list of supported languages you can take a look at the Azure Function Documentation.
Hopefully, it supports the usage of
Creating our Azure Function project
First, we’ll have to install the
Azure Functions for Rust SDK using
cargo install azure-functions-sdk
Once we have installed this, we’ll have a new
func command available that we can use to create new applications, build them and also run them locally.
Let’s create a new project and
cd into the generated folder:
cargo func new-app azure-function-rust
Open it with your IDE of choice and you should be able to see a project with the following structure:
As you can see, the
func command has generated an scaffold project with almost everything ready to run your Azure Function!
Writing our first function
We’re going to create just a
hello function that will be able to read from the
querystring and from the
name parameter and then return a sentence with this name in it. Pretty basic and simple, I know, but the idea is to focus on how to use the tool rather than the specifics of Rust.
Basically, we have to focus in the
src > functions folder. There we’ll find a
mod.rs file, which will serve as a declaration of all the exported functions that we’re going to create (one in our case).
We’ll come back to
mod.rs in a minute, but before, we must create a custom module that will contain our
hello function. In the
functions folder, just create a
hello.rs file and write this into it:
For this to properly work we will have to also install some serde crates. Open your
Cargo.toml and insert the following dependencies:
Finally, we’re going to get back to our
mod.rs file, open it and export our brand new
Running the Azure Function locally
Ok, running the function locally is super simple. Just get to your terminal and execute this:
If you browse to
http://localhost:8080 you should be able to see something similar to this:
You should be seeing something similar to the image below:
Deploying our function to Azure
As we mentioned earlier, in order to be able to use Rust we have to leverage the power of
Unfortunately, this comes at a price as we won’t be able to use the Consumption Plan, which basically means that you lose the ability to pay only while your functions are running. As you will see in a moment, we’ll have to create a custom
App Service Plan.
But before that, we have to build our project, create a
Docker image and push it to
Docker Hub (or a custom registry of yours).
Let’s do it!
Building the Azure Function
Let’s build our
cargo func build
This will create a
Docker image called
azure-functions/<name_of_your_project>. In our case, if you do
docker images you should be able to see a
Docker image called
Now, in order to push it to
Docker Hub we must log in using our
Let’s create a new
docker tag azure-functions/azure-function-rust <username>/azure-function-rust
# eg. docker tag azure-functions/azure-function-rust robertohuertasm/azure-function-rust
If you do
docker images again you should see the tag.
Finally, let’s push it to
Docker Hub and check that the image has been correctly uploaded:
docker push <username>/azure-function-rust
Creating our Azure Function in the Azure Portal
Go to your Azure Portal and click over the
Create a resource option.
Function App and create one.
Then just fill the form accordingly:
Be sure that you choose the right
App Service Plan for you according to your location and the pricing tier.
Finally, let’s configure the container and create the
Now wait for it to be deployed.
Checking that every thing works as expected
Azure Function is deployed just browse to its corresponding URL and check that everything is ok. In my case, the Url is https://azure-function-rust.azurewebsites.net.
If you just browse there you’ll see the same blue screen that we saw when testing this locally so let’s go ahead and browse to https://azure-function-rust.azurewebsites.net/api/hello?name=Reader.
Are you seeing this?
Try now to make a
POST request and see what happens!
As you can see, creating an Azure Function using Rust is pretty straightforward. Nevertheless, it feels a little bit too much to have to use Docker for this and having to give up to the Consumption Plan.
In my opinion, although writing AWS Lambdas in Rust feels a little bit convoluted it seems a better option if you’re willing to save some money and leverage the auto-scaling functionalities. Let’s hope Microsoft adds support for Rust soon!
Originally published at robertohuertas.com on December 22, 2018.