This repository contains 20+ usage examples of triggers and bindings in Java serverless Azure Functions.
Triggers define how your function will be invoked: by an HTTP request, a new blob created on a Storage container, a new message in Event Grid, etc.
Bindings provide a declarative way to connect data to your function code. They can be used to declare input parameters to your function, for example, a set of CosmosDB documents, or a message from a Service Bus queue. Output bindings allow your function to supply the Functions runtime with parameters that the runtime will use to perform a data operation. Those can be: a JSON document to be added to a CosmosDB collection, an image file to be added to Blob storage, and so on.
Together, triggers and bindings allow you to greatly reduce data-management boilerplate code in your functions, freeing you to focus on the business problem that needs to be solved.
In order to use these examples, you will need:
- An active Azure Account (get a free trial account here).
- Java SDK 1.8+ installed.
- Maven.
- Some knowledge of Java and Git.
- Azure CLI installed.
- Visual Studio Code to modify the source code to suit your own needs. VS Code is optional; you can use any supported IDE. If using Visual Studio Code, please ensure to install the Azure Functions extension.
- An HTTP client, such as Postman for testing the GET and POST calls in HTTP-triggered functions.
The first step is to deploy the infrastructure components that will support the examples: a Function App, Cosmos DB account, Storage account, etc. There are many ways to do this:
-
Click the "Deploy to Azure" button below. You can then fill out the required parameters, and Azure Resource Manager will deploy all services that need to be in place for the examples to run.
-
You can also deploy the support infrastructure from the Azure CLI, by executing the following commands:
az group create --name ExampleResourceGroup --location "Central US"
az group deployment create \ --name ExampleDeployment \ --resource-group ExampleResourceGroup \ --template-uri "https://raw.githubusercontent.com/daniel-rocha/azure-functions-java-bindings/master/azuredeploy.json" \ --parameters "Function App Name"=<yourappname> "Database Account Name"=<yourdatabasename> "storageAccountName"=<yourstorageaccountname>
See below for the description of each mandatory parameter.
The deployment can take a few minutes to complete. Once it is successful, you can move to the next section.
These are all required parameters for a successful deployment:
Parameter | Description |
---|---|
Subscription | The name or ID of the Azure subscription to which the examples are being deployed. Can be empty if only subscription is active. |
Resource Group | Name of the resource group that will be created to host all the support services. |
Location | Azure region where the resource group (and associated services) will live. |
Function App Name | The name of your function app, for example, "myfunctionapp". This will be the hostname of your function app when testing HTTP calls. The function app will be created during deployment. |
Database Account Name | The name of your CosmosDB database account that will be created during deployment. |
storageAccountName | The name of the storage account that will be created during deployment. |
Our examples rely on some data to be placed in the right places, for the bindings to work. Here is how to deploy it:
-
Clone this repository to your local machine.
-
Run the following commands:
Windows:
C:> cd <project path>\env
C:> setupenvironment.ps1 -group <myresourcegroup> -s <subscription name>
Mac OSX and Linux
This script requires Python 3 to be installed.
$ cd <project path>/env
$ setupenvironment.sh
The first step is to deploy the infrastructure components that will support the examples: a Function App, Cosmos DB account, Storage account, etc. There are many ways to do this:
- Deploy to Azure using Azure Functions and Visual Studio Code
- Deploy an Azure Functions app using Maven
Once your functions project is deployed, you can test if everything is working by going to the following URL with your browser or Postman:
https://<yourfunctionappname>.azurewebsites.net/api/WriteOneDoc?desc=MyData+Here
This function takes a desc
query string parameter, creates a CosmosDB document with it, and a randomly generated ID, and returns the document to the caller as JSON:
{"id":"1860707505", "description": "MyData Here2"}
If you got here, congratulations! Now you're ready to explore all examples listed below. Instructions on how to test them are included in the comments section of each source file.
Here is a complete list of the examples currently in this repository: