Use Azure Event Grid with Cloud Shell, PowerShell, and CLI

30 minutes
  • 3 Learning Objectives

About this Hands-on Lab

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.
  1. Open the Azure portal by using the credentials above and the Open Azure Portal button.
  2. Once logged in, click on the Cloud Shell icon.
  3. Select PowerShell.
  4. Click Show Advanced Settings.
  5. 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.
  6. Set Storage account to Use existing.
  7. Create a new file share and name it fileshare.
  8. Select Create Storage.
  9. Once you see the PowerShell prompt, set the environment variables:

    #get variables needed
    
    $resourceGroup = az group list --query '[0].name' --output json
    
    $location = az resource list --query '[0].location'
    
    $namespace = az eventhubs namespace list --resource-group $resourceGroup --query '[0].name' --output json 
    
    $hubId = az eventhubs eventhub list --resource-group $resourceGroup --namespace-name $namespace --query '[0].id' --output json
    #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 json
    
    $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.
  1. 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}
  2. Once we invoke a web request, the Status Code: 200 is displayed.
  3. 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.
  1. Minimize the Cloud Shell window. Refresh the All resources page in the Azure portal.
  2. Click on the topic-09152020 resource.
  3. 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.
  4. Clicking on the es09152020 link takes you to the Event Subscription view.
  5. 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.

Additional Resources

As a developer, you want to use the Azure Cloud Shell, the Azure CLI, and PowerShell to connect to and interact with Azure Event Grid.

To do this, you will use the 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.

The activity gives step-by-step instructions for this lab. Follow along with them and you will successfully connect to and interact with Azure Event Grid.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?