Azure Functions Documentation

Create Your First Azure Function

A step-by-step guide to building and deploying your initial serverless function.

This guide will walk you through the process of creating a simple Azure Function using the Azure portal. We'll create an HTTP-triggered function that returns a greeting message.

Prerequisites

Steps

1

Sign in to the Azure Portal

Open your web browser and navigate to the Azure portal. Sign in with your Azure account credentials.

2

Create a Function App

In the Azure portal, search for "Function App" and select it from the results. Click the "Create" button.

On the "Basics" tab, configure the following settings:

  • Subscription: Select your Azure subscription.
  • Resource Group: Choose an existing resource group or create a new one.
  • Function App name: Enter a globally unique name for your function app.
  • Publish: Select "Code".
  • Runtime stack: Choose your preferred language (e.g., .NET, Node.js, Python). For this guide, let's assume Node.js.
  • Version: Select the appropriate version for your runtime stack.
  • Region: Choose a region close to your users.

Click "Review + create" and then "Create" to deploy your function app.

3

Create a New Function

Once your function app is deployed, navigate to it. In the Function App menu, select "Functions" and then click "+ Create".

In the "Create Function" blade:

  • Development environment: Select "Develop in portal".
  • Template: Choose "HTTP trigger".
  • New Function Name: Enter a name for your function, for example, HttpGreeting.
  • Authorization level: Select "Anonymous" for simplicity during this tutorial.

Click "Create".

4

Write Your Function Code

Your new function will be created with some default code. You should see a code editor. For a Node.js HTTP trigger, the default might look something 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,
        body: responseMessage
    };
};

This code checks for a name parameter in the query string or request body and returns a personalized greeting.

5

Test Your Function

In the function editor, click the "Get Function URL" button at the top. This will provide you with the endpoint URL for your function.

You can test your function by:

  • Pasting the URL into your browser.
  • Appending ?name=YourName to the URL in your browser's address bar and pressing Enter.
  • Using a tool like curl or Postman to send a POST request with a JSON body containing a name property.

You should see the greeting message returned by your function.

Congratulations!

You have successfully created and tested your first Azure Function. This is the fundamental process for building serverless applications on Azure. You can now explore more advanced triggers, bindings, and development patterns.

Next Steps