Create an Azure Function

This guide walks you through the process of creating your first Azure Function using the Azure portal. Azure Functions provides a serverless compute experience that enables you to build and run application code without provisioning or managing infrastructure.

Prerequisites

Before you begin, ensure you have:

Steps to Create a Function

  1. Sign in to the Azure portal

    Navigate to the Azure portal and sign in with your Azure account credentials.

  2. Create a new Function App

    A Function App is the execution container for your individual functions. To create one:

    1. In the Azure portal search bar, type "Function App" and select it from the results.
    2. Click + Create.
    3. On the Basics tab, configure the following settings:
      • Subscription: Select your Azure subscription.
      • Resource Group: Select an existing resource group or create a new one.
      • Function App name: Enter a globally unique name for your function app. This name becomes the DNS domain name. For example, my-unique-function-app-name.
      • Publish: Choose Code.
      • Runtime stack: Select your preferred language (e.g., .NET, Node.js, Python, Java). For this example, let's choose Node.js.
      • Version: Select the appropriate version for your chosen runtime.
      • Region: Choose the Azure region closest to your users.
    4. Click Next: Hosting.
    5. Configure your hosting options (Storage account, Operating System, Plan type). For simplicity, we'll use the Consumption plan.
    6. Click Next: Monitoring, then Next: Tags, and finally Review + create.
    7. Review your configuration and click Create. The deployment may take a few minutes.

    Once deployed, navigate to your new Function App resource.

  3. Create your first function

    Inside your Function App, select Functions from the left-hand menu, then click + Create.

    You will be presented with several options for creating a function:

    • Develop in portal: Quick creation and editing directly in the browser.
    • Develop from package: Deploy from a ZIP package.
    • Other quick templates: Use pre-built templates for common scenarios.

    For this guide, we'll use the Develop in portal option.

    On the Create a function page:

    1. Choose a template. Select HTTP trigger. This template creates a function that is triggered by an HTTP request.
    2. Provide a New function name, for example, MyHttpTriggerFunction.
    3. Set the Authorization level. For simplicity, choose Anonymous. This means anyone can call the function without a key. For production, consider Function or Admin.
    4. Click Create.
  4. View and test your function

    Once your function is created, you will see its code editor. You can now test your function:

    1. Click Get Function URL from the top menu. Copy the URL provided.
    2. Open a new browser tab or use a tool like curl to make a GET request to the URL. You should see a response like:

      Hello, Your Function App name. This HTTP triggered function executed successfully.

      You can also test with a POST request and pass a name in the request body:

      curl -w "\n" -X POST "{your_function_url}" -d '{"name": "World"}' -H "Content-Type: application/json"

      The response should be:

      Hello, World. This HTTP triggered function executed successfully.

Next Steps

Congratulations on creating your first Azure Function! You can now explore other trigger types, integrate with various Azure services using bindings, and learn about deployment strategies.

Note: For production environments, always configure appropriate authentication and authorization for your HTTP-triggered functions.
Tip: Use the Azure CLI or Azure Functions Core Tools locally to develop and test your functions before deploying to Azure.