Skip to content

Commit 1d9a1ad

Browse files
committed
1561486, finished functionality review and refresh on the two articles.
1 parent 0aa88c3 commit 1d9a1ad

File tree

2 files changed

+161
-112
lines changed

2 files changed

+161
-112
lines changed

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

Lines changed: 91 additions & 75 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,151 @@ 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. An HTTP request triggers the function you create. Finally, you publish your project to run as a [serverless function](functions-scale.md#consumption-plan) in Azure.
1717

1818
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.
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

3232
[!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+
To locally develop and test Python functions, it's recommended to use a Python 3.6.x environment. 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 the virtualenv is activated, you'll run the remaining commands in it. If you want to get out of the virtual environment, run this command:
58+
59+
```console
60+
deactivate
61+
```
5962
6063
## Create a local Functions project
6164
6265
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.
6366
64-
In the virtual environment, run the following command, choosing **python** as your worker runtime.
67+
1. In the virtual environment, run the following command:
6568
6669
```console
6770
func init MyFunctionProj
6871
```
72+
1. Select **python** as your worker runtime.
6973
70-
A folder named _MyFunctionProj_ is created, which contains the following three files:
74+
The command creates a _MyFunctionProj_. It contains these three files:
7175
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.
76+
* `local.settings.json`: used to store app settings and connection strings when running locally. This file doesn't get published to Azure.
77+
* `requirements.txt`: contains the list of packages the system will install on publishing to Azure.
78+
* `host.json`: contains global configuration options that affect all functions in a function app. This file does get published to Azure.
7579
76-
Navigate to the new MyFunctionProj folder:
80+
1. Go to the new *MyFunctionProj* folder:
7781
78-
```console
79-
cd MyFunctionProj
80-
```
82+
```console
83+
cd MyFunctionProj
84+
```
8185
8286
## Create a function
8387
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.
91-
92-
A subfolder named _HttpTrigger_ is created, which contains the following files:
93-
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.
95-
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).
97-
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).
88+
Now it's time to create a function for the new project.
9989
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).
90+
1. To add a function to your project, run the following command:
10191
102-
## Run the function locally
92+
```console
93+
func new
94+
```
10395
104-
The following command starts the function app, which runs locally using the same Azure Functions runtime that is in Azure.
96+
1. Use your down-arrow to select the **HTTP trigger** template.
10597
106-
```console
107-
func host start
108-
```
98+
1. When you're prompted for a function name, enter *HttpTrigger* and then press Enter.
10999
110-
When the Functions host starts, it writes something like the following output, which has been truncated for readability:
100+
These commands create a subfolder named _HttpTrigger_. It contains the following files:
111101
112-
```output
102+
* **function.json**: configuration file that defines the function, trigger, and other bindings. Take a look at this file. Notice that the value for `scriptFile` points to the file containing the function, while the system defines the invocation trigger and bindings in the `bindings` array.
113103
114-
%%%%%%
115-
%%%%%%
116-
@ %%%%%% @
117-
@@ %%%%%% @@
118-
@@@ %%%%%%%%%%% @@@
119-
@@ %%%%%%%%%% @@
120-
@@ %%%% @@
121-
@@ %%% @@
122-
@@ %% @@
123-
%%
124-
%
104+
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).
125105
126-
...
127-
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.
106+
* **\_\_init\_\_.py**: script file that is your HTTP triggered function. Open the script. Take note 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).
131107
132-
...
108+
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).
133109
134-
Http Functions:
135-
136-
HttpTrigger: http://localhost:7071/api/HttpTrigger
137-
138-
[8/27/2018 10:38:27 PM] Host started (29486ms)
139-
[8/27/2018 10:38:27 PM] Job host started
140-
```
141-
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:
110+
## Run the function locally
143111
144-
![Test locally in the browser](./media/functions-create-first-function-python/function-test-local-browser.png)
112+
The function runs locally using the same Azure Functions runtime that is in Azure.
113+
114+
1. This command starts the function app:
115+
116+
```console
117+
func host start
118+
```
119+
120+
When the Functions host starts, it writes something like the following output. It's truncated so you can read it better:
121+
122+
```output
123+
124+
%%%%%%
125+
%%%%%%
126+
@ %%%%%% @
127+
@@ %%%%%% @@
128+
@@@ %%%%%%%%%%% @@@
129+
@@ %%%%%%%%%% @@
130+
@@ %%%% @@
131+
@@ %%% @@
132+
@@ %% @@
133+
%%
134+
%
135+
136+
...
137+
138+
Content root path: C:\functions\MyFunctionProj
139+
Now listening on: http://0.0.0.0:7071
140+
Application started. Press Ctrl+C to shut down.
141+
142+
...
143+
144+
Http Functions:
145+
146+
HttpTrigger: http://localhost:7071/api/HttpTrigger
147+
148+
[8/27/2018 10:38:27 PM] Host started (29486ms)
149+
[8/27/2018 10:38:27 PM] Job host started
150+
```
151+
152+
1. Copy the URL of your `HttpTrigger` function from the runtime output.
153+
154+
1. Paste it into your browser's address bar.
155+
156+
1. Append the query string `?name=<yourname>` to this URL and execute the request. The following screenshot shows the response in the browser to the GET request returned by the local function:
157+
158+
![Test locally in the browser](./media/functions-create-first-function-python/function-test-local-browser.png)
159+
160+
1. Select Ctrl+C to shut down your function app
145161
146162
Now that you have run your function locally, you can create the function app and other required resources in Azure.
147163
@@ -153,31 +169,31 @@ Now that you have run your function locally, you can create the function app and
153169
154170
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.
155171
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.
172+
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.
157173
158174
```azurecli-interactive
159175
az functionapp create --resource-group myResourceGroup --os-type Linux \
160176
--consumption-plan-location westeurope --runtime python \
161177
--name <APP_NAME> --storage-account <STORAGE_NAME>
162178
```
163179
> [!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.
180+
> 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.
165181
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.
182+
This command will also provision an associated Azure Application Insights instance. It will be in the same resource group that you can use for monitoring and viewing logs.
167183
168184
You're now ready to publish your local functions project to the function app in Azure.
169185
170186
## Deploy the function app project to Azure
171187
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.
188+
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.
173189
174-
```command
190+
```azurecli-interactive
175191
func azure functionapp publish <APP_NAME> --build remote
176192
```
177193
178194
The `--build remote` option builds your Python project remotely in Azure from the files in the deployment package.
179195
180-
You'll see output similar to the following, which has been truncated for readability:
196+
You'll see output similar to the following message. It's truncated so you can read it better:
181197
182198
```output
183199
Getting site publishing info...
@@ -193,7 +209,7 @@ Functions in myfunctionapp:
193209
Invoke url: https://myfunctionapp.azurewebsites.net/api/httptrigger?code=cCr8sAxfBiow548FBDLS1....
194210
```
195211
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.
212+
Copy the `Invoke url` value for your `HttpTrigger`. You can use it to test your function in Azure. The URL contains a `code` query string value. It's your function key. This key makes it difficult for others to call your HTTP trigger endpoint in Azure.
197213
198214
[!INCLUDE [functions-test-function-code](../../includes/functions-test-function-code.md)]
199215

0 commit comments

Comments
 (0)