Run your code on-demand with Azure Functions


Now that we've created a function app, let's look at how to build, configure, and execute a function. We'll execute our function in the next exercise, so you don't need to complete any tasks in this unit.


Functions are event driven, which means they run in response to an event. The type of event that starts a function is called a trigger. Each function must be configured with exactly one trigger.

Azure supports triggers for the following services.

Service Trigger description
Blob Storage Starts a function when a new or updated blob is detected.
Azure Cosmos DB Start a function when inserts and updates are detected.
Event Grid Starts a function when an event is received from Event Grid.
HTTP Starts a function with an HTTP request.
Microsoft Graph Events Starts a function in response to an incoming webhook from the Microsoft Graph. Each instance of this trigger can react to one Microsoft Graph resource type.
Queue Storage Starts a function when a new item is received on a queue. The queue message is provided as input to the function.
Service Bus Starts a function in response to messages from a Service Bus queue.
Timer Starts a function on a schedule.


A binding is a declarative way to connect data and services to your function. Bindings interact with various data sources, which means you don't have to write the code in your function to connect to data sources and manage connections. The platform takes care of that complexity for you as part of the binding code. Each binding has a direction--your code reads data from input bindings, and writes data to output bindings. Each function can have zero or more bindings to manage the input and output data processed by the function.

A trigger is a type of input binding that has the ability to initiate execution of some code.

Azure provides a large number of bindings to connect to different storage and messaging services.

Define a sample binding

Let's look at an example of configuring a function with an input binding (trigger) and an output binding. Let's say we want to write a new row to Azure Table storage whenever a new message appears in Azure Queue Storage. This scenario can be implemented using an Azure Queue Storage trigger and an Azure Table storage output binding.

The following snippet is the function.json file for this scenario.

  "bindings": [
      "name": "order",
      "type": "queueTrigger",
      "direction": "in",
      "queueName": "myqueue-items",
      "connection": "MY_STORAGE_ACCT_APP_SETTING"
      "name": "$return",
      "type": "table",
      "direction": "out",
      "tableName": "outTable",

Our JSON configuration specifies that our function will be triggered when a message is added to a queue named myqueue-items. The return value of our function is then written to outTable in Azure Table storage. For PowerShell functions, output bindings are explicitly written to with the Push-OutputBinding cmdlet.

This example is a simple illustration of how we configure bindings for a function. We could change the output to be an email using a SendGrid binding, or put an event onto a Service Bus to notify some other component in our architecture, or even have multiple output bindings to push data to various services.


To view and edit the contents of function.json in the Azure portal, from the Home page, select your function app, and in the right pane, select JSON View. The Resource JSON view displays the Resource ID and the editable JSON code. To close the JSON view, select the X in the top right corner of the pane.

Create a function in the Azure portal

Azure provides several predefined function templates for common scenarios:

  • Quickstart
  • Custom functions

Function templates

When you create your first function in the Azure Create function pane, you can select a predefined trigger for your function. Based on your selections, Azure generates default code and configuration information, such as creating an event log entry when input data is received.

Selecting a template from the Add function pane provides easy access to the most common development environments, triggers, dependencies. When you create a function in the Azure portal, you can choose from more than 20 templates. Once created you can further customize the code.

When you create a function from a template, several files are created, including a configuration file, function.json, and a source code file, index.js.

You can create or edit functions for your function app by selecting Functions under the Functions category from the Function App menu.

When you select a function that you created in your function app, the Function pane opens. By selecting Code + Test from the Function menu, you have access to actions in the command bar to test and run the code, to save or discard changes you make, or to obtain the published URL. By selecting Test/Run from the command bar, you can run use cases for requests that include query strings and values. The function's path above the code box displays the name of the file that is open. You can select a specific file from the dropdown to test or edit, for example, function.json.

Screenshot of the function code and test editor showing the expanded Test/Run view, with menu options highlighted.

In the image above, the pane on the right has Input and Output tabs. Selecting the Input tab enables you to build and test the function by adding query parameters and supplying values for your query string. The Output tab displays the results of the request.

Test your Azure function

After you've created a function, you'll want to test it. There are two approaches:

  • Running it manually
  • Testing it from within the Azure portal itself

Run function manually

You can start a function by manually triggering the configured trigger. For instance, if you're using an HTTP trigger, you can use a tool, such as Postman or cURL, to initiate an HTTP request to your function endpoint URL, which is available from the function definition (Get function URL).

Test in the Azure portal

The portal also provides a convenient way to test your functions. As previously described, in the screenshot above. When you select Run in this pane, the results automatically appear in the Output tab, and the Logs pane opens to display the status.

Monitoring and Application Insights dashboard

The ability to monitor your functions is critical during development and in production. The Azure portal provides a monitoring dashboard, which you turn on by enabling Application Insights integration. In the Function App menu, under Settings, select Application Insights, select Turn on Application Insights, and then select Apply. In the dialog box, select Yes. The Application Insights dashboard provides a quick way to view the history of function operations by displaying the timestamp, result code, duration, and operation ID populated by Application Insights.

Screenshot showing the HTTP function Application Insights dashboard with function results, corresponding HTTP status codes, and the Monitoring menu items highlighted.

Streaming logs pane

After you've enabled Application Insights in the Azure portal, you can add logging statements to your function for debugging. The called methods for each language are passed a "logging" object, which can be used to add log information to the Logs pane in the Code + Test pane when running a test.

The following code snippets show how to create a log message:

  • In JavaScript, the context object is passed to the handler.

    context.log('Enter your logging statement here');
  • In C#, log.LogInformation method, the log object is passed to the C# method processing the function.

    log.LogInformation("Enter your logging statement here");
  • In PowerShell, use Write-Host cmdlet to write to the log:

    Write-Host "Enter your logging statement here"

Errors, failures, warnings, and anomalies

You can use Metrics or options from the Investigate category in the Function menu to monitor performance, diagnose failures, or configure dozens of predefined workbooks to manage your function app, everything from compilation errors and warnings in the code, to usage statistics by role.