Your First Azure Function in Rust


Prerequisites

Install CMake

The azure-functions crate has a dependency on the grpcio crate that uses CMake to build and link against the gRPC native library.

CMake must be installed and on the PATH to be able to use Azure Functions for Rust.

Windows

Install CMake from the Windows installer.

macOS

The easiest way to install CMake on macOS is with Homebrew:

$ brew install cmake

Linux

Use your distro's package manager to install a cmake (or similar) package.

For example on Debian/Ubuntu:

$ apt-get install cmake

Install the Azure Functions Core Tools

Install version 2 or higher of the Azure Functions Core Tools.

If you are on Windows, you must add %ProgramFiles%\nodejs\node_modules\azure-functions-core-tools\bin (where func.exe is located) to the PATH environment variable.

Installing the Azure Functions for Rust SDK

Install the Azure Functions for Rust SDK using cargo install:

$ cargo install azure-functions-sdk

This installs a new cargo command named func that can be used to create and run new Azure Functions applications.

Creating a new Azure Functions application

Use the cargo func new-app command to create a new Azure Functions application:

$ cargo func new-app hello

This will create a new application in the ./hello directory with a module named functions where the exported Azure Functions are expected to be placed.

Adding a simple HTTP-triggered application

Use the cargo func new command to create a new HTTP-triggered Azure Function named hello:

$ cargo func new http -n hello

The source for the function will be in src/functions/hello.rs.

Building the Azure Functions application

To build your Azure Functions application, just use cargo build:

$ cargo build

If you are using a nightly compiler, you can enable improved error messages during compilation.

Add the following to your Cargo.toml:

[features]
unstable = ["azure-functions/unstable"]

Build your application:

$ cargo build --features unstable

This enables Azure Functions for Rust to emit diagnostic messages that will include the position of an error within an attribute.

Running the Azure Functions application

To build and run your Azure Functions application, use cargo func run:

$ cargo func run

The cargo func run command builds and runs your application locally using the Azure Function Host that was installed by the Azure Functions Core Tools.

By default, the host will be configured to listen on 0.0.0.0:8080.

For the hello function added previously, it can be invoked from http://localhost:8080/api/hello.

Debugging the Azure Functions application

The easiest way to debug the Azure Functions application is to use Visual Studio Code with the CodeLLDB extension.

Copy the example launch.json and tasks.json files to the .vscode directory inside the root of your project.

This will enable a Debug launch configuration that will build and run your application locally before attaching the lldb debugger to the Rust worker process.

Deploying the Azure Functions application

In the future, there will be a cargo func deploy command to deploy the Azure Functions application directly to Azure.

Until that time, you must manually build and push the Docker image to a repository that can be accessed by Azure.

Note: this requires Docker that is at least 18.06 for the experimental BuildKit support.

To enable the BuildKit support, set the DOCKER_BUILDKIT environment variable to 1 before running docker build.

Start by building your image with docker build -t $TAG_NAME .:

$ docker build -t $TAG_NAME .

Where $TAG_NAME is the tag name to use (e.g. username/my-functions-app).

Use docker push to push the image to a repository that is accessible to Azure Functions.

$ docker push $TAG_NAME

Create the Function App in Azure using the "Linux (Preview)" OS. Under the "Publish" setting, select "Docker Image".

From the "Configure Container" section, select the repository and enter the image you pushed.

That's it! Once the Functions App starts in Azure, you should be able to view the functions and run them.