Azure Functions
Since Camel 4.19
Only producer is supported
The azure-functions component enables integration with Azure Functions. Azure Functions is a serverless compute service that enables you to run code on-demand without having to explicitly provision or manage infrastructure.
This component supports:
-
Invoking HTTP-triggered Azure Functions
-
Managing Azure Function Apps (list, create, delete, start, stop, restart)
-
Managing function app configuration and tags
Prerequisites
You must have a valid Azure subscription. More information is available at Azure Documentation Portal.
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-azure-functions</artifactId>
<version>${camel-version}</version>
</dependency> where ${camel-version} must be replaced by the actual version of Camel.
Configuring Options
Camel components are configured on two separate levels:
-
component level
-
endpoint level
Configuring Component Options
At the component level, you set general and shared configurations that are, then, inherited by the endpoints. It is the highest configuration level.
For example, a component may have security settings, credentials for authentication, urls for network connection and so forth.
Some components only have a few options, and others may have many. Because components typically have pre-configured defaults that are commonly used, then you may often only need to configure a few options on a component; or none at all.
You can configure components using:
-
the Component DSL.
-
in a configuration file (
application.properties,*.yamlfiles, etc). -
directly in the Java code.
Configuring Endpoint Options
You usually spend more time setting up endpoints because they have many options. These options help you customize what you want the endpoint to do. The options are also categorized into whether the endpoint is used as a consumer (from), as a producer (to), or both.
Configuring endpoints is most often done directly in the endpoint URI as path and query parameters. You can also use the Endpoint DSL and DataFormat DSL as a type safe way of configuring endpoints and data formats in Java.
A good practice when configuring options is to use Property Placeholders.
Property placeholders provide a few benefits:
-
They help prevent using hardcoded urls, port numbers, sensitive information, and other settings.
-
They allow externalizing the configuration from the code.
-
They help the code to become more flexible and reusable.
The following two sections list all the options, firstly for the component followed by the endpoint.
Component Options
The Azure Functions component supports the following options which are listed below.
| Name | Description | Default | Type |
|---|---|---|---|
The component configuration. | FunctionsConfiguration | ||
The timeout in milliseconds to connect to the function app for invokeFunction. Must be greater than 0. | 30000 | int | |
The HTTP method of the invokeFunction request, such as GET, POST, PUT or DELETE. GET and DELETE send no body. | POST | String | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
The Azure region of the function app that createFunctionApp creates, such as eastus or westeurope. Required for createFunctionApp. | String | ||
The operation to perform. invokeFunction calls an HTTP-triggered function; the other operations manage the function app through Azure Resource Manager and need subscriptionId and resourceGroup. The CamelAzureFunctionsOperation header overrides it. Enum values:
| invokeFunction | FunctionsOperations | |
The timeout in milliseconds to wait for the response of the function for invokeFunction (the request timeout of the Java HTTP client): the exchange fails with an HttpTimeoutException when it elapses. Must be greater than 0. | 60000 | int | |
The language runtime of the function app that createFunctionApp creates: java, node, python or dotnet (case-insensitive), set as the FUNCTIONS_WORKER_RUNTIME app setting. Another value or no value sets no FUNCTIONS_WORKER_RUNTIME. | String | ||
The runtime version of the function app that createFunctionApp creates. Not used yet: createFunctionApp always sets the Functions runtime version 4. | String | ||
The connection string of the storage account of the function app that createFunctionApp creates, set as the AzureWebJobsStorage app setting. Required for createFunctionApp. | String | ||
Option to disable throwing the HttpOperationFailedException when the function answers invokeFunction with an HTTP status of 400 or above. This allows you to get all responses regardless of the HTTP status code, with the status in the CamelAzureFunctionsStatusCode header. | true | boolean | |
Autowired The AppServiceManager that the management operations use, instead of one that the component creates from the subscriptionId, tenantId and credential options. | AppServiceManager | ||
Whether autowiring is enabled. This is used for automatic autowiring options (the option must be marked as autowired) by looking up in the registry to find if there is a single instance of matching type, which then gets configured on the component. This can be used for automatic configuring JDBC data sources, JMS connection factories, AWS Clients, etc. | true | boolean | |
The client ID of the Microsoft Entra application (service principal) that the management operations authenticate with. Used only when clientId, clientSecret and tenantId are all set and credentialType is AZURE_IDENTITY or FUNCTION_KEY. | String | ||
The client secret of the Microsoft Entra application (service principal) that the management operations authenticate with. Used only when clientId, clientSecret and tenantId are all set and credentialType is AZURE_IDENTITY or FUNCTION_KEY. | String | ||
How the management operations authenticate to Azure. AZURE_IDENTITY and FUNCTION_KEY use the client secret credential when clientId, clientSecret and tenantId are all set, else DefaultAzureCredential; TOKEN_CREDENTIAL uses the tokenCredential option. Not used when appServiceManager is set, nor by invokeFunction, which sends the functionKey or hostKey when one is set. Enum values:
| AZURE_IDENTITY | CredentialType | |
The function key that invokeFunction sends in the x-functions-key HTTP header. Takes precedence over hostKey. Not used by the management operations. | String | ||
The host key of the function app that invokeFunction sends in the x-functions-key HTTP header when functionKey is not set. Not used by the management operations. | String | ||
The name of the resource group of the function app, used by the management operations: listFunctionApps lists the function apps of this resource group. Required when the operation of the endpoint is a management operation (any operation other than invokeFunction). | String | ||
The ID of the Azure subscription of the function app, used by the management operations. Required when the operation of the endpoint is a management operation (any operation other than invokeFunction). | String | ||
The Microsoft Entra tenant ID of the management operations: used with clientId and clientSecret, and as the tenant of the Azure profile of the management client. | String | ||
Autowired The TokenCredential that the management operations authenticate with when credentialType is TOKEN_CREDENTIAL. | TokenCredential |
Endpoint Options
The Azure Functions endpoint is configured using URI syntax:
azure-functions:functionApp/functionName
With the following path and query parameters:
Query Parameters
| Name | Description | Default | Type |
|---|---|---|---|
The timeout in milliseconds to connect to the function app for invokeFunction. Must be greater than 0. | 30000 | int | |
The HTTP method of the invokeFunction request, such as GET, POST, PUT or DELETE. GET and DELETE send no body. | POST | String | |
The Azure region of the function app that createFunctionApp creates, such as eastus or westeurope. Required for createFunctionApp. | String | ||
The operation to perform. invokeFunction calls an HTTP-triggered function; the other operations manage the function app through Azure Resource Manager and need subscriptionId and resourceGroup. The CamelAzureFunctionsOperation header overrides it. Enum values:
| invokeFunction | FunctionsOperations | |
The timeout in milliseconds to wait for the response of the function for invokeFunction (the request timeout of the Java HTTP client): the exchange fails with an HttpTimeoutException when it elapses. Must be greater than 0. | 60000 | int | |
The language runtime of the function app that createFunctionApp creates: java, node, python or dotnet (case-insensitive), set as the FUNCTIONS_WORKER_RUNTIME app setting. Another value or no value sets no FUNCTIONS_WORKER_RUNTIME. | String | ||
The runtime version of the function app that createFunctionApp creates. Not used yet: createFunctionApp always sets the Functions runtime version 4. | String | ||
The connection string of the storage account of the function app that createFunctionApp creates, set as the AzureWebJobsStorage app setting. Required for createFunctionApp. | String | ||
Option to disable throwing the HttpOperationFailedException when the function answers invokeFunction with an HTTP status of 400 or above. This allows you to get all responses regardless of the HTTP status code, with the status in the CamelAzureFunctionsStatusCode header. | true | boolean | |
Whether the producer should be started lazy (on the first message). By starting lazy you can use this to allow CamelContext and routes to startup in situations where a producer may otherwise fail during starting and cause the route to fail being started. By deferring this startup to be lazy then the startup failure can be handled during routing messages via Camel’s routing error handlers. Beware that when the first message is processed then creating and starting the producer may take a little time and prolong the total processing time of the processing. | false | boolean | |
Autowired The AppServiceManager that the management operations use, instead of one that the component creates from the subscriptionId, tenantId and credential options. | AppServiceManager | ||
The client ID of the Microsoft Entra application (service principal) that the management operations authenticate with. Used only when clientId, clientSecret and tenantId are all set and credentialType is AZURE_IDENTITY or FUNCTION_KEY. | String | ||
The client secret of the Microsoft Entra application (service principal) that the management operations authenticate with. Used only when clientId, clientSecret and tenantId are all set and credentialType is AZURE_IDENTITY or FUNCTION_KEY. | String | ||
How the management operations authenticate to Azure. AZURE_IDENTITY and FUNCTION_KEY use the client secret credential when clientId, clientSecret and tenantId are all set, else DefaultAzureCredential; TOKEN_CREDENTIAL uses the tokenCredential option. Not used when appServiceManager is set, nor by invokeFunction, which sends the functionKey or hostKey when one is set. Enum values:
| AZURE_IDENTITY | CredentialType | |
The function key that invokeFunction sends in the x-functions-key HTTP header. Takes precedence over hostKey. Not used by the management operations. | String | ||
The host key of the function app that invokeFunction sends in the x-functions-key HTTP header when functionKey is not set. Not used by the management operations. | String | ||
The name of the resource group of the function app, used by the management operations: listFunctionApps lists the function apps of this resource group. Required when the operation of the endpoint is a management operation (any operation other than invokeFunction). | String | ||
The ID of the Azure subscription of the function app, used by the management operations. Required when the operation of the endpoint is a management operation (any operation other than invokeFunction). | String | ||
The Microsoft Entra tenant ID of the management operations: used with clientId and clientSecret, and as the tenant of the Azure profile of the management client. | String | ||
Autowired The TokenCredential that the management operations authenticate with when credentialType is TOKEN_CREDENTIAL. | TokenCredential |
Message Headers
The Azure Functions component supports the following message header(s), which is/are listed below:
| Name | Description | Default | Type |
|---|---|---|---|
CamelAzureFunctionsOperation (producer) Constant: | The operation to perform. Overrides the operation in the endpoint. Enum values:
| FunctionsOperations | |
CamelAzureFunctionsFunctionApp (producer) Constant: | The name of the function app. Overrides the function app of the endpoint. | String | |
CamelAzureFunctionsFunctionName (producer) Constant: | The function within the function app. Overrides the function name of the endpoint. | String | |
CamelAzureFunctionsResourceGroup (producer) Constant: | The name of the resource group of the function app. Overrides the resourceGroup option. | String | |
CamelAzureFunctionsHttpMethod (producer) Constant: | The HTTP method of the invokeFunction request. Overrides the httpMethod option. | String | |
CamelAzureFunctionsHttpHeaders (producer) Constant: | The HTTP headers to send with the invokeFunction request, as a Map of header names to values. | Map | |
| Constant: | The content type of the body that invokeFunction sends. Used when the request has a body and the CamelAzureFunctionsHttpHeaders map sets no Content-Type; when neither sets one, application/json is sent. | String | |
CamelAzureFunctionsStatusCode (producer) Constant: | The HTTP status code of the response of invokeFunction. | Integer | |
CamelAzureFunctionsResponseHeaders (producer) Constant: | The HTTP headers of the response of invokeFunction. | Map | |
CamelAzureFunctionsFunctionAppState (producer) Constant: | The state of the function app, such as Running or Stopped. Set by getFunctionApp, startFunctionApp, stopFunctionApp and restartFunctionApp. | String | |
CamelAzureFunctionsResourceId (producer) Constant: | The Azure resource ID of the function app. Set by getFunctionApp and createFunctionApp. | String | |
CamelAzureFunctionsDefaultHostname (producer) Constant: | The default host name of the function app. Set by getFunctionApp and createFunctionApp. | String | |
CamelAzureFunctionsAppSettings (producer) Constant: | The app settings that updateFunctionAppConfiguration adds or changes, as a Map of names to values. Required for updateFunctionAppConfiguration. | Map | |
CamelAzureFunctionsResourceTags (producer) Constant: | The tags that tagResource adds to the function app or changes, as a Map of names to values. Required for tagResource. | Map | |
CamelAzureFunctionsTagKeys (producer) Constant: | The names of the tags that untagResource removes from the function app. Required for untagResource. | List | |
CamelAzureFunctionsLocation (producer) Constant: | The Azure region of the function app that createFunctionApp creates. Overrides the location option. | String | |
CamelAzureFunctionsRuntime (producer) Constant: | The language runtime of the function app that createFunctionApp creates. Overrides the runtime option. | String | |
CamelAzureFunctionsRuntimeVersion (producer) Constant: | The runtime version of the function app that createFunctionApp creates. Not used yet. | String | |
CamelAzureFunctionsStorageConnectionString (producer) Constant: | The connection string of the storage account of the function app that createFunctionApp creates. Overrides the storageAccountConnectionString option. | String |
Message Body
The message body at each side of the Azure Functions component: Consumer is the body of the messages the consumer creates, Consumer reply the bodies a consumer that answers its caller can reply with, Producer input the bodies the producer can send or write, and Producer output the body after the producer.
| Side | Body | Description |
|---|---|---|
Producer input |
| The request body of invokeFunction, the default: text, a number or a boolean is sent as text (UTF-8), and bytes, a stream or a file as their content. A Map or a POJO is not marshalled: marshal it to JSON first, as a body that does not convert to bytes fails. The Content-Type is the one the CamelAzureFunctionsHttpHeaders header sets, else the Content-Type header of the message, else application/json. GET and DELETE send no body. The management operations do not use the body: they take their input from headers, such as the app settings and the tags.
|
Producer output |
| The response body of the function as text for invokeFunction, the default, with the status in the CamelAzureFunctionsStatusCode header. A status of 400 or above fails the exchange with an HttpOperationFailedException, unless throwExceptionOnFailure=false, which keeps the response as text. A List of Maps that describe the function apps or the functions for the list operations, a Map for the get and create operations, the settings or the tags as a Map, and true for delete, start, stop and restart.
|
The bodies are the ones with the default options. A side that is not listed is not known, and tooling does not guess it. The carriers, the sides and the tools that use them are described in Message Body Types.
URI Format
azure-functions:functionApp[/functionName] Where:
-
functionAppis the name of your Azure Function App -
functionNameis the name of the function within the app (required forinvokeFunctionoperation)
The invokeFunction operation calls https://<functionApp>.azurewebsites.net/api/<functionName>. The function app, from the endpoint or the CamelAzureFunctionsFunctionApp header, must be an app name: letters, digits and hyphens, not starting or ending with a hyphen, at most 60 characters. The function name, from the endpoint or the CamelAzureFunctionsFunctionName header, is the path after /api/: one or more segments separated by /, so a function with a custom route, such as products/42, can be called. Characters that are not allowed in a URL path, such as a space, are encoded. A function name with ?, #, \, %, an empty segment, or a . or .. segment is rejected, and so is a function app that is not an app name: the exchange fails with an IllegalArgumentException.
Usage
Authentication Information
There are three different Credential Types: AZURE_IDENTITY, FUNCTION_KEY, and TOKEN_CREDENTIAL.
AZURE_IDENTITY (default):
This will use com.azure.identity.DefaultAzureCredentialBuilder().build() instance. This follows the Default Azure Credential Chain which tries multiple authentication methods:
-
Environment variables
-
Managed Identity
-
Azure CLI
-
Visual Studio Code
-
IntelliJ IDEA
See the documentation here about Azure AD authentication.
FUNCTION_KEY:
For invoking HTTP-triggered functions, you can use function keys or host keys:
-
Java
-
XML
-
YAML
from("direct:invoke")
.to("azure-functions:myFunctionApp/myFunction?credentialType=FUNCTION_KEY&functionKey=your-function-key"); <route>
<from uri="direct:invoke"/>
<to uri="azure-functions:myFunctionApp/myFunction?credentialType=FUNCTION_KEY&functionKey=your-function-key"/>
</route> - route:
from:
uri: direct:invoke
steps:
- to:
uri: azure-functions:myFunctionApp/myFunction
parameters:
credentialType: FUNCTION_KEY
functionKey: your-function-key TOKEN_CREDENTIAL:
Provide an implementation of com.azure.core.credential.TokenCredential into the Camel Registry:
-
Java
-
XML
-
YAML
from("direct:invoke")
.to("azure-functions:myFunctionApp/myFunction?credentialType=TOKEN_CREDENTIAL&tokenCredential=#myCredential"); <route>
<from uri="direct:invoke"/>
<to uri="azure-functions:myFunctionApp/myFunction?credentialType=TOKEN_CREDENTIAL&tokenCredential=#myCredential"/>
</route> - route:
from:
uri: direct:invoke
steps:
- to:
uri: azure-functions:myFunctionApp/myFunction
parameters:
credentialType: TOKEN_CREDENTIAL
tokenCredential: "#myCredential" Service Principal Authentication
For service principal authentication, provide the client credentials:
-
Java
-
XML
-
YAML
from("direct:manage")
.to("azure-functions:myFunctionApp?operation=listFunctionApps&subscriptionId=your-subscription-id&resourceGroup=your-resource-group&clientId=your-client-id&clientSecret=your-client-secret&tenantId=your-tenant-id"); <route>
<from uri="direct:manage"/>
<to uri="azure-functions:myFunctionApp?operation=listFunctionApps&subscriptionId=your-subscription-id&resourceGroup=your-resource-group&clientId=your-client-id&clientSecret=your-client-secret&tenantId=your-tenant-id"/>
</route> - route:
from:
uri: direct:manage
steps:
- to:
uri: azure-functions:myFunctionApp
parameters:
operation: listFunctionApps
subscriptionId: your-subscription-id
resourceGroup: your-resource-group
clientId: your-client-id
clientSecret: your-client-secret
tenantId: your-tenant-id Invoking a function
The invokeFunction operation sends the message body to the function: text, a number or a boolean is sent as text (UTF-8), and bytes, a stream or a file (also the file of a file consumer) as their content. A Map or a POJO is not marshalled: marshal it to JSON first, for example with marshal().json(), as a body that does not convert to bytes fails the exchange. GET and DELETE send no body.
A request with a body has the Content-Type that the CamelAzureFunctionsHttpHeaders header, a Map<String, String> of HTTP headers to send, sets. When that map sets none, the Content-Type header of the message is used, and when the message has none either, Content-Type: application/json. Text is always sent as UTF-8: the charset parameter of a Content-Type from the message, for example text/plain; charset=ISO-8859-1, is not used for the encoding.
The response body of the function becomes the message body, as text, with the HTTP status in the CamelAzureFunctionsStatusCode header and the HTTP headers of the response in the CamelAzureFunctionsResponseHeaders header. A status of 400 or above fails the exchange with an org.apache.camel.http.base.HttpOperationFailedException, as the HTTP components do: it holds the status, the response body and the response headers. The message body is then still the request body, as in camel-http, with the status in the CamelAzureFunctionsStatusCode header. Set throwExceptionOnFailure=false to get the response of every status as the message body instead.
Operations
The component supports the following operations:
Function Invocation
| Operation | Description |
|---|---|
| Invokes an HTTP-triggered Azure Function (default operation) |
Function App Management
| Operation | Description |
|---|---|
| List all function apps in the subscription or resource group |
| Get details of a specific function app |
| Create a new function app |
| Delete a function app |
| Start a stopped function app |
| Stop a running function app |
| Restart a function app |
Function Operations
| Operation | Description |
|---|---|
| List all functions within a function app |
| Get details of a specific function |
| Get the master key for the function app |
Examples
Invoke a Function
-
Java
-
XML
-
YAML
from("direct:invoke")
.setBody(constant("{\"name\": \"World\"}"))
.to("azure-functions:myFunctionApp/HelloFunction?functionKey=your-key")
.log("Response: ${body}"); <route>
<from uri="direct:invoke"/>
<setBody>
<constant>{"name": "World"}</constant>
</setBody>
<to uri="azure-functions:myFunctionApp/HelloFunction?functionKey=your-key"/>
<log message="Response: ${body}"/>
</route> - route:
from:
uri: direct:invoke
steps:
- setBody:
constant: '{"name": "World"}'
- to:
uri: azure-functions:myFunctionApp/HelloFunction
parameters:
functionKey: your-key
- log:
message: "Response: ${body}" List Function Apps
-
Java
-
XML
-
YAML
from("direct:list")
.to("azure-functions:myFunctionApp?operation=listFunctionApps&subscriptionId=xxx&resourceGroup=myResourceGroup")
.log("Function Apps: ${body}"); <route>
<from uri="direct:list"/>
<to uri="azure-functions:myFunctionApp?operation=listFunctionApps&subscriptionId=xxx&resourceGroup=myResourceGroup"/>
<log message="Function Apps: ${body}"/>
</route> - route:
from:
uri: direct:list
steps:
- to:
uri: azure-functions:myFunctionApp
parameters:
operation: listFunctionApps
subscriptionId: xxx
resourceGroup: myResourceGroup
- log:
message: "Function Apps: ${body}" Stop a Function App
-
Java
-
XML
-
YAML
from("direct:stop")
.to("azure-functions:myFunctionApp?operation=stopFunctionApp&subscriptionId=xxx&resourceGroup=myResourceGroup")
.log("Function App stopped"); <route>
<from uri="direct:stop"/>
<to uri="azure-functions:myFunctionApp?operation=stopFunctionApp&subscriptionId=xxx&resourceGroup=myResourceGroup"/>
<log message="Function App stopped"/>
</route> - route:
from:
uri: direct:stop
steps:
- to:
uri: azure-functions:myFunctionApp
parameters:
operation: stopFunctionApp
subscriptionId: xxx
resourceGroup: myResourceGroup
- log:
message: "Function App stopped" Update Configuration
from("direct:updateConfig")
.setHeader("CamelAzureFunctionsAppSettings", constant(Map.of("MY_SETTING", "value")))
.to("azure-functions:myFunctionApp?operation=updateFunctionAppConfiguration&subscriptionId=xxx&resourceGroup=myResourceGroup")
.log("Configuration updated: ${body}"); Create a Function App
-
Java
-
XML
-
YAML
from("direct:create")
.to("azure-functions:newFunctionApp?operation=createFunctionApp&subscriptionId=xxx&resourceGroup=myResourceGroup&location=eastus&runtime=java&storageAccountConnectionString=RAW(DefaultEndpointsProtocol=https;...)")
.log("Created function app: ${header.CamelAzureFunctionsResourceId}"); <route>
<from uri="direct:create"/>
<to uri="azure-functions:newFunctionApp?operation=createFunctionApp&subscriptionId=xxx&resourceGroup=myResourceGroup&location=eastus&runtime=java&storageAccountConnectionString=RAW(DefaultEndpointsProtocol=https;...)"/>
<log message="Created function app: ${header.CamelAzureFunctionsResourceId}"/>
</route> - route:
from:
uri: direct:create
steps:
- to:
uri: azure-functions:newFunctionApp
parameters:
operation: createFunctionApp
subscriptionId: xxx
resourceGroup: myResourceGroup
location: eastus
runtime: java
storageAccountConnectionString: "RAW(DefaultEndpointsProtocol=https;...)"
- log:
message: "Created function app: ${header.CamelAzureFunctionsResourceId}"