In this lab, you will gain experience using the Azure Cloud Shell, the Azure CLI, and PowerShell to connect to and use Azure Event Grid.
First, you will use the Azure Cloud Shell to create an Event Grid topic and a subscription which will send the events to an Event Hub. Then, you’ll use the Azure Cloud Shell to send sample events to the Event Grid topic. Finally, you will verify events were sent from the Event Grid topic to the Event Hub using the Azure Portal.
Upon completion of the lab, you will have gained the experience required to work with Azure Event Grid using Azure Cloud Shell, the Azure CLI, and PowerShell.
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Use Cloud Shell/PS/CLI to create an Event Grid topic and subscription.
- Open the Azure portal by using the credentials above and the Open Azure Portal button.
- Once logged in, click on the Cloud Shell icon.
- Select PowerShell.
- Click Show Advanced Settings.
- Look in the Azure Portal for the three bar menu, select it, and click on All resources. Set the PowerShell’s Cloud Shell Region to match your existing resource’s location.
- Set Storage account to Use existing.
- Create a new file share and name it fileshare.
- Select Create Storage.
Once you see the PowerShell prompt, set the environment variables:
#get variables needed $resourceGroup = az group list --query '[0].name' --output tsv $location = az resource list --query '[0].location' --output tsv $namespace = az eventhubs namespace list --resource-group $resourceGroup --query '[0].name' --output tsv $hubId = az eventhubs eventhub list --resource-group $resourceGroup --namespace-name $namespace --query '[0].id' --output tsv
#create event grid topic and subscription $topicname = “topic-09152020” az eventgrid topic create --name $topicname -l $location -g $resourceGroup $topicId = az eventgrid topic list --query ‘[0].id’ --output tsv $ehSubName = “es09152020” az eventgrid event-subscription create --name $ehSubName --source-resource-id $topicId --endpoint $hubId --endpoint-type eventhub $eventID = Get-Random 99999 $eventDate = Get-Date -Format s $htbody = @{ id= $eventID eventType="recordInserted" subject="myapp/guitars/gibson" eventTime= $eventDate data= @{ make="Gibson" model="Les Paul" } dataVersion="1.0" } $body = "["+(ConvertTo-Json $htbody)+"]"
- Use Cloud Shell/PS to send a set of events to the Event Grid topic.
Continuing in PowerShell, we send a set of events to the Event Grid topic.
$endpoint = az eventgrid topic list --resource-group $resourceGroup --query '[0].endpoint' --output tsv $key = az eventgrid topic key list --name $topicname -g $resourceGroup --query "key1" --output tsv Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $key}
- Once we invoke a web request, the Status Code: 200 is displayed.
- Repeatly (around ten times) invoke the web request:
Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $key}
- Use Azure Portal to view Event Hub message count.
- Minimize the Cloud Shell window. Refresh the All resources page in the Azure portal.
- Click on the topic-09152020 resource.
- Scroll down and notice the count for Published Events, Matched Events, Delivered Events. Note that the Published Events count can have a delay (ours was around 2-3 mins) for the numbers to be displayed. Be sure to click Refresh.
- Clicking on the es09152020 link takes you to the Event Subscription view.
- In the Event Subscription view you can also access Event Hubs by clicking on the link. On this page you can view the number of Incoming and Successful Requests.