This tutorial guides you through creating your first Azure Function, demonstrating how to use different trigger types to invoke your function.
Azure Functions provide a serverless compute experience that enables you to build and deploy event-driven applications on Azure without managing infrastructure. This tutorial will walk you through the steps to create a simple function that can be triggered by various events, such as HTTP requests or messages in a queue.
func init MyFunctionProject --worker-runtime node --language javascript
Replace node and javascript with your preferred runtime and language (e.g., dotnet, python, csharp).
cd MyFunctionProject
func new --name HttpTriggerFunction --template "HTTP trigger"
This command creates a new function named HttpTriggerFunction with an HTTP trigger template.
HttpTriggerFunction containing index.js (or your language's equivalent) and function.json.Open the index.js file (or equivalent) in your HttpTriggerFunction folder. You'll 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
};
};
The function.json file defines the function's bindings, including the HTTP trigger:
{
"bindings": [
{
"authLevel": "function",
"type": "httpTrigger",
"direction": "in",
"name": "req",
"methods": [
"get",
"post"
]
},
{
"type": "http",
"direction": "out",
"name": "res"
}
]
}
func start
http://localhost:7071/api/HttpTriggerFunction.?name=World to the URL (e.g., http://localhost:7071/api/HttpTriggerFunction?name=World) to see the personalized response.Let's add a function that runs on a schedule using a timer trigger.
func new --name TimerTriggerFunction --template "Timer trigger"
TimerTriggerFunction folder. You'll find index.js and function.json.function.json. The schedule property defines when the timer will fire. By default, it's set to run every 5 minutes:
"schedule": "0 */5 * * * *"
This is a CRON expression. You can modify it to change the trigger frequency. For example, to run every minute, you would use "schedule": "* * * * * *".
index.js. The function code is straightforward:
module.exports = async function (context, myTimer) {
var timeStamp = new Date().toISOString();
if (myTimer.isPastDue) {
context.log('JavaScript is running late!');
}
context.log('JavaScript timer trigger function executed at', timeStamp);
};
func start.