Create an Azure Function

This guide walks you through the process of creating your first Azure Function using various methods. Azure Functions is a serverless compute service that lets you run code on-demand without explicitly provisioning or managing infrastructure.

Method 1: Using the Azure Portal

The Azure Portal provides an intuitive web-based interface to create and manage your functions.

  1. Sign in to the Azure Portal.
  2. Navigate to your Function App. If you don't have one, you'll need to create one first.
  3. In your Function App, select Functions from the left-hand menu.
  4. Click Create.
  5. Choose a template for your function. Common templates include:
    • HTTP trigger: For functions invoked via HTTP requests.
    • Timer trigger: For functions that run on a schedule.
    • Blob trigger: For functions that run when a blob is created or updated.
  6. Provide a New Function Name.
  7. Configure any necessary authorization level and trigger settings.
  8. Click Create.

Method 2: Using Visual Studio Code

Visual Studio Code with the Azure Functions extension offers a powerful local development experience.

  1. Install Visual Studio Code and the Azure Functions extension.
  2. Open the Command Palette (Ctrl+Shift+P or Cmd+Shift+P).
  3. Type Azure Functions: Create New Project... and select it.
  4. Choose a folder for your project.
  5. Select your preferred programming language.
  6. Choose a template for your function (e.g., HTTP Trigger).
  7. Provide a function name.
  8. Select an authorization level.
  9. The extension will create the necessary project files, including a starter code file.

Method 3: Using the Azure CLI

The Azure Command-Line Interface (CLI) is ideal for scripting and automation.

  1. Install the Azure CLI.
  2. Log in to your Azure account: az login
  3. Create a new function app resource group if needed: az group create --name MyResourceGroup --location westus
  4. Create a new function app: az functionapp create --resource-group MyResourceGroup --consumption-plan-location westus --name MyUniqueFunctionAppName --runtime node --runtime-version 18 --functions-version 4 (Adjust runtime and version as needed).
  5. Navigate to your local function app project directory.
  6. Use the Azure Functions Core Tools to create a new function: func new --name MyHttpTriggerFunction --template "HTTP trigger" --authlevel "anonymous" (Adjust template and authlevel).

Understanding Function Code

Let's look at a simple HTTP trigger function written in JavaScript:


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. Pass a name in the query string or in the request body for a personalized response.';

    context.res = {
        status: 200,
        body: responseMessage
    };
};
            

Tip: The context object provides information about the trigger, input bindings, and output bindings. context.log() is used for logging.

Next Steps

Once you've created your function, you can:

Explore the Triggers and Bindings documentation to learn how to connect your functions to the wider Azure ecosystem.


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. Pass a name in the query string or in the request body for a personalized response.';

    context.res = {
        status: 200,
        body: responseMessage
    };
};