Getting Started with Azure Functions

Welcome to the Azure Functions getting started guide! This tutorial will walk you through the fundamental steps of creating and deploying your first Azure Function.

What are Azure Functions?

Azure Functions is a serverless compute service that lets you run code without explicitly provisioning or managing infrastructure. You pay only for the time your code runs and can automatically scale based on demand.

Prerequisites

Step 1: Create a New Project

You can create an Azure Functions project locally using the Azure Functions Core Tools. Open your terminal or command prompt and run the following command:

func init MyFunctionApp --worker-runtime node

This command initializes a new Node.js Functions project in a directory named MyFunctionApp.

Step 2: Create a New Function

Navigate into your project directory and create a new HTTP-triggered function:

cd MyFunctionApp
func new --name MyHttpTrigger --template "HTTP trigger"

This will create a new function named MyHttpTrigger with a default HTTP trigger template.

Step 3: Understand the Function Code

Open the MyFunctionApp/MyHttpTrigger/index.js file. 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
};
};

This function logs a message, checks for a name parameter in the query string or request body, and returns a personalized greeting.

Step 4: Run the Function Locally

Start the Azure Functions host locally:

func start

Once the host starts, you'll see a list of available HTTP endpoints. The default URL for your HTTP trigger will look something like:

http://localhost:7071/api/MyHttpTrigger

Step 5: Test Your Function

Open your web browser and navigate to the URL above. You should see the default response. To test the personalization, add a name parameter to the URL:

http://localhost:7071/api/MyHttpTrigger?name=YourName

You should now see "Hello, YourName. This HTTP triggered function executed successfully."

Step 6: Deploy to Azure

To deploy your function to Azure, you'll need an Azure Function App resource. The easiest way to create and deploy is using the Azure Functions Core Tools with Azure CLI.

  1. Log in to your Azure account:
    az login
  2. Deploy your function app:
    func azure functionapp publish
    Replace <YOUR_FUNCTION_APP_NAME> with a globally unique name for your function app.

After deployment, you'll receive the URL for your deployed function in Azure. You can test it similarly to how you tested it locally.

Next Steps

Congratulations on creating and deploying your first Azure Function! Here are some resources to continue your learning: