Hoppa över navigering

Microsoft Azure Event Grid Publish/Consume Samples for Python

Senast uppdaterad: 2018-05-31
Redigera på GitHub

This contains Python samples for publishing events to Azure Event Grid and consuming events from Azure Event Grid. It also contains a set of management samples that demonstrates how to manage topics and event subscriptions.

Features

These samples demonstrates the following features:

Data Plane:

  • How to publish events to Azure Event Grid.
  • How to consume events delivered by Azure Event Grid.

The above two samples use the Event Grid data plane SDK azure-eventgrid.

Management Plane:

  • How to create a topic and an event subscription to a topic.
  • How to create an event subscription to a Storage account.
  • How to create an event subscription to an Azure subscription / Resource Group.

The above three samples use the Event Grid management plane SDK azure-mgmt-eventgrid

Getting Started

Prerequisites

Quickstart

  1. git clone https://github.com/Azure-Samples/event-grid-python-public-consume-events.git
  2. cd event-grid-python-public-consume-events
  3. pipenv install
  4. For Management only, rename the file env_template to .env and update the correct values inside with your subscription ID and Azure Service Principal credentials.

Running the samples

Management Plane

  1. create_eg_topics_and_event_subscriptions.py creates a new event subscription for an Event Grid Topic, and send it to a webhook

In order for this sample to work, you need to have a valid .env file (see previous section).

Run the sample : pipenv run python create_eg_topics_and_event_subscriptions.py

  1. create_storage_event_subscriptions.py creates a new event subscription for a storage account, filtering jpg creation, and send it to an Relay Hybrid Connection

In order for this sample to work, you need to have a valid .env file (see previous section).

Run the sample : pipenv run python create_storage_event_subscriptions.py

  1. create_arm_event_subscriptions.py creates a new event subscription for an Azure Subscription and send it to a Storage Queue

In order for this sample to work, you need to have a valid .env file (see previous section).

Run the sample : pipenv run python create_arm_event_subscriptions.py

Data plane

  1. event_grid_publisher.py publishes a custom event to a Topic. Topic might be created by the initial sample create_eg_topics_and_event_subscriptions.py or CLI.

In order for this sample to work, you need an Event Grid Topic and a Key (replace global variables in the script)

Run the sample : pipenv run python event_grid_publisher.py

  1. ConsumerFunction is an Azure Function app that receive an event from EventGrid. You can deploy the ConsumerFunction folder to an Azure Function using VSCode, this folder is ready to deploy. See the Readme in that directory for details.

If you just want the code, look at ConsumerFunction/ConsumerFunction/run.py.

Creating an Azure Function is out of the scope of this sample, you can follow CLI tutorial or Portal tutorial or using VSCode.

Resources

  • https://docs.microsoft.com/azure/event-grid/overview
  • https://docs.microsoft.com/python/api/overview/azure/event-grid