Skip to content

Commit 3b539ab

Browse files
committed
First commit
1 parent 7546ce4 commit 3b539ab

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import json
2+
import logging
3+
4+
import azure.functions as func
5+
6+
7+
def main(event: func.EventGridEvent):
8+
result = json.dumps({
9+
'id': event.id,
10+
'data': event.get_json(),
11+
'topic': event.topic,
12+
'subject': event.subject,
13+
'event_type': event.event_type,
14+
})
15+
16+
logging.info('Python EventGrid trigger processed an event: %s', result)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"scriptFile": "__init__.py",
3+
"bindings": [
4+
{
5+
"type": "eventGridTrigger",
6+
"name": "event",
7+
"direction": "in"
8+
}
9+
]
10+
}

azurefunctioneventgridevent/README.md

+210
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,210 @@
1+
# Azure Function Event Grid event Python example
2+
3+
This folder contains a Python application example that handles Functions on Microsoft Azure.
4+
5+
It handles an Azure Function that responds to an Event Grid event (trigger) when an event is sent to an Event Grid topic.
6+
7+
## Requirements
8+
9+
* You must have a [Microsoft Azure](https://azure.microsoft.com/) subscription.
10+
11+
* The code was written for:
12+
* Python 3
13+
14+
* To develop functions app with Python, you must have the following installed:
15+
* Azure CLI
16+
* Azure Functions Core Tools Version 3.x
17+
18+
* Azure Functions Core Tools Version 3.x
19+
20+
Azure Functions Core Tools lets you develop and test your functions on your local computer from the command prompt or terminal. Your local functions can connect to live Azure services, and you can debug your functions on your local computer using the full Functions runtime. You can even deploy a function app to your Azure subscription.
21+
22+
Version 3.x/2.x: Supports either version 3.x or 2.x of the Azure Functions runtime. These versions support Windows, macOS, and Linux and use platform-specific package managers or npm for installation.
23+
24+
Azure Functions Core Tools currently depends on either the Azure CLI or Azure PowerShell for authenticating with your Azure account. This means that you must install one of these tools to be able to publish to Azure from Azure Functions Core Tools.
25+
26+
Version 3.x/2.x of the tools uses the Azure Functions runtime that is built on .NET Core. This version is supported on all platforms .NET Core supports, including Windows, macOS, and Linux.
27+
28+
Install version 3.x of the Core Tools on your local computer:
29+
30+
* For Windows:
31+
32+
1. Download and run the Core Tools installer, based on your version of Windows:
33+
34+
* v3.x - Windows 64-bit (Recommended. Visual Studio Code debugging requires 64-bit.)
35+
* v3.x - Windows 32-bit
36+
37+
2. If you don't plan to use extension bundles, install the .NET Core 3.x SDK for Windows.
38+
39+
* For MacOS:
40+
41+
1. Install Homebrew, if it's not already installed.
42+
2. Install the Core Tools package:
43+
44+
```bash
45+
brew tap azure/functions
46+
brew install azure-functions-core-tools@3
47+
# if upgrading on a machine that has 2.x installed
48+
brew link --overwrite azure-functions-core-tools@3
49+
```
50+
51+
3. If you don't plan to use extension bundles, install the .NET Core 3.x SDK for macOS.
52+
53+
## Using the code
54+
55+
* Create the Azure Funtion project and the Azure Function (Boilerplate code)
56+
57+
*This step is only necessary when you want to create an Azure Function from scratch.*
58+
59+
The Azure Functions Core Tools help you to create the boilerplate code for the Azure Funtion project and the Azure Function:
60+
61+
* Create an Azure Functions project
62+
63+
In the terminal window or from a command prompt, navigate to an empty folder for your project, and run the following command:
64+
65+
```bash
66+
func init azurefunctioneventgridevent
67+
```
68+
69+
In version 3.x/2.x, when you run the command you must choose a runtime for your project.
70+
71+
Select `python`
72+
73+
Then, the project is created with these files:
74+
75+
* `host.json` - JSON configuration file for the Function App.
76+
* `local.settings.json` - It stores app settings, connection strings, and settings used by local development tools. Settings in the local.settings.json file are used only when you are running projects locally.
77+
* `requirements.txt` - File with python dependencies.
78+
79+
*Because `local.settings.json` can contain secrets downloaded from Azure, the file is excluded from source control by default in the `.gitignore` file.*
80+
81+
* Create the Azure Function
82+
83+
In the terminal window or from a command prompt, move to the folder for your project, and run the following command:
84+
85+
```bash
86+
func new
87+
```
88+
89+
Select `Azure Event Grid trigger`
90+
91+
Select the name `EventGridTrigger`.
92+
93+
Then, the function `EventGridTrigger` is created successfully from the `Event Grid trigger` template.
94+
95+
The `EventGridTrigger` folder content is:
96+
97+
* `__init__.py` - Code of the function.
98+
* `function.json` - Configuration of the function.
99+
100+
* Create the Function App
101+
102+
1. You must create a Storage Account for the Function App, using the Azure console.
103+
104+
2. You must create the Function App. You can create it in two ways:
105+
106+
* Using the Azure console.
107+
108+
* Using the Azure CLI tool:
109+
110+
You create the Azure Function App by executing the command:
111+
112+
```bash
113+
az functionapp create --functions-version 3 --resource-group <RESOURCE_GROUP> --os-type Linux --consumption-plan-location westeurope --runtime python --name <FUNCTION_APP> --storage-account <STORAGE_ACCOUNT>
114+
```
115+
116+
In the previous command, replace with the proper:
117+
118+
* `<RESOURCE_GROUP>` - Resource group name.
119+
* `<FUNCTION_APP>` - Function App name.
120+
* `<STORAGE_ACCOUNT>`- Storage Account name.
121+
122+
* Configure the Azure Function
123+
124+
The `function.json` file is configurated.
125+
126+
The variable `name`, in the `function.json`, will hold the parameter that receives the event data.
127+
128+
* Deploy the function to Azure
129+
130+
The deploy process to Azure Functions uses account credentials from the Azure CLI. Log in with the Azure CLI before continuing.
131+
132+
```bash
133+
az login
134+
```
135+
136+
To publish your local code to a function app in Azure, use the publish command:
137+
138+
```bash
139+
func azure functionapp publish <FUNCTION_APP>
140+
```
141+
142+
When the deploy is complete, you see the URL that you can use to access your Azure Function App:
143+
144+
```bash
145+
Deployment successful.
146+
Remote build succeeded!
147+
Syncing triggers...
148+
Functions in <FUNCTION_APP>:
149+
EventGridTrigger - [eventGridTrigger]
150+
```
151+
152+
* Create an Event Grid Topic and subscribe to Event Subscription.
153+
154+
1. Create a custom topic (Event Grid Topic).
155+
156+
You must create the custom topic by creating an Event Grid Topic, using the Azure console.
157+
158+
An event grid topic provides a user-defined endpoint that you post your events to.
159+
160+
2. Subscribe to custom topic (Event Subscription).
161+
162+
You must subscribe to custom topic by creating an Event Subscription, using the Azure console.
163+
164+
You subscribe to an event grid topic to tell Event Grid which events you want to track, and where to send the events.
165+
166+
1. Enter a `name` for the event subscription.
167+
168+
2. Select `Azure Function` for the `Endpoint type`.
169+
170+
3. Choose `Select an endpoint`.
171+
172+
4. For the function endpoint, select the Azure Subscription and Resource Group your Function App is in and then select the Function App and function you created earlier. Select `Confirm Selection`.
173+
174+
* Test the function.
175+
176+
You must send an event to your topic.
177+
178+
In the Azure portal, select Cloud Shell. You use the Azure CLI and the `curl` command:
179+
180+
1. Select Bash in the top-left corner of the Cloud Shell window.
181+
182+
2. Run the following command to get the endpoint for the topic: After you copy and paste the command, update the `<TOPIC_NAME>` and `<RESOURCE_GROUP_NAME>` before you run the command.
183+
184+
```bash
185+
endpoint=$(az eventgrid topic show --name <TOPIC_NAME> -g <RESOURCE_GROUP_NAME> --query "endpoint" --output tsv)
186+
```
187+
188+
3. Run the following command to get the key for the custom topic: After you copy and paste the command, update the `<TOPIC_NAME>` and `<RESOURCE_GROUP_NAME>` before you run the command.
189+
190+
```bash
191+
key=$(az eventgrid topic key list --name <TOPIC_NAME> -g <RESOURCE_GROUP_NAME> --query "key1" --output tsv)
192+
```
193+
194+
4. Copy the following statement with the event definition, and press ENTER.
195+
196+
```bash
197+
event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motocycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]'
198+
```
199+
200+
5. Run the following `curl` command to post the event:
201+
202+
```bash
203+
curl -X POST -H "aeg-sas-key: $key" -d "$event" $endpoint
204+
```
205+
206+
You should see the next message in the log:
207+
208+
```bash
209+
Python EventGrid trigger processed an event: {"id": "<EVENT_ID>", "data": {"make": "Ducati", "model": "Monster"}, "topic": "/subscriptions/<SUBSCRIPTION_ID>/resourceGroups/<RESOURCE_GROUP_NAME>/providers/Microsoft.EventGrid/topics/<EVENT_GRID_TOPIC>", "subject": "myapp/vehicles/motocycles", "event_type": "recordInserted"}
210+
```

azurefunctioneventgridevent/host.json

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"version": "2.0",
3+
"logging": {
4+
"applicationInsights": {
5+
"samplingSettings": {
6+
"isEnabled": true,
7+
"excludedTypes": "Request"
8+
}
9+
}
10+
},
11+
"extensionBundle": {
12+
"id": "Microsoft.Azure.Functions.ExtensionBundle",
13+
"version": "[2.*, 3.0.0)"
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Do not include azure-functions-worker as it may conflict with the Azure Functions platform
2+
3+
azure-functions

0 commit comments

Comments
 (0)