How to setup Azure function with Example
- Log in to the Azure portal and click on the “Create a resource” button.
- Search for “Function App” in the search bar and click on the “Function App” option.
- Click on the “Create” button to create a new Function App.
- Choose the subscription, resource group, and region for your Function App.
- In the “Function App” section, specify the name for your Function App.
- Choose the “OS” option (Windows or Linux).
- 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.
- Click on the “Create” button to create your Function App.
Here’s an example code for a simple Azure Function in JavaScript:
- 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.
- Choose the “In-portal” option and select the “Webhook + API” template.
- Give your function a name and choose the language (JavaScript).
- In the “Code + Test” section, you can write your function code directly in the portal.
- 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.
- 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.