Skip to content

Instantly share code, notes, and snippets.

@ianphil
Created October 9, 2019 02:25
Show Gist options
  • Save ianphil/468209bf52706c748ed29ec9a221a3f6 to your computer and use it in GitHub Desktop.
Save ianphil/468209bf52706c748ed29ec9a221a3f6 to your computer and use it in GitHub Desktop.
Use AzureCLI to setup event subscription and EventGrid viewer
#!/usr/bin/env bash
# Variables
RESOURCE_GROUP_NAME=eventing
RESOURCE_GROUP_LOCATION=eastus
STORAGE_ACCOUNT_NAME=ipeventing
STORAGE_SKU=Standard_LRS
STORAGE_KIND=BlobStorage
STORAGE_ACCESS_TIER=Hot
SITE_NAME=ipeventsite
CONTAINER_NAME=testcontainer
EVENT_SUBSCRIPTION_NAME=ipeventsubname
ENDPOINT=https://$SITE_NAME.azurewebsites.net/api/updates
# Create resource group
az group create -n "$RESOURCE_GROUP_NAME" -l "$RESOURCE_GROUP_LOCATION"
# Create storage account, create container, and get Id/key
az storage account create -n "$STORAGE_ACCOUNT_NAME" -g "$RESOURCE_GROUP_NAME" -l "$RESOURCE_GROUP_LOCATION" --sku "$STORAGE_SKU" --kind "$STORAGE_KIND" --access-tier "$STORAGE_ACCESS_TIER"
STORAGE_ID=$(az storage account show --name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --query id --output tsv)
STORAGE_ACCESS_KEY="$(az storage account keys list --account-name "$STORAGE_ACCOUNT_NAME" --resource-group "$RESOURCE_GROUP_NAME" --query "[0].value" --output tsv)"
az storage container create -n "$CONTAINER_NAME" --account-key "$STORAGE_ACCESS_KEY" --account-name "$STORAGE_ACCOUNT_NAME"
# Deploy Azure Event Grid Viewer
az group deployment create -g "$RESOURCE_GROUP_NAME" --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" --parameters siteName=$SITE_NAME hostingPlanName=viewerhost
# Here you should navigate to https://$SITE_NAME.azurewebsites.net/. There will be a validation event fired while creating an event subscription in the next couple steps.
# Make sure EventGrid provider is registered for subscription
az provider register --namespace Microsoft.EventGrid
# Create event subscription on storage account
az eventgrid event-subscription create --source-resource-id "$STORAGE_ID" -n "$EVENT_SUBSCRIPTION_NAME" --endpoint $ENDPOINT
# Create a file locally and upload it. This will cause an event to be shown in the EventGrid viewer.
echo "Hello World" > testfile.txt
az storage blob upload --file testfile.txt --container-name "$CONTAINER_NAME" -n testfile.txt --account-key "$STORAGE_ACCESS_KEY" --account-name "$STORAGE_ACCOUNT_NAME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment