0
0
Azurecloud~5 mins

Function execution model in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Azure Functions lets you run small pieces of code without managing servers. It automatically runs your code when triggered by events, so you only pay for the time your code runs.
When you want to run code in response to a file upload without setting up a server.
When you need to process messages from a queue automatically and quickly.
When you want to run scheduled tasks like cleaning up data every night.
When you want to build a simple API without managing web servers.
When you want to scale your code automatically based on demand.
Config File - function.json
function.json
{
  "bindings": [
    {
      "name": "req",
      "type": "httpTrigger",
      "direction": "in",
      "authLevel": "function",
      "methods": ["get", "post"]
    },
    {
      "name": "$return",
      "type": "http",
      "direction": "out"
    }
  ]
}

This file defines how the Azure Function is triggered and what it returns.

  • bindings: Lists triggers and outputs.
  • httpTrigger: Runs the function when an HTTP GET or POST request arrives.
  • authLevel: Controls access; 'function' means a key is needed.
  • http: Defines the HTTP response output.
Commands
Create a new Azure Functions app project using the .NET runtime.
Terminal
func init MyFunctionApp --worker-runtime dotnet
Expected OutputExpected
Creating new Azure Functions project in C:\Users\user\MyFunctionApp Project created successfully.
--worker-runtime - Specifies the language runtime for the function app.
Add a new function named HttpExample triggered by HTTP requests with function-level authorization.
Terminal
func new --name HttpExample --template "HTTP trigger" --authlevel function
Expected OutputExpected
Created new function 'HttpExample' with HTTP trigger.
--template - Specifies the function trigger template.
--authlevel - Sets the authorization level for the function.
Run the function app locally to test the function execution model.
Terminal
func start
Expected OutputExpected
Functions: HttpExample: [GET,POST] http://localhost:7071/api/HttpExample Host started (http://localhost:7071, Ctrl+C to shut down)
Send an HTTP GET request to trigger the function and see the response.
Terminal
curl http://localhost:7071/api/HttpExample?name=Azure
Expected OutputExpected
"Hello, Azure"
Key Concept

Azure Functions run your code only when triggered by events, automatically managing resources and scaling for you.

Common Mistakes
Not setting the correct authLevel in function.json
The function may reject requests or be open to unauthorized access.
Set authLevel to 'function' or 'anonymous' based on your security needs.
Running func start without creating a function first
The app will start but no functions will be available to trigger.
Create at least one function using 'func new' before starting.
Using the wrong HTTP method when triggering the function
The function will not respond if the method is not allowed.
Use only the HTTP methods defined in the function.json bindings.
Summary
Initialize an Azure Functions app with 'func init' specifying the runtime.
Create a function with an HTTP trigger using 'func new' and set authorization.
Run the function app locally with 'func start' to test the execution.
Trigger the function by sending an HTTP request and receive the response.