Skip to content

Commit d87c119

Browse files
Merge pull request #88173 from john-par/1561426-review-functionality-and-freshness-python-first-two-articles
1561426, review functionality and freshness python first two articles
2 parents 6608265 + ef43ce8 commit d87c119

7 files changed

+184
-147
lines changed

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

Lines changed: 96 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: Create an HTTP triggered function in Azure
33
description: Learn how to create your first Python function in Azure using the Azure Functions Core Tools and the Azure CLI.
44
author: ggailey777
55
ms.author: glenga
6-
ms.date: 04/24/2019
6+
ms.date: 09/11/2019
77
ms.topic: quickstart
88
ms.service: azure-functions
99
ms.custom: mvc
@@ -13,135 +13,146 @@ manager: gwallace
1313

1414
# Create an HTTP triggered function in Azure
1515

16-
This article shows you how to use command-line tools to create a Python project that runs in Azure Functions. The function you create is triggered by HTTP requests. Finally, you publish your project to run as a [serverless function](functions-scale.md#consumption-plan) in Azure.
16+
This article shows you how to use command-line tools to create a Python project that runs in Azure Functions. You also create a function that is triggered by an HTTP request. Finally, you publish your project to run as a [serverless function](functions-scale.md#consumption-plan) in Azure.
1717

18-
This article is the first of two quickstarts for Azure Functions. After you complete this article, you [add an Azure Storage queue output binding](functions-add-output-binding-storage-queue-python.md) to your function.
18+
This article is the first of two Python quickstarts for Azure Functions. After you complete this quickstart, you can [add an Azure Storage queue output binding](functions-add-output-binding-storage-queue-python.md) to your function.
1919

2020
## Prerequisites
2121

22-
Before you start, you must have the following:
22+
Before you start, you must:
2323

2424
+ Install [Python 3.6.x](https://www.python.org/downloads/).
2525

2626
+ Install [Azure Functions Core Tools](./functions-run-local.md#v2) version 2.7.1575 or a later version.
2727

2828
+ Install the [Azure CLI](/cli/azure/install-azure-cli) version 2.x or a later version.
2929

30-
+ An active Azure subscription.
30+
+ Have an active Azure subscription.
3131

32-
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
32+
[!INCLUDE [quickstarts-free-trial-note](../../includes/quickstarts-free-trial-note.md)]
3333

3434
## Create and activate a virtual environment (optional)
3535

36-
To locally develop and test Python functions, it is recommended to use a Python 3.6 environment. Run the following commands to create and activate a virtual environment named `.venv`.
36+
You should use a Python 3.6.x environment to locally develop Python functions. Run the following commands to create and activate a virtual environment named `.venv`.
3737

3838
> [!NOTE]
3939
> If Python didn't install venv on your Linux distribution, you can install it using the following command:
4040
> ```command
4141
> sudo apt-get install python3-venv
42-
>
4342
4443
### Bash:
4544
4645
```bash
47-
python3.6 -m venv .venv
46+
python -m venv .venv
4847
source .venv/bin/activate
4948
```
5049
5150
### PowerShell or a Windows command prompt:
5251
5352
```powershell
54-
py -3.6 -m venv .venv
53+
py -m venv .venv
5554
.venv\scripts\activate
5655
```
5756
58-
The remaining commands are run inside the virtual environment.
57+
Now that you activated the virtual environment, run the remaining commands in it. To get out of the virtual environment, run `deactivate`.
5958
60-
## Create a local Functions project
59+
## Create a local functions project
6160
62-
A Functions project is the equivalent of a function app in Azure. It can have multiple functions that all share the same local and hosting configurations.
61+
A functions project is the equivalent of a function app in Azure. It can have multiple functions that all share the same local and hosting configurations.
6362
64-
In the virtual environment, run the following command, choosing **python** as your worker runtime.
63+
1. In the virtual environment, run the following command:
6564
66-
```console
67-
func init MyFunctionProj
68-
```
69-
70-
A folder named _MyFunctionProj_ is created, which contains the following three files:
71-
72-
* `local.settings.json` is used to store app settings and connection strings when running locally. This file doesn't get published to Azure.
73-
* `requirements.txt` contains the list of packages to be installed on publishing to Azure.
74-
* `host.json` contains global configuration options that affect all functions in a function app. This file does get published to Azure.
75-
76-
Navigate to the new MyFunctionProj folder:
77-
78-
```console
79-
cd MyFunctionProj
80-
```
81-
82-
## Create a function
83-
84-
To add a function to your project, run the following command:
85-
86-
```console
87-
func new
88-
```
89-
90-
Choose the **HTTP trigger** template, type `HttpTrigger` as the name for the function, then press Enter.
65+
```console
66+
func init MyFunctionProj
67+
```
9168
92-
A subfolder named _HttpTrigger_ is created, which contains the following files:
69+
1. Select **python** as your worker runtime.
9370
94-
* **function.json**: configuration file that defines the function, trigger, and other bindings. Review this file and see that the value for `scriptFile` points to the file containing the function, while the invocation trigger and bindings are defined in the `bindings` array.
71+
The command creates a _MyFunctionProj_ folder. It contains these three files:
9572
96-
Each binding requires a direction, type and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook.md#trigger) and output binding of type [`http`](functions-bindings-http-webhook.md#output).
73+
* *local.settings.json*: used to store app settings and connection strings when running locally. This file doesn't get published to Azure.
74+
* *requirements.txt*: contains the list of packages the system will install on publishing to Azure.
75+
* *host.json*: contains global configuration options that affect all functions in a function app. This file does get published to Azure.
9776
98-
* **\_\_init\_\_.py**: script file that is your HTTP triggered function. Review this script and see that it contains a default `main()`. HTTP data from the trigger is passed to this function using the `req` named binding parameter. Defined in function.json, `req` is an instance of the [azure.functions.HttpRequest class](/python/api/azure-functions/azure.functions.httprequest).
77+
1. Go to the new *MyFunctionProj* folder:
9978
100-
The return object, defined as `$return` in function.json, is an instance of [azure.functions.HttpResponse class](/python/api/azure-functions/azure.functions.httpresponse). To learn more, see [Azure Functions HTTP triggers and bindings](functions-bindings-http-webhook.md).
79+
```console
80+
cd MyFunctionProj
81+
```
10182
102-
## Run the function locally
83+
## Create a function
10384
104-
The following command starts the function app, which runs locally using the same Azure Functions runtime that is in Azure.
85+
Add a function to the new project.
10586
106-
```console
107-
func host start
108-
```
87+
1. To add a function to your project, run the following command:
10988
110-
When the Functions host starts, it writes something like the following output, which has been truncated for readability:
89+
```console
90+
func new
91+
```
11192
112-
```output
113-
114-
%%%%%%
115-
%%%%%%
116-
@ %%%%%% @
117-
@@ %%%%%% @@
118-
@@@ %%%%%%%%%%% @@@
119-
@@ %%%%%%%%%% @@
120-
@@ %%%% @@
121-
@@ %%% @@
122-
@@ %% @@
123-
%%
124-
%
93+
1. Use your down-arrow to select the **HTTP trigger** template.
12594
126-
...
95+
1. When you're prompted for a function name, enter *HttpTrigger* and then press Enter.
12796
128-
Content root path: C:\functions\MyFunctionProj
129-
Now listening on: http://0.0.0.0:7071
130-
Application started. Press Ctrl+C to shut down.
97+
These commands create a subfolder named _HttpTrigger_. It contains the following files:
13198
132-
...
99+
* *function.json*: configuration file that defines the function, trigger, and other bindings. Notice that in this file, the value for `scriptFile` points to the file containing the function, and the `bindings` array defines the invocation trigger and bindings.
133100
134-
Http Functions:
101+
Each binding requires a direction, type and a unique name. The HTTP trigger has an input binding of type [`httpTrigger`](functions-bindings-http-webhook.md#trigger) and output binding of type [`http`](functions-bindings-http-webhook.md#output).
135102
136-
HttpTrigger: http://localhost:7071/api/HttpTrigger
103+
* *\_\_init\_\_.py*: script file that is your HTTP triggered function. Notice that this script has a default `main()`. HTTP data from the trigger passes to the function using the `req` named `binding parameter`. The `req`, which is defined in function.json, is an instance of the [azure.functions.HttpRequest class](/python/api/azure-functions/azure.functions.httprequest).
137104
138-
[8/27/2018 10:38:27 PM] Host started (29486ms)
139-
[8/27/2018 10:38:27 PM] Job host started
140-
```
105+
The return object, defined as `$return` in *function.json*, is an instance of [azure.functions.HttpResponse class](/python/api/azure-functions/azure.functions.httpresponse). To learn more, see [Azure Functions HTTP triggers and bindings](functions-bindings-http-webhook.md).
141106
142-
Copy the URL of your `HttpTrigger` function from the runtime output and paste it into your browser's address bar. Append the query string `?name=<yourname>` to this URL and execute the request. The following shows the response in the browser to the GET request returned by the local function:
107+
## Run the function locally
143108
144-
![Test locally in the browser](./media/functions-create-first-function-python/function-test-local-browser.png)
109+
The function runs locally using the Azure Functions runtime.
110+
111+
1. This command starts the function app:
112+
113+
```console
114+
func host start
115+
```
116+
117+
When the Azure Functions host starts, it writes something like the following output. It's truncated here so you can read it better:
118+
119+
```output
120+
121+
%%%%%%
122+
%%%%%%
123+
@ %%%%%% @
124+
@@ %%%%%% @@
125+
@@@ %%%%%%%%%%% @@@
126+
@@ %%%%%%%%%% @@
127+
@@ %%%% @@
128+
@@ %%% @@
129+
@@ %% @@
130+
%%
131+
%
132+
133+
...
134+
135+
Content root path: C:\functions\MyFunctionProj
136+
Now listening on: http://0.0.0.0:7071
137+
Application started. Press Ctrl+C to shut down.
138+
139+
...
140+
141+
Http Functions:
142+
143+
HttpTrigger: http://localhost:7071/api/HttpTrigger
144+
145+
[8/27/2018 10:38:27 PM] Host started (29486ms)
146+
[8/27/2018 10:38:27 PM] Job host started
147+
```
148+
149+
1. Copy the URL of your `HttpTrigger` function from the runtime output and paste it into your browser's address bar.
150+
151+
1. Append the query string `?name=<yourname>` to this URL and execute the request. The following screenshot shows the response to the GET request that the local function returns to the browser:
152+
153+
![Verify locally in the browser](./media/functions-create-first-function-python/function-test-local-browser.png)
154+
155+
1. Select Ctrl+C to shut down your function app.
145156
146157
Now that you have run your function locally, you can create the function app and other required resources in Azure.
147158
@@ -153,31 +164,32 @@ Now that you have run your function locally, you can create the function app and
153164
154165
A function app provides an environment for executing your function code. It lets you group functions as a logical unit for easier management, deployment, and sharing of resources.
155166
156-
Run the following command using a unique function app name in place of the `<APP_NAME>` placeholder and the storage account name for `<STORAGE_NAME>`. The `<APP_NAME>` is also the default DNS domain for the function app. This name needs to be unique across all apps in Azure.
167+
Run the following command. Replace `<APP_NAME>` with a unique function app name. Replace `<STORAGE_NAME>` with a storage account name. The `<APP_NAME>` is also the default DNS domain for the function app. This name needs to be unique across all apps in Azure.
168+
169+
> [!NOTE]
170+
> You can't host Linux and Windows apps in the same resource group. If you have an existing resource group named `myResourceGroup` with a Windows function app or web app, you must use a different resource group.
157171
158172
```azurecli-interactive
159173
az functionapp create --resource-group myResourceGroup --os-type Linux \
160174
--consumption-plan-location westeurope --runtime python \
161175
--name <APP_NAME> --storage-account <STORAGE_NAME>
162176
```
163-
> [!NOTE]
164-
> Linux and Windows apps cannot be hosted in the same resource group. If you have an existing resource group named `myResourceGroup` with a Windows function app or web app, you must use a different resource group.
165177
166-
This command will also provision an associated Azure Application Insights instance in the same resource group that can be used for monitoring and viewing logs.
178+
The preceding command also provisions an associated Azure Application Insights instance in the same resource group. You can use this instance to monitor your function app and view logs.
167179
168180
You're now ready to publish your local functions project to the function app in Azure.
169181
170182
## Deploy the function app project to Azure
171183
172-
After the function app is created in Azure, you can use the [`func azure functionapp publish`](functions-run-local.md#project-file-deployment) Core Tools command to deploy your project code to Azure. In these examples, replace `<APP_NAME>` with the name of your app from the previous step.
184+
After you create the function app in Azure, you can use the [func azure functionapp publish](functions-run-local.md#project-file-deployment) Core Tools command to deploy your project code to Azure. In this example, replace `<APP_NAME>` with the name of your app.
173185
174-
```command
186+
```console
175187
func azure functionapp publish <APP_NAME> --build remote
176188
```
177189
178190
The `--build remote` option builds your Python project remotely in Azure from the files in the deployment package.
179191
180-
You'll see output similar to the following, which has been truncated for readability:
192+
You'll see output similar to the following message. It's truncated here so you can read it better:
181193
182194
```output
183195
Getting site publishing info...
@@ -193,12 +205,12 @@ Functions in myfunctionapp:
193205
Invoke url: https://myfunctionapp.azurewebsites.net/api/httptrigger?code=cCr8sAxfBiow548FBDLS1....
194206
```
195207
196-
Copy the `Invoke url` value for your `HttpTrigger`, which you can now use to test your function in Azure. The URL contains a `code` query string value that is your function key. This key makes it difficult for others to call your HTTP trigger endpoint in Azure.
208+
You can copy the `Invoke url` value for your `HttpTrigger` and use it to verify your function in Azure. The URL contains a `code` query string value that is your function key, which makes it difficult for others to call your HTTP trigger endpoint in Azure.
197209
198210
[!INCLUDE [functions-test-function-code](../../includes/functions-test-function-code.md)]
199211
200212
> [!NOTE]
201-
> To view near real-time logs for a published Python app, we recommend using the [Application Insights Live Metrics Stream](functions-monitoring.md#streaming-logs)
213+
> To view near real-time logs for a published Python app, use the [Application Insights Live Metrics Stream](functions-monitoring.md#streaming-logs).
202214
203215
## Next steps
204216

0 commit comments

Comments
 (0)