|
| 1 | +--- |
| 2 | +title: "Create a function in Azure using the Azure CLI" |
| 3 | +description: "Learn how to create an Azure Functions code project from the command line using the Azure CLI, then publish the local project to serverless hosting in Azure Functions." |
| 4 | +ms.date: 07/08/2025 |
| 5 | +ms.topic: quickstart |
| 6 | +ms.custom: devx-track-csharp, devx-track-azurecli, devx-track-azurepowershell, mode-other, devx-track-dotnet |
| 7 | +zone_pivot_groups: programming-languages-set-functions |
| 8 | +--- |
| 9 | + |
| 10 | +# Quickstart: Create a function in Azure from the command line |
| 11 | + |
| 12 | +In this article, you use command-line tools locally to create a function that responds to HTTP requests. After verifying your code locally, you deploy it to a serverless hosting plan in Azure Functions. |
| 13 | + |
| 14 | +Completing this quickstart incurs a small cost of a few USD cents or less in your Azure account. |
| 15 | + |
| 16 | +## Configure your local environment |
| 17 | + |
| 18 | +Before you begin, you must have the following: |
| 19 | + |
| 20 | +[!INCLUDE [functions-requirements-azure-cli](../../includes/functions-requirements-azure-cli.md)] |
| 21 | + |
| 22 | ++ The [`jq` command line JSON processor](https://jqlang.org/download/), used to parse JSON output, and is also available in Azure Cloud Shell. |
| 23 | + |
| 24 | +[!INCLUDE [functions-install-core-tools](../../includes/functions-install-core-tools.md)] |
| 25 | + |
| 26 | +## Create a local function project and function |
| 27 | + |
| 28 | +In Azure Functions, a function project is a container for one or more individual functions that each responds to a specific trigger. All functions in a project share the same local and hosting configurations. In this section, you create a function project that contains a single function. |
| 29 | +::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python" |
| 30 | +1. In a terminal or command prompt, run the following command for your chosen language to create a function app project in the current folder: |
| 31 | +::: zone-end |
| 32 | +::: zone pivot="programming-language-csharp" |
| 33 | + |
| 34 | + ```console |
| 35 | + func init --worker-runtime dotnet-isolated |
| 36 | + ``` |
| 37 | +::: zone-end |
| 38 | +::: zone pivot="programming-language-javascript" |
| 39 | + ```console |
| 40 | + func init --worker-runtime node --language javascript |
| 41 | + ``` |
| 42 | +::: zone-end |
| 43 | +::: zone pivot="programming-language-powershell" |
| 44 | + ```console |
| 45 | + func init --worker-runtime powershell |
| 46 | + ``` |
| 47 | +::: zone-end |
| 48 | +::: zone pivot="programming-language-python" |
| 49 | + ```console |
| 50 | + func init --worker-runtime python |
| 51 | + ``` |
| 52 | +::: zone-end |
| 53 | +::: zone pivot="programming-language-typescript" |
| 54 | + ```console |
| 55 | + func init --worker-runtime node --language typescript |
| 56 | + ``` |
| 57 | +::: zone-end |
| 58 | +::: zone pivot="programming-language-java" |
| 59 | +<!--- The Maven archetype requires it's own create flow...--> |
| 60 | +1. In an empty folder, run this `mvn` command to generate the code project from an Azure Functions [Maven archetype](https://maven.apache.org/guides/introduction/introduction-to-archetypes.html): |
| 61 | + |
| 62 | + |
| 63 | + ### [Bash](#tab/bash) |
| 64 | + |
| 65 | + ```bash |
| 66 | + mvn archetype:generate -DarchetypeGroupId=com.microsoft.azure -DarchetypeArtifactId=azure-functions-archetype -DjavaVersion=8 |
| 67 | + ``` |
| 68 | + |
| 69 | + ### [PowerShell](#tab/powershell) |
| 70 | + |
| 71 | + ```powershell |
| 72 | + mvn archetype:generate "-DarchetypeGroupId=com.microsoft.azure" "-DarchetypeArtifactId=azure-functions-archetype" "-DjavaVersion=8" |
| 73 | + ``` |
| 74 | + |
| 75 | + ### [Cmd](#tab/cmd) |
| 76 | + |
| 77 | + ```cmd |
| 78 | + mvn archetype:generate "-DarchetypeGroupId=com.microsoft.azure" "-DarchetypeArtifactId=azure-functions-archetype" "-DjavaVersion=8" |
| 79 | + ``` |
| 80 | + |
| 81 | + --- |
| 82 | + |
| 83 | + > [!IMPORTANT] |
| 84 | + > + Use `-DjavaVersion=11` if you want your functions to run on Java 11. To learn more, see [Java versions](functions-reference-java.md#java-versions). |
| 85 | + > + The `JAVA_HOME` environment variable must be set to the install location of the correct version of the JDK to complete this article. |
| 86 | + |
| 87 | +2. Maven asks you for values needed to finish generating the project on deployment. |
| 88 | + Provide the following values when prompted: |
| 89 | + |
| 90 | + | Prompt | Value | Description | |
| 91 | + | ------ | ----- | ----------- | |
| 92 | + | **groupId** | `com.fabrikam` | A value that uniquely identifies your project across all projects, following the [package naming rules](https://docs.oracle.com/javase/specs/jls/se6/html/packages.html#7.7) for Java. | |
| 93 | + | **artifactId** | `fabrikam-functions` | A value that is the name of the jar, without a version number. | |
| 94 | + | **version** | `1.0-SNAPSHOT` | Choose the default value. | |
| 95 | + | **package** | `com.fabrikam` | A value that is the Java package for the generated function code. Use the default. | |
| 96 | + |
| 97 | +3. Type `Y` or press Enter to confirm. |
| 98 | + |
| 99 | + Maven creates the project files in a new folder with a name of _artifactId_, which in this example is `fabrikam-functions`. |
| 100 | + |
| 101 | +4. Navigate into the project folder: |
| 102 | + |
| 103 | + ```console |
| 104 | + cd fabrikam-functions |
| 105 | + ``` |
| 106 | +::: zone-end |
| 107 | + |
| 108 | + The project root folder contains various files for the project, including configurations files named [local.settings.json](functions-develop-local.md#local-settings-file) and [host.json](functions-host-json.md). Because *local.settings.json* can contain secrets downloaded from Azure, the file is excluded from source control by default in the *.gitignore* file. |
| 109 | + |
| 110 | +::: zone pivot="programming-language-csharp,programming-language-javascript,programming-language-typescript,programming-language-powershell,programming-language-python" |
| 111 | +2. Use this `func new` command to add a function to your project: |
| 112 | + |
| 113 | + ```console |
| 114 | + func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous" |
| 115 | + ``` |
| 116 | + |
| 117 | + A new code file is added to your project. In this case, the `--name` argument is the unique name of your function (`HttpExample`) and the `--template` argument specifies an HTTP trigger. |
| 118 | +::: zone-end |
| 119 | +## Run the function locally |
| 120 | + |
| 121 | +1. Run your function by starting the local Azure Functions runtime host from the root folder: |
| 122 | + |
| 123 | + To test the function locally, start the local Azure Functions runtime host in the root of the project folder. |
| 124 | + ::: zone pivot="programming-language-csharp" |
| 125 | + ```console |
| 126 | + func start |
| 127 | + ``` |
| 128 | + ::: zone-end |
| 129 | + ::: zone pivot="programming-language-javascript,programming-language-powershell,programming-language-python" |
| 130 | + ```console |
| 131 | + func start |
| 132 | + ``` |
| 133 | + ::: zone-end |
| 134 | + ::: zone pivot="programming-language-typescript" |
| 135 | + ```console |
| 136 | + npm install |
| 137 | + npm start |
| 138 | + ``` |
| 139 | + ::: zone-end |
| 140 | + ::: zone pivot="programming-language-java" |
| 141 | + ```console |
| 142 | + mvn clean package |
| 143 | + mvn azure-functions:run |
| 144 | + ``` |
| 145 | + ::: zone-end |
| 146 | + |
| 147 | + Toward the end of the output, the following lines should appear: |
| 148 | + |
| 149 | + <pre> |
| 150 | + ... |
| 151 | + |
| 152 | + Now listening on: http://0.0.0.0:7071 |
| 153 | + Application started. Press Ctrl+C to shut down. |
| 154 | + |
| 155 | + Http Functions: |
| 156 | + |
| 157 | + HttpExample: [GET,POST] http://localhost:7071/api/HttpExample |
| 158 | + ... |
| 159 | + |
| 160 | + </pre> |
| 161 | + |
| 162 | + >[!NOTE] |
| 163 | + > If HttpExample doesn't appear as shown above, you likely started the host from outside the root folder of the project. In that case, use **Ctrl**+**C** to stop the host, navigate to the project's root folder, and run the previous command again. |
| 164 | + |
| 165 | +1. Copy the URL of your `HttpExample` function from this output to a browser and browse to the function URL and you should receive success response with a "hello world" message. |
| 166 | + |
| 167 | +1. When you're done, use **Ctrl**+**C** and choose `y` to stop the functions host. |
| 168 | +
|
| 169 | +[!INCLUDE [functions-create-azure-resources-cli](../../includes/functions-create-azure-resources-flex-cli.md)] |
| 170 | +
|
| 171 | +## Update application settings |
| 172 | +
|
| 173 | +To enable the Functions host to connect to the default storage account using shared secrets, you must replace the `AzureWebJobsStorage` connection string setting with a complex setting, prefixed with `AzureWebJobsStorage`, that uses the user-assigned managed identity to connect to the storage account. |
| 174 | +
|
| 175 | +1. Remove the existing `AzureWebJobsStorage` connection string setting: |
| 176 | +
|
| 177 | + :::code language="azurecli" source="~/azure_cli_scripts/azure-functions/create-function-app-flex-plan-identities/create-function-app-flex-plan-identities.md" range="52" ::: |
| 178 | +
|
| 179 | + The [az functionapp config appsettings delete](/cli/azure/functionapp/config/appsettings#az-functionapp-config-appsettings-delete) command removes this setting from your app. |
| 180 | +
|
| 181 | +1. Add equivalent settings, with an `AzureWebJobsStorage__` prefix, that define a user-assigned managed identity connection to the default storage account: |
| 182 | + |
| 183 | + :::code language="azurecli" source="~/azure_cli_scripts/azure-functions/create-function-app-flex-plan-identities/create-function-app-flex-plan-identities.md" range="47-51" ::: |
| 184 | + |
| 185 | +At this point, the Functions host is able to connect to the storage account securely using managed identities. You can now deploy your project code to the Azure resources |
| 186 | +
|
| 187 | +[!INCLUDE [functions-publish-project-cli](../../includes/functions-publish-project-cli.md)] |
| 188 | +
|
| 189 | +## Invoke the function on Azure |
| 190 | +
|
| 191 | +Because your function uses an HTTP trigger and supports GET requests, you invoke it by making an HTTP request to its URL. It's easiest to do this in a browser. |
| 192 | + |
| 193 | +Copy the complete **Invoke URL** shown in the output of the publish command into a browser address bar. When you navigate to this URL, the browser should display similar output as when you ran the function locally. |
| 194 | + |
| 195 | +--- |
| 196 | + |
| 197 | +[!INCLUDE [functions-streaming-logs-cli-qs](../../includes/functions-streaming-logs-cli-qs.md)] |
| 198 | + |
| 199 | +[!INCLUDE [functions-cleanup-resources-cli](../../includes/functions-cleanup-resources-cli.md)] |
| 200 | + |
| 201 | +## Next steps |
| 202 | + |
| 203 | +> [!div class="nextstepaction"] |
| 204 | +> [Connect to Azure Cosmos DB](functions-add-output-binding-cosmos-db-vs-code.md?pivots=programming-language-csharp&tabs=isolated-process) |
| 205 | +> [!div class="nextstepaction"] |
| 206 | +> [Connect to Azure Queue Storage](functions-add-output-binding-storage-queue-cli.md?pivots=programming-language-csharp&tabs=isolated-process) |
0 commit comments