Azure Functions Documentation

Create Your First Azure Function

Create Your First Azure Function

This tutorial guides you through creating your first Azure Function using the Azure portal. Azure Functions is a serverless compute service that enables you to run code on-demand without explicitly provisioning or managing infrastructure.

Prerequisites:
Azure Portal Dashboard Example

Step 1: Navigate to Azure Functions

Log in to the Azure portal. In the search bar at the top, type "Functions" and select Functions from the services list.

Step 2: Create a New Function App

A Function App is a logical container for your individual functions. To create one:

  1. On the Functions page, click + Create.
  2. Fill in the following details on the Basics tab:
    • Subscription: Select your Azure subscription.
    • Resource Group: Create a new one (e.g., MyFunctionResourceGroup) or select an existing one.
    • Function App name: Enter a globally unique name (e.g., myfirstfunctionapp12345).
    • Publish: Select Code.
    • Runtime stack: Choose your preferred language (e.g., .NET, Node.js, Python). For this tutorial, let's assume you choose Node.js.
    • Version: Select the appropriate version for your chosen runtime stack.
    • Region: Choose a region close to you or your users.
  3. Click Review + create and then Create.
Important: The Function App name must be globally unique across all of Azure.

Step 3: Create Your First HTTP Trigger Function

Once your Function App is deployed, navigate to it. Then, follow these steps to create a simple HTTP-triggered function:

  1. In your Function App, select Functions from the left-hand menu.
  2. Click + Create.
  3. Under Development environment, select Develop in portal.
  4. Under Select a template, choose the HTTP trigger template.
  5. Give your function a name, such as MyHttpTriggerFunction.
  6. Set the Authorization level to Anonymous for easy testing (you can change this later for more secure scenarios).
  7. Click Create.

Step 4: Test Your Function

Your new function is now deployed and ready to be tested.

  1. In your function's page, select Get Function URL from the top menu.
  2. Copy the generated URL.
  3. Paste the URL into your web browser's address bar and press Enter.

You should see a response similar to:

Hello, Azure Functions! This HTTP triggered function executed successfully.

You can also test by appending a name query parameter to the URL:

[Your_Function_URL]?name=World

The response should be:

Hello, World! This HTTP triggered function executed successfully.

Step 5: Explore the Code

Click on Code + Test in the left-hand menu for your function. You'll see the code for your HTTP trigger function. For a Node.js function, it might look like 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
    };
};

Next Steps

Congratulations! You've successfully created and tested your first Azure Function. Here are some ideas for what to do next:

  • Explore different trigger types (e.g., Timer, Queue).
  • Learn about input and output bindings to integrate with other Azure services.
  • Deploy your functions from a local development environment using Azure CLI or Visual Studio Code.
  • Implement more complex logic and handle errors gracefully.