Create Your First Azure Function: A "Hello World" Example

This tutorial will guide you through creating a simple HTTP-triggered Azure Function that responds with a "Hello, World!" message. We'll use Visual Studio Code for this example.

Prerequisites

1

Create a New Project

Open Visual Studio Code and press F1 to open the command palette. Type "Azure Functions: Create new project..." and select it.

Choose a folder for your project and select "Create new project".

Select your preferred language. For this tutorial, let's choose Node.js.

Select a template for your first function. Choose HTTP trigger.

Provide a name for your function, for example, HelloWorldHttp.

Set the authorization level to Anonymous for simplicity in this tutorial.

2

Explore the Project Files

Your new project will contain a few files. The most important ones for this tutorial are:

  • HelloWorldHttp/index.js: This is the main code file for your HTTP trigger function.
  • HelloWorldHttp/function.json: This file defines the function's configuration, including its triggers and bindings.
  • package.json: Manages your Node.js project dependencies.
  • local.settings.json: Used for local development to store app settings and connection strings.

Open HelloWorldHttp/index.js. You should 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
    };
};
3

Run the Function Locally

To run your function locally, make sure the Azure Functions extension is active. Click the "Run" icon in the Azure Functions extension sidebar, then click "Start debugging".

Once the function host starts, you'll see output in the terminal, including the URL for your HTTP trigger. It will typically look like:

Http Functions:
    HelloWorldHttp: [GET,POST] http://localhost:7071/api/HelloWorldHttp

Open your web browser and navigate to the URL provided. You can also append ?name=AzureUser to the URL to see a personalized response:

Example URL: http://localhost:7071/api/HelloWorldHttp?name=AzureUser

4

Deploy to Azure

To deploy your function to Azure:

  1. In VS Code, click the "Deploy" icon in the Azure Functions extension sidebar.
  2. Sign in to your Azure account if prompted.
  3. Select your subscription.
  4. Choose "Create new Function App in Azure..."
  5. Enter a globally unique name for your Function App (e.g., my-hello-world-app-xyz).
  6. Select a runtime stack (e.g., Node.js 18 LTS).
  7. Select a region closest to you.
  8. Select "Create new resource group" or choose an existing one.
  9. Select "Create new Storage account" or choose an existing one.

The extension will now build and deploy your function. Once complete, you'll be prompted to view the deployment output. You can then find your deployed Function App in the Azure portal.

To get the URL of your deployed function, navigate to your Function App in the Azure portal, go to "Functions", click on "HelloWorldHttp", and then click "Get Function URL".

Congratulations! You've successfully created, run locally, and deployed your first Azure Function.

Next Steps