Azure Functions

The quarkus-azure-functions extension is a simple integration point between Azure Functions and Quarkus. It interacts with Azure Functions runtime to bootstrap quarkus and turns any Azure Functions class you write into a CDI/Arc bean.

This allows you to inject any service or component initialized by quarkus directly into your function classes. You can also change the lifecycle of your function class from request scoped (the default) to application scope too if you want your function class to be a singleton.

import com.microsoft.azure.functions.ExecutionContext;
import com.microsoft.azure.functions.HttpMethod;
import com.microsoft.azure.functions.HttpRequestMessage;
import com.microsoft.azure.functions.HttpResponseMessage;
import com.microsoft.azure.functions.HttpStatus;
import com.microsoft.azure.functions.annotation.AuthorizationLevel;
import com.microsoft.azure.functions.annotation.FunctionName;
import com.microsoft.azure.functions.annotation.HttpTrigger;

import jakarta.inject.Inject;
import java.util.Optional;

public class Function {
    @Inject
    GreetingService service;

    @FunctionName("HttpExample")
    public HttpResponseMessage run(
            @HttpTrigger(
                name = "req",
                methods = {HttpMethod.GET, HttpMethod.POST},
                authLevel = AuthorizationLevel.ANONYMOUS)
                HttpRequestMessage<Optional<String>> request,
            final ExecutionContext context) {

        // Parse query parameter
        final String query = request.getQueryParameters().get("name");
        final String name = request.getBody().orElse(query);

        if (name == null) {
            return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build();
        } else {
            return request.createResponseBuilder(HttpStatus.OK).body(service.greeting(name)).build();
        }
    }
}

This technology is considered preview.

In preview, backward compatibility and presence in the ecosystem is not guaranteed. Specific improvements might require changing configuration or APIs, and plans to become stable are under way. Feedback is welcome on our mailing list or as issues in our GitHub issue tracker.

For a full list of possible statuses, check our FAQ entry.

Prerequisites

To complete this guide, you need:

Solution

This guide walks you through running a maven project that can deploy an Http Trigger Azure Function class. This function class injects a CDI bean service that generates a greeting message that is passed back to the client.

Creating the Maven Deployment Project

You can download the example code from Quarkus’s application generator at this link.

You can also generate this example with the Quarkus CLI:

quarkus create app --extension=quarkus-azure-functions

Login to Azure

If you don’t log in to Azure you won’t be able to deploy.

az login

Quarkus dev mode

Quarkus dev mode does not work currently with Azure Functions.

Run locally in Azure Functions simulated environment

If you want to try your app with a simulated local Azure Functions environment, you can use this command

./mvnw clean package azure-functions:run

Note that you must have the Azure Functions Core Tools installed for this to work!

Deploy to Azure

The pom.xml you generated in the previous step pulls in the azure-functions-maven-plugin. Running maven package generates config files and a staging directory required by the azure-functions-maven-plugin. Here’s how to execute it.

./mvnw clean package azure-functions:deploy

If deployment is a success, the azure maven plugin will tell you the base URL to access your function.

i.e.

[INFO] HTTP Trigger Urls:
[INFO] 	 HttpExample : https://{appName}.azurewebsites.net/api/httpexample

The URL to access the service would be

Extension maven dependencies

You must include the quarkus-azure-functions extension as this is the integration point between Quarkus and Azure Functions. It registers callback with the Azure Functions runtime to bootstrap Quarkus and to set up Quarkus/Arc as the function factory for your function classes.

Azure Deployment Descriptors

Templates for Azure Functions deployment descriptors (host.json, function.json) are within base directory of the project. Edit them as you need to. Rerun the build when you are ready.