Skip to content

Commit 6705b9a

Browse files
Merge pull request #244799 from ggailey777/patch-2
[Functions] Fix v2 procedure and make clear that the `EnableWorkerIndexing` flag is required
2 parents 87602e6 + 0a44375 commit 6705b9a

File tree

4 files changed

+37
-131
lines changed

4 files changed

+37
-131
lines changed

articles/azure-functions/create-first-function-cli-python.md

Lines changed: 27 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
title: Create a Python function from the command line - Azure Functions
33
description: Learn how to create a Python function from the command line, then publish the local project to serverless hosting in Azure Functions.
4-
ms.date: 03/22/2023
4+
ms.date: 07/15/2023
55
ms.topic: quickstart
66
ms.devlang: python
77
ms.custom: devx-track-python, devx-track-azurecli, devx-track-azurepowershell, mode-api, devdivchpfy22
@@ -28,12 +28,8 @@ Before you begin, you must have the following requirements in place:
2828

2929
+ An Azure account with an active subscription. [Create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio).
3030

31-
::: zone pivot="python-mode-configuration"
3231
+ The [Azure Functions Core Tools](functions-run-local.md#v2) version 4.x.
33-
::: zone-end
34-
::: zone pivot="python-mode-decorators"
35-
+ The [Azure Functions Core Tools](functions-run-local.md#v2) version 4.2.1 or later.
36-
::: zone-end
32+
3733
+ One of the following tools for creating Azure resources:
3834

3935
+ [Azure CLI](/cli/azure/install-azure-cli) version 2.4 or later.
@@ -47,36 +43,6 @@ Before you begin, you must have the following requirements in place:
4743

4844
[!INCLUDE [functions-x86-emulation-on-arm64-note](../../includes/functions-x86-emulation-on-arm64-note.md)]
4945

50-
### Prerequisite check
51-
52-
Verify your prerequisites, which depend on whether you're using Azure CLI or Azure PowerShell for creating Azure resources.
53-
54-
# [Azure CLI](#tab/azure-cli)
55-
56-
::: zone pivot="python-mode-configuration"
57-
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools version is 4.x.
58-
::: zone-end
59-
::: zone pivot="python-mode-decorators"
60-
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools version is 4.0.4785 or later.
61-
::: zone-end
62-
+ Run `az --version` to check that the Azure CLI version is 2.4 or later.
63-
64-
+ Run `az login` to sign in to Azure and verify an active subscription.
65-
66-
+ Run `python --version` (Linux/macOS) or `py --version` (Windows) to check your Python version reports 3.9.x, 3.8.x, or 3.7.x.
67-
68-
# [Azure PowerShell](#tab/azure-powershell)
69-
70-
+ In a terminal or command window, run `func --version` to check that the Azure Functions Core Tools version is 4.x.
71-
72-
+ Run `(Get-Module -ListAvailable Az).Version` and verify version 5.0 or later.
73-
74-
+ Run `Connect-AzAccount` to sign in to Azure and verify an active subscription.
75-
76-
+ Run `python --version` (Linux/macOS) or `py --version` (Windows) to check your Python version reports 3.9.x, 3.8.x, or 3.7.x.
77-
78-
---
79-
8046
## <a name="create-venv"></a>Create and activate a virtual environment
8147

8248
In a suitable folder, run the following commands to create and activate a virtual environment named `.venv`. Make sure that you're using Python 3.9, 3.8, or 3.7, which are supported by Azure Functions.
@@ -146,15 +112,9 @@ In Azure Functions, a function project is a container for one or more individual
146112
func new --name HttpExample --template "HTTP trigger" --authlevel "anonymous"
147113
```
148114

149-
`func new` creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named *function.json*.
150-
151-
Get the list of templates by using the following command:
152-
153-
```console
154-
func templates list -l python
155-
```
156-
::: zone-end
157-
::: zone pivot="python-mode-decorators"
115+
`func new` creates a subfolder matching the function name that contains a code file appropriate to the project's chosen language and a configuration file named *function.json*.
116+
::: zone-end
117+
::: zone pivot="python-mode-decorators"
158118
1. Run the `func init` command as follows to create a functions project in a folder named *LocalFunctionProj* with the specified runtime and the specified programming model version.
159119

160120
```console
@@ -166,51 +126,33 @@ In Azure Functions, a function project is a container for one or more individual
166126
```console
167127
cd LocalFunctionProj
168128
```
169-
129+
170130
This folder contains various files for the project, including configuration 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.
171131

172132
1. The file `function_app.py` can include all functions within your project. To start with, there's already an HTTP function stored in the file.
173133

174-
```python
175-
import azure.functions as func
176-
177-
app = func.FunctionApp()
178-
179-
@app.function_name(name="HttpTrigger1")
180-
@app.route(route="hello")
181-
def test_function(req: func.HttpRequest) -> func.HttpResponse:
182-
return func.HttpResponse("HttpTrigger1 function processed a request!")
183-
```
184-
::: zone-end
185-
186-
### (Optional) Examine the file contents
187-
188-
If desired, you can skip to [Run the function locally](#run-the-function-locally) and examine the file contents later.
189-
190-
::: zone pivot="python-mode-configuration"
191-
#### \_\_init\_\_.py
192-
193-
*\_\_init\_\_.py* contains a `main()` Python function that's triggered according to the configuration in *function.json*.
194-
195-
:::code language="python" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/__init__.py":::
196-
197-
For an HTTP trigger, the function receives request data in the variable `req` as defined in *function.json*. `req` is an instance of the [azure.functions.HttpRequest class](/python/api/azure-functions/azure.functions.httprequest). The return object, defined as `$return` in *function.json*, is an instance of [azure.functions.HttpResponse class](/python/api/azure-functions/azure.functions.httpresponse). For more information, see [Azure Functions HTTP triggers and bindings](./functions-bindings-http-webhook.md?tabs=python).
198-
199-
#### function.json
200-
201-
*function.json* is a configuration file that defines the input and output `bindings` for the function, including the trigger type.
202-
203-
If desired, you can change `scriptFile` to invoke a different Python file.
204-
205-
:::code language="json" source="~/functions-quickstart-templates/Functions.Templates/Templates/HttpTrigger-Python/function.json":::
206-
207-
Each binding requires a direction, a type, and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook-trigger.md) and output binding of type [`http`](functions-bindings-http-webhook-output.md).
208-
::: zone-end
209-
::: zone pivot="python-mode-decorators"
210-
`function_app.py` is the entry point to the function and where functions will be stored and/or referenced. This file will include configuration of triggers and bindings through decorators, and the function content itself.
211-
212-
For more information, see [Azure Functions HTTP triggers and bindings](./functions-bindings-http-webhook.md?tabs=python).
134+
```python
135+
import azure.functions as func
136+
137+
app = func.FunctionApp()
138+
139+
@app.function_name(name="HttpTrigger1")
140+
@app.route(route="hello")
141+
def test_function(req: func.HttpRequest) -> func.HttpResponse:
142+
return func.HttpResponse("HttpTrigger1 function processed a request!")
143+
```
144+
145+
1. Open the local.settings.json project file and verify that the `AzureWebJobsFeatureFlags` setting has a value of `EnableWorkerIndexing`. This is required for Functions to interpret your project correctly as the Python v2 model. You'll add this same setting to your application settings after you publish your project to Azure.
146+
147+
1. In the local.settings.json file, update the `AzureWebJobsStorage` setting as in the following example:
148+
149+
```json
150+
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
151+
```
213152

153+
This tells the local Functions host to use the storage emulator for the storage connection currently required by the Python v2 model. When you publish your project to Azure, you'll need to instead use the default storage account. If you're instead using an Azure Storage account, set your storage account connection string here.
154+
::: zone-end
155+
::: zone pivot="python-mode-decorators"
214156
## Start the storage emulator
215157

216158
By default, local development uses the Azurite storage emulator. This emulator is used when the `AzureWebJobsStorage` setting in the *local.settings.json* project file is set to `UseDevelopmentStorage=true`. When using the emulator, you must start the local Azurite storage emulator before running the function.

articles/azure-functions/create-first-function-vs-code-python.md

Lines changed: 8 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
7575
|**Select a template for your project's first function**| Choose `HTTP trigger`.|
7676
|**Provide a function name**| Enter `HttpExample`.|
7777
|**Authorization level**| Choose `Anonymous`, which lets anyone call your function endpoint. For more information about the authorization level, see [Authorization keys](functions-bindings-http-webhook-trigger.md#authorization-keys).|
78-
|**Select how you would like to open your project**| Choose `Open in current window`.|
7978

8079
4. Visual Studio Code uses the provided information and generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. For more information about the files that are created, see [Generated project files](functions-develop-vs-code.md?tabs=python#generated-project-files).
8180
::: zone-end
@@ -86,56 +85,21 @@ In this section, you use Visual Studio Code to create a local Azure Functions pr
8685
|--|--|
8786
|**Select a language**| Choose `Python (Programming Model V2)`.|
8887
|**Select a Python interpreter to create a virtual environment**| Choose your preferred Python interpreter. If an option isn't shown, type in the full path to your Python binary.|
89-
|**Select how you would like to open your project**| Choose `Open in current window`.|
88+
|**Select a template for your project's first function** | Choose `HTTP trigger`. |
89+
|**Name of the function you want to create**| Enter `HttpExample`.|
90+
|**Authorization level**| Choose `ANONYMOUS`, which lets anyone call your function endpoint. For more information about the authorization level, see [Authorization keys](functions-bindings-http-webhook-trigger.md#authorization-keys).|
9091

91-
4. Visual Studio Code uses the provided information and generates an Azure Functions project.
92-
93-
5. Open the generated `function_app.py` project file, which contains your functions.
94-
95-
6. Uncomment the `test_function` function, which is an HTTP triggered function.
96-
97-
7. Replace the `app.route()` method call with the following code:
98-
99-
```python
100-
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
101-
```
102-
103-
This code enables your HTTP function endpoint to be called in Azure without having to provide an [Authorization keys](functions-bindings-http-webhook-trigger.md#authorization-keys). Local execution doesn't require authorization keys.
104-
105-
Your function code should now look like the following example:
106-
107-
```python
108-
app = func.FunctionApp()
109-
@app.function_name(name="HttpTrigger1")
110-
@app.route(route="hello", auth_level=func.AuthLevel.ANONYMOUS)
111-
def test_function(req: func.HttpRequest) -> func.HttpResponse:
112-
logging.info('Python HTTP trigger function processed a request.')
113-
114-
name = req.params.get('name')
115-
if not name:
116-
try:
117-
req_body = req.get_json()
118-
except ValueError:
119-
pass
120-
else:
121-
name = req_body.get('name')
122-
123-
if name:
124-
return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
125-
else:
126-
return func.HttpResponse(
127-
"This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
128-
status_code=200
129-
)
130-
```
92+
4. Visual Studio Code uses the provided information and generates an Azure Functions project with an HTTP trigger. You can view the local project files in the Explorer. The generated `function_app.py` project file contains your functions.
13193

132-
8. Open the local.settings.json project file and updated the `AzureWebJobsStorage` setting as in the following example:
94+
5. Open the local.settings.json project file and verify that the `AzureWebJobsFeatureFlags` setting has a value of `EnableWorkerIndexing`. This is required for Functions to interpret your project correctly as the Python v2 model. You'll add this same setting to your application settings after you publish your project to Azure.
95+
96+
6. In the local.settings.json file, update the `AzureWebJobsStorage` setting as in the following example:
13397

13498
```json
13599
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
136100
```
137101

138-
This tells the local Functions host to use the storage emulator for the storage connection currently required by the v2 model. When you publish your project to Azure, you'll instead use the default storage account. If you're instead using an Azure Storage account, set your storage account connection string here.
102+
This tells the local Functions host to use the storage emulator for the storage connection currently required by the Python v2 model. When you publish your project to Azure, you'll need to instead use the default storage account. If you're instead using an Azure Storage account, set your storage account connection string here.
139103

140104
## Start the emulator
141105

articles/azure-functions/functions-machine-learning-tensorflow.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ In Azure Functions, a function project is a container for one or more individual
111111
func new --name classify --template "HTTP trigger"
112112
```
113113
114-
This command creates a folder matching the name of the function, *classify*. In that folder are two files: *\_\_init\_\_.py*, which contains the function code, and *function.json*, which describes the function's trigger and its input and output bindings. For details on the contents of these files, see [Examine the file contents](./create-first-function-cli-python.md#optional-examine-the-file-contents) in the Python quickstart.
114+
This command creates a folder matching the name of the function, *classify*. In that folder are two files: *\_\_init\_\_.py*, which contains the function code, and *function.json*, which describes the function's trigger and its input and output bindings. For details on the contents of these files, see [Programming model](./functions-reference-python.md?pivots=python-mode-configuration#programming-model) in the Python developer guide.
115115
116116
117117
## Run the function locally

articles/azure-functions/machine-learning-pytorch.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ In Azure Functions, a function project is a container for one or more individual
109109
func new --name classify --template "HTTP trigger"
110110
```
111111
112-
This command creates a folder matching the name of the function, *classify*. In that folder are two files: *\_\_init\_\_.py*, which contains the function code, and *function.json*, which describes the function's trigger and its input and output bindings. For details on the contents of these files, see [Examine the file contents](./create-first-function-cli-python.md#optional-examine-the-file-contents) in the Python quickstart.
112+
This command creates a folder matching the name of the function, *classify*. In that folder are two files: *\_\_init\_\_.py*, which contains the function code, and *function.json*, which describes the function's trigger and its input and output bindings. For details on the contents of these files, see [Programming model](./functions-reference-python.md?pivots=python-mode-configuration#programming-model) in the Python developer guide.
113113
114114
115115
## Run the function locally

0 commit comments

Comments
 (0)