Tim McNamara provides a walkthrough of creating an Azure Function using Rust. He explains the setup, code building, configuration, and running the function locally. Despite encountering errors, Tim successfully creates the function and highlights additional documentation for scaling it in Azure.
Tim McNamara walks through the process of creating an Azure Function using Rust as the programming language. Tim begins by explaining the necessary setup, including having an Azure account and Visual Studio code. He directs attention to the Azure documentation’s Quickstart Guide, which proves to be a valuable resource for the task.
Tim highlights the limited support for Rust as a language but suggests using the custom handler functionality to enable Azure to respond to any HTTP request. He proceeds to provide step-by-step instructions, starting with creating a resource and selecting the directory. Tim chooses the name “Zap” for the function and opts for the “Custom Handler” and “HTTP trigger” options. The template code is generated accordingly.
Next, Tim explains the process of creating and building the code using the Cargo package manager. He mentions the necessary dependencies, such as the Warp framework and Tokio for asynchronous operations. Tim copies the code into the appropriate files and proceeds to build and release it.
Tim then examines how the function operates, focusing on the path created by Warp. He demonstrates making changes to the path and recompiling the code. After moving the handler file to the appropriate location, Tim moves on to configuring the function app by modifying the host.json file. He adjusts the custom handler description and enables forwarding of HTTP requests.
With the configuration complete, Tim runs the function locally using the `func` command. Although he encounters some errors and accidentally deletes his code, he persists and manages to fix the issues. Tim successfully creates his first function, expressing his satisfaction with the outcome.
Lastly, Tim mentions that there is additional documentation available for configuring the function in Azure to make it accessible to anyone on the internet in a scalable manner. He emphasizes the scalability potential of Azure and its cloud capabilities.
Okay, let’s have a go at creating at Azure Function in Visual Studio code.
It turns out that there is a really useful doc which describes this in the Quickstart Guide within the Azure documentation at learn.microsoft.com.
There isn’t a lot of support for Rust as a language, but we can use the custom handler functionality: to enable Azure to just respond to any HTTP request.
There’s a bunch of setup we need to do. We need an Azure account. We need Visual Studio code. And so we then start to get some instructions and we need to create a resource. And so on my side, on the left here, I can go into the Azure button and open up, And then I click “Create Function” I’ve got a … I need to choose the directory so I’ll just see if I can create one.
I’m going to call it Zap*because that sounds like a neat name; and I can choose different languages. I’m going to go for `Custom Handler` and HTTP trigger and provide the function name. And it will be … I’m just going to call it HTTP zap function anonymous admin authorization. We want `Anonymous` which will enable anyone to call this function. We then get a bunch of template [code] created for us.
And so the next question or the next step is to create and build it.
We go into a terminal and then I run
cargo init --name handler open in the function app root (the same folder of host.json). So I’ll just see where I am. Now if I go into `./zap`, I’ve got
host.json there I say
cargo init --name handler which is my Rust code.
And then in the
Cargo.toml file it says that we should include the Warp framework as well as Tokio which is the Async runtime (
rt), runtime macros, make things a little bit simpler and RT multithreaded make use of make means that we can make use of all of our calls. Cargo.toml file is rust’s dependency metadata format. And then in
src/main.rs, I was going to here with inside the `src/main.rs` and I’m just going to copy and paste this directly.
It’s a little bit unclear to read at the moment because I’ve got a lot going on in the screen.But we have some sort of state and we are mapping I think names to … two things. Otherwise well, we’ve got API example and let’s call this… We get a name and then we find and if we don’t have one we have like a generic message.
Okay, sorry. Cool. Okay.
So now we build and then release so … I can run the [cargo] build command.
So this will take a while to work. I need to go and download a lot of data, but hopefully so I’ve got 133 crates to download and compile then once that works […] so let’s go back to looking at how this function actually operates.
Warp is creating a path. So I’ve got “
api” and then http example if we change this path, let’s say to “Greet”, it will change the URL parameters that are specified.
So I’ve gone and built my code. I’ll just change it so that recompiles it with new handler. And I now need to move the handler … Sorry, `cargo`, the function that invokes the Rust compiler, it’s sort of a package manager, has put a file called handler—or if you’re in Windows,
handler.exe—in this `./target/release` directory and we need to move it into the root of the package that has `./host.json` And now we can wire things together.
Okay, so I need to open up, it says here configure your function app: open `host.json`. Okay. In the custom handler description section, set the value of direct default executable path to `”handler”`. In the custom handler section, add a property named `enableForwardingHttpRequest` to set that to `true` confirm the customer handler looks a little bit like this.
Okay, I want looks a slightly different because we put the one thing at the top, make it a little bit more so it just looks exactly the same.
Now we can run the function locally this `func` command is created from it’s actually in part of the requirements that came … it was part of those requirements that were … That’s not right. It was one of the dependencies that we were asked to install right at the start.
Oh gosh, selected the wrong thing. [Copy] [that’s not a real command. ]
Okay, so we got an HTTP success which is almost useful now I want to be able to say API. /Greet Nothing. I wonder if I need to say name equals tim. (`/api/HTTPZap?name=Tim`)
Maybe I will take this back and say stop touching it before it actually works.
Oh gosh, I’ve just gone and deleted my code.Wait … that’s not very satisfactory.
What’s going on here? First I’ll fix this again and we will try to … that won’t work. That’s not working either.
I wonder if I need to go and define something else. So I wonder if this actually needs to be http zap and close it down clear. And then I’m going to recompile and reload.
So … copy it back into the root and then `func start` and then it’s setting itself up and sort of said that there’s this HTTPZap thing.
AHA, hello term this HTTP triggered okay, so if I don’t play with the example code, it works fine. (Joyous Tim laughter) Oops. Okay, there we go, there we have it.
We have created our first function: actually relatively simply. So that was cool. And sorry about the problems that I encountered.
There is actually a whole bunch of extra documentation for getting things configured and working inside Azure to be able to um, enable anyone on the internet to access your function essentially in infinitely scalable way, or I suppose virtually infinite, or as big as the cloud. As big as Azure can go.