Create Your First Azure Function: A "Hello World" Example
This tutorial will guide you through creating a simple HTTP-triggered Azure Function that responds with a "Hello, World!" message. We'll use Visual Studio Code for this example.
Prerequisites
- Visual Studio Code installed.
- The Azure Functions extension for VS Code installed.
- An Azure account (you can create a free one).
Create a New Project
Open Visual Studio Code and press F1
to open the command palette. Type "Azure Functions: Create new project..." and select it.
Choose a folder for your project and select "Create new project".
Select your preferred language. For this tutorial, let's choose Node.js.
Select a template for your first function. Choose HTTP trigger.
Provide a name for your function, for example, HelloWorldHttp
.
Set the authorization level to Anonymous for simplicity in this tutorial.
Explore the Project Files
Your new project will contain a few files. The most important ones for this tutorial are:
HelloWorldHttp/index.js
: This is the main code file for your HTTP trigger function.HelloWorldHttp/function.json
: This file defines the function's configuration, including its triggers and bindings.package.json
: Manages your Node.js project dependencies.local.settings.json
: Used for local development to store app settings and connection strings.
Open HelloWorldHttp/index.js
. You should 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
};
};
Run the Function Locally
To run your function locally, make sure the Azure Functions extension is active. Click the "Run" icon in the Azure Functions extension sidebar, then click "Start debugging".
Once the function host starts, you'll see output in the terminal, including the URL for your HTTP trigger. It will typically look like:
Http Functions:
HelloWorldHttp: [GET,POST] http://localhost:7071/api/HelloWorldHttp
Open your web browser and navigate to the URL provided. You can also append ?name=AzureUser
to the URL to see a personalized response:
Example URL: http://localhost:7071/api/HelloWorldHttp?name=AzureUser
Deploy to Azure
To deploy your function to Azure:
- In VS Code, click the "Deploy" icon in the Azure Functions extension sidebar.
- Sign in to your Azure account if prompted.
- Select your subscription.
- Choose "Create new Function App in Azure..."
- Enter a globally unique name for your Function App (e.g.,
my-hello-world-app-xyz
). - Select a runtime stack (e.g., Node.js 18 LTS).
- Select a region closest to you.
- Select "Create new resource group" or choose an existing one.
- Select "Create new Storage account" or choose an existing one.
The extension will now build and deploy your function. Once complete, you'll be prompted to view the deployment output. You can then find your deployed Function App in the Azure portal.
To get the URL of your deployed function, navigate to your Function App in the Azure portal, go to "Functions", click on "HelloWorldHttp", and then click "Get Function URL".
Next Steps
- Explore other trigger types like Timer triggers and Queue triggers.
- Learn about Azure Functions bindings for easier integration with other Azure services.
- Discover how to manage and monitor your functions in Azure.