You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: articles/azure-functions/functions-create-first-function-python.md
+96-84Lines changed: 96 additions & 84 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,7 +3,7 @@ title: Create an HTTP triggered function in Azure
3
3
description: Learn how to create your first Python function in Azure using the Azure Functions Core Tools and the Azure CLI.
4
4
author: ggailey777
5
5
ms.author: glenga
6
-
ms.date: 04/24/2019
6
+
ms.date: 09/11/2019
7
7
ms.topic: quickstart
8
8
ms.service: azure-functions
9
9
ms.custom: mvc
@@ -13,135 +13,146 @@ manager: gwallace
13
13
14
14
# Create an HTTP triggered function in Azure
15
15
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.
17
17
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.
## Create and activate a virtual environment (optional)
35
35
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`.
37
37
38
38
> [!NOTE]
39
39
> If Python didn't install venv on your Linux distribution, you can install it using the following command:
40
40
> ```command
41
41
> sudo apt-get install python3-venv
42
-
>
43
42
44
43
### Bash:
45
44
46
45
```bash
47
-
python3.6 -m venv .venv
46
+
python -m venv .venv
48
47
source .venv/bin/activate
49
48
```
50
49
51
50
### PowerShell or a Windows command prompt:
52
51
53
52
```powershell
54
-
py -3.6 -m venv .venv
53
+
py -m venv .venv
55
54
.venv\scripts\activate
56
55
```
57
56
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`.
59
58
60
-
## Create a local Functions project
59
+
## Create a local functions project
61
60
62
-
A Functions project is the equivalent of a functionappin Azure. It can have multiple functions that all share the same local and hosting configurations.
61
+
A functions project is the equivalent of a functionappin Azure. It can have multiple functions that all share the same local and hosting configurations.
63
62
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:
65
64
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
+
```
91
68
92
-
A subfolder named _HttpTrigger_ is created, which contains the following files:
69
+
1. Select **python** as your worker runtime.
93
70
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:
95
72
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.
97
76
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:
99
78
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
+
```
101
82
102
-
## Run the function locally
83
+
## Create a function
103
84
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.
105
86
106
-
```console
107
-
func host start
108
-
```
87
+
1. To add a function to your project, run the following command:
109
88
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
+
```
111
92
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.
125
94
126
-
...
95
+
1. When you're prompted for a functionname, enter *HttpTrigger* and then press Enter.
127
96
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:
131
98
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.
133
100
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).
**\_\_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 functionusing 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).
137
104
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).
141
106
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 localfunction:
107
+
## Run the function locally
143
108
144
-

109
+
The functionruns locally using the Azure Functions runtime.
110
+
111
+
1. This command starts the functionapp:
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:
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 localfunctionreturns to the browser:
152
+
153
+

154
+
155
+
1. Select Ctrl+C to shut down your functionapp.
145
156
146
157
Now that you have run your functionlocally, you can create the functionapp and other required resources in Azure.
147
158
@@ -153,31 +164,32 @@ Now that you have run your function locally, you can create the function app and
153
164
154
165
A functionapp provides an environment for executing your functioncode. It lets you group functions as a logical unit for easier management, deployment, and sharing of resources.
155
166
156
-
Run the following command using a unique functionapp 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 appsin Azure.
167
+
Run the following command. Replace `<APP_NAME>` with a unique functionapp name. Replace `<STORAGE_NAME>` with a storage account name. The `<APP_NAME>` is also the default DNS domain forthe function app. This name needs to be unique across all appsin 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.
157
171
158
172
```azurecli-interactive
159
173
az functionapp create --resource-group myResourceGroup --os-type Linux \
> Linux and Windows apps cannot be hosted in the same resource group. If you have an existing resource group named `myResourceGroup` with a Windows functionapp or web app, you must use a different resource group.
165
177
166
-
This commandwill 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.
167
179
168
180
You're now ready to publish your local functions project to the functionappin Azure.
169
181
170
182
## Deploy the function app project to Azure
171
183
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 functionappin 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.
Copy the `Invoke url` value foryour `HttpTrigger`, which you can now use to test your functionin Azure. The URL contains a `code` query string value that is your functionkey. This key makes it difficult forothers to call your HTTP trigger endpointin Azure.
208
+
You can copy the `Invoke url` value foryour `HttpTrigger` and use it to verify your functionin Azure. The URL contains a `code` query string value that is your functionkey, which makes it difficult forothers to call your HTTP trigger endpointin Azure.
> 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).
0 commit comments