Get Started with Azure Functions
Azure Functions is a serverless compute service that lets you run event-triggered code without explicitly provisioning or managing infrastructure. This guide will walk you through the essential steps to create and deploy your first Azure Function.
Prerequisites
Before you begin, ensure you have the following:
- An Azure Account: If you don't have one, you can sign up for a free account.
- Azure Functions Core Tools: Install this locally to develop and test your functions. Installation Guide.
- A Code Editor: Visual Studio Code with the Azure Functions extension is highly recommended.
- Node.js: Required for some templates. Download Node.js.
Step 1: Create a New Function Project
Open your terminal or command prompt and navigate to the directory where you want to create your project. Then, run the following command:
func init MyFunctionProject --worker-runtime node --docker
This command initializes a new Functions project named MyFunctionProject using Node.js as the runtime and includes Dockerfile generation.
Step 2: Create Your First Function
Navigate into your new project directory:
cd MyFunctionProject
Now, create a new function. For this example, we'll create an HTTP-triggered function:
func new --name HttpTriggerExample --template "HTTP trigger"
This will create a new folder named HttpTriggerExample containing your function code and configuration files.
Step 3: Explore the Function Code
Open the HttpTriggerExample folder in your code editor. You'll typically find files like:
index.js: The main JavaScript file containing your function logic.function.json: Defines the function's bindings (triggers and inputs/outputs).
Here's a typical example of index.js for an HTTP trigger:
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
};
};
Step 4: Run Your Function Locally
From your project's root directory (MyFunctionProject), start the Functions host:
func start
The output will show the local endpoint for your HTTP-triggered function, usually something like http://localhost:7071/api/HttpTriggerExample.
?name=World to the URL.
Step 5: Deploy to Azure
To deploy your function to Azure, you'll need an Azure Functions App. You can create one via the Azure portal or using the Azure CLI:
az functionapp create --resource-group MyResourceGroup --consumption-plan-location westus --name MyUniqueFunctionAppName --storage-account MyUniqueStorageAccount --runtime node --runtime-version 16 --functions-version 4
Once your Function App is created, you can deploy your project using the Azure Functions Core Tools:
func azure functionapp publish MyUniqueFunctionAppName
Next Steps
Congratulations! You've created and deployed your first Azure Function. Here are some resources to continue your learning: