Azure Functions Tutorials

Create a Serverless Function with Triggers

This tutorial guides you through creating your first Azure Function, demonstrating how to use different trigger types to invoke your function.

Introduction

Azure Functions provide a serverless compute experience that enables you to build and deploy event-driven applications on Azure without managing infrastructure. This tutorial will walk you through the steps to create a simple function that can be triggered by various events, such as HTTP requests or messages in a queue.

Prerequisites

Step 1: Create a New Azure Functions Project

Using Azure Functions Core Tools
  1. Open your terminal or command prompt.
  2. Navigate to the directory where you want to create your project.
  3. Run the following command to create a new project:
    func init MyFunctionProject --worker-runtime node --language javascript

    Replace node and javascript with your preferred runtime and language (e.g., dotnet, python, csharp).

  4. Navigate into your new project directory:
    cd MyFunctionProject

Step 2: Create a New Function with an HTTP Trigger

Using Azure Functions Core Tools
  1. In your project directory, run the following command to create a new function:
    func new --name HttpTriggerFunction --template "HTTP trigger"

    This command creates a new function named HttpTriggerFunction with an HTTP trigger template.

  2. Open the project in your code editor. You will find a new folder named HttpTriggerFunction containing index.js (or your language's equivalent) and function.json.

Step 3: Understand the HTTP Trigger Code

Open the index.js file (or equivalent) in your HttpTriggerFunction folder. You'll see code similar to this:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || (req.body && req.body.name));
    const responseMessage = name
        ? 'Hello, ' + name + '. This HTTP triggered function executed successfully.'
        : "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.";

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
};

The function.json file defines the function's bindings, including the HTTP trigger:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}

Step 4: Run Your Function Locally

Using Azure Functions Core Tools
  1. In your terminal, at the root of your project directory, run:
    func start
  2. The output will show the URLs where your HTTP trigger function is running. Look for a URL like http://localhost:7071/api/HttpTriggerFunction.
  3. Open this URL in your web browser. You should see the personalized message or the default message.
  4. Try adding a query parameter like ?name=World to the URL (e.g., http://localhost:7071/api/HttpTriggerFunction?name=World) to see the personalized response.

Step 5: Add a Timer Trigger

Let's add a function that runs on a schedule using a timer trigger.

Using Azure Functions Core Tools
  1. In your terminal, at the root of your project directory, run:
    func new --name TimerTriggerFunction --template "Timer trigger"
  2. Open the new TimerTriggerFunction folder. You'll find index.js and function.json.
  3. Open function.json. The schedule property defines when the timer will fire. By default, it's set to run every 5 minutes:
    "schedule": "0 */5 * * * *"

    This is a CRON expression. You can modify it to change the trigger frequency. For example, to run every minute, you would use "schedule": "* * * * * *".

  4. Open index.js. The function code is straightforward:
    module.exports = async function (context, myTimer) {
        var timeStamp = new Date().toISOString();
        if (myTimer.isPastDue) {
            context.log('JavaScript is running late!');
        }
        context.log('JavaScript timer trigger function executed at', timeStamp);
    };
  5. Stop your local function host (Ctrl+C) and restart it with func start.
  6. You will see messages in the console every time the timer trigger fires.

Next Steps

Note: This tutorial covers the basics. Azure Functions offer a rich set of features for building sophisticated serverless applications.
Explore More Tutorials