How to setup Azure function with Example

  1. Log in to the Azure portal and click on the “Create a resource” button.
  2. Search for “Function App” in the search bar and click on the “Function App” option.
  3. Click on the “Create” button to create a new Function App.
  4. Choose the subscription, resource group, and region for your Function App.
  5. In the “Function App” section, specify the name for your Function App.
  6. Choose the “OS” option (Windows or Linux).
  7. Choose the “Hosting plan” (Consumption or App Service plan). The Consumption plan is a serverless plan where you only pay for the time your functions run, while the App Service plan is a more traditional hosting plan where you pay for the resources you allocate.
  8. Click on the “Create” button to create your Function App.

Here’s an example code for a simple Azure Function in JavaScript:

  1. In the Function App page, click on the “Functions” option in the left sidebar and then click on the “Add” button to create a new function.
  2. Choose the “In-portal” option and select the “Webhook + API” template.
  3. Give your function a name and choose the language (JavaScript).
  4. In the “Code + Test” section, you can write your function code directly in the portal.
  5. Replace the default function code with the following code:
module.exports = function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const name = (req.query.name || req.body.name || 'World');
    const responseMessage = `Hello ${name}!`;

    context.res = {
        // status: 200, /* Defaults to 200 */
        body: responseMessage
    };
    context.done();
};

This function simply returns a “Hello, World!” message as an HTTP response.

  1. Click on the “Save” button to save your function.

Once you’ve created and tested your function, you can trigger it using various events, such as HTTP requests, timers, or Azure Blob Storage events.

Leave a Reply

Your email address will not be published. Required fields are marked *