Create your first Azure Function using Visual Studio Code
This tutorial guides you through creating a simple "Hello World" HTTP-triggered Azure Function. You'll learn how to create a project, add a function, and run it locally.
Introduction
Azure Functions is a serverless compute service that allows you to run small pieces of code, or "functions," without managing infrastructure. This tutorial is designed for beginners and will walk you through the essential steps to get started.
Prerequisites
Before you begin, ensure you have the following installed:
Create an Azure Functions project
Let's start by creating a new Azure Functions project in Visual Studio Code.
-
Open the Command Palette
In Visual Studio Code, press Ctrl+Shift+P (or Cmd+Shift+P on macOS) to open the Command Palette.
-
Create New Project
Type "Azure Functions: Create New Project" and select it from the list.
-
Select Folder
Choose a folder on your local machine to host your project. If the folder doesn't exist, you'll be prompted to create it.
-
Select Language
Choose your preferred programming language. For this tutorial, we'll select JavaScript.
-
Select Template
Select the HTTP trigger template. This template creates a function that is triggered by an HTTP request.
-
Provide Function Name
Enter a name for your function. Let's call it
HelloWorld. -
Set Authorization Level
Choose the authorization level. For local development, select Anonymous.
Create a function
Once the project is created, Visual Studio Code will open the project folder. You'll see a file named index.js (or similar, depending on your language choice) within a folder named after your function (e.g., HelloWorld).
Open the index.js file. It will contain the basic code for your HTTP-triggered function:
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. 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
};
};
This code defines an asynchronous function that takes `context` and `req` (request) as arguments. It checks for a `name` parameter in the query string or request body and returns a personalized greeting.
Run the function locally
To test your function without deploying it, you can run it locally using the Azure Functions Core Tools.
-
Open Integrated Terminal
In Visual Studio Code, open the integrated terminal by going to View > Terminal.
-
Start the Functions Host
Navigate to your project's root directory in the terminal and run the following command:
func start
The Functions host will start, and you'll see output indicating that your HelloWorld function is running. It will provide a local URL for your function, typically:
http://localhost:7071/api/HelloWorld
Test the function:
- Open a web browser and navigate to
http://localhost:7071/api/HelloWorld?name=Azure. You should see "Hello, Azure!". - You can also try sending a POST request with a JSON body containing a
nameproperty using a tool like Postman or `curl`.
Note: If you encounter issues starting the Functions host, ensure Azure Functions Core Tools are installed and accessible in your PATH.
Next Steps
Congratulations! You've created and run your first Azure Function locally.
From here, you can explore:
- Deploying your function to Azure.
- Exploring other trigger types (e.g., Timer, Blob Storage, Queue).
- Integrating with other Azure services.
You can find more advanced tutorials and documentation on the official Azure Functions documentation.