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
+91-75Lines changed: 91 additions & 75 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,151 @@ 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. 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.
17
17
18
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.
## 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
+
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`.
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 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
+
```
59
62
60
63
## Create a local Functions project
61
64
62
65
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.
63
66
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:
65
68
66
69
```console
67
70
func init MyFunctionProj
68
71
```
72
+
1. Select **python** as your worker runtime.
69
73
70
-
A folder named _MyFunctionProj_ is created, which contains the following three files:
74
+
The command creates a _MyFunctionProj_. It contains these three files:
71
75
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 functionapp. This file does get published to Azure.
75
79
76
-
Navigate to the new MyFunctionProj folder:
80
+
1. Go to the new *MyFunctionProj* folder:
77
81
78
-
```console
79
-
cd MyFunctionProj
80
-
```
82
+
```console
83
+
cd MyFunctionProj
84
+
```
81
85
82
86
## Create a function
83
87
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.
99
89
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:
101
91
102
-
## Run the function locally
92
+
```console
93
+
func new
94
+
```
103
95
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.
105
97
106
-
```console
107
-
func host start
108
-
```
98
+
1. When you're prompted for a functionname, enter *HttpTrigger* and then press Enter.
109
99
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:
111
101
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 bindingsin the `bindings` array.
113
103
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).
125
105
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 functionusing 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).
131
107
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).
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
143
111
144
-

112
+
The functionruns locally using the same Azure Functions runtime that is in Azure.
113
+
114
+
1. This command starts the functionapp:
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:
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
+

159
+
160
+
1. Select Ctrl+C to shut down your functionapp
145
161
146
162
Now that you have run your functionlocally, you can create the functionapp and other required resources in Azure.
147
163
@@ -153,31 +169,31 @@ Now that you have run your function locally, you can create the function app and
153
169
154
170
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
171
156
-
Run the following command using a unique functionapp namein place of the `<APP_NAME>`placeholder and the storage account namefor`<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.
172
+
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.
157
173
158
174
```azurecli-interactive
159
175
az functionapp create --resource-group myResourceGroup --os-type Linux \
> Linux and Windows apps cannot be hostedin 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.
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.
165
181
166
-
This command will also provision an associated Azure Application Insights instancein the same resource group that can be usedfor 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.
167
183
168
184
You're now ready to publish your local functions project to the functionappin Azure.
169
185
170
186
## Deploy the function app project to Azure
171
187
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 functionapp 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.
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.
212
+
Copy the `Invoke url` value foryour `HttpTrigger`. You can use it to test your functionin 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.
0 commit comments