Azure Functions Documentation

Your guide to serverless computing with Azure

Create Your First Azure Function Using the Azure CLI

This guide walks you through creating your first Azure Function using the Azure Command-Line Interface (CLI). This approach is ideal for developers who prefer a terminal-based workflow or for scripting and automation.

Prerequisites

Step 1: Sign in to Azure

Open your terminal or command prompt and sign in to your Azure account:

az login

This command will open a browser window for you to authenticate.

Step 2: Create a New Project

Navigate to the directory where you want to create your project and run the following command to create a new Functions project:

func init MyFunctionProj --worker-runtime dotnet --docker

This command initializes a new project named MyFunctionProj. We've specified the dotnet runtime and added the --docker flag to include a Dockerfile for containerization.

You can choose other runtimes like node, python, java, or powershell.

Step 3: Create Your First Function

Now, navigate into your new project directory and create your first function:

cd MyFunctionProj
func new --name HttpTriggerExample --template "HTTP trigger" --authlevel "anonymous"

This command creates a new function named HttpTriggerExample using the "HTTP trigger" template with anonymous authentication. You can change the template and authorization level as needed.

Step 4: Examine the Generated Files

Open your project in your favorite code editor. You'll see a structure similar to this:

MyFunctionProj/
├── HttpTriggerExample/
│   ├── __init__.py  (or HttpTriggerExample.cs, index.js, etc. depending on runtime)
│   └── function.json
├── .gitignore
├── host.json
├── local.settings.json
└── requirements.txt (or .csproj, package.json, etc.)
  • HttpTriggerExample/function.json: Defines the function's triggers and bindings.
  • HttpTriggerExample/__init__.py (or equivalent): Contains your function's code.
  • host.json: Global configuration settings for the Functions host.
  • local.settings.json: Local application settings, including connection strings.

Step 5: Run Your Function Locally

Start the Azure Functions host locally:

func start

The output will show the local endpoint for your HTTP trigger. It typically looks like this:

Http Functions:
    HttpTriggerExample: [GET,POST] http://localhost:7071/api/HttpTriggerExample

Open your browser and navigate to the URL provided. You can also send a request using tools like Postman or `curl`.

curl "http://localhost:7071/api/HttpTriggerExample?name=AzureCLI"

You should see a response like:

Hello, AzureCLI. This HTTP triggered function executed successfully.

Tip: Managing Local Settings

The local.settings.json file is crucial for managing local configurations, especially connection strings for services like Azure Storage or Cosmos DB. Remember to never commit this file to source control if it contains secrets.

Step 6: Deploy Your Function

To deploy your function app to Azure, you first need to create a Function App resource in Azure. You can do this via the Azure portal or the CLI:

az functionapp create --resource-group MyResourceGroup --consumption-plan-location westus2 --name MyUniqueFunctionAppName --storage-account MyUniqueStorageAccount --runtime dotnet --runtime-version 6 --functions-version 4

Replace MyResourceGroup, MyUniqueFunctionAppName, and MyUniqueStorageAccount with your desired names.

Once the Function App is created, deploy your code:

func azure functionapp publish MyUniqueFunctionAppName

This command will zip your project and upload it to the specified Azure Function App.

Next Steps