![Azure Automation](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Azure-Automation_COLOR.png?resize=106%2C106&ssl=1)
![Event Grid](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/eventgrid.png?resize=106%2C106&ssl=1)
In this post, I demonstrate how to automatically purge content from an Azure Content Delivery Network (CDN) using Event Grid and Azure Automation. I am writing this post with two audiences in mind. First, if you have a CDN and need to purge old content every time content is updated or removed from a source, this post will show you how. However, if you don’t have a CDN but are interested in understanding how to configured Event Grid to trigger automation jobs in an Azure, this post is also for you.
Background
A CDN distributes and caches content across the globe, providing lower latency delivery and reducing global bandwidth usage. It may be necessary to rapidly refresh the cache when new content is added to the source. This post will demonstrate how to do that using Event Grid and Azure Automation.
If you would like to follow along at home, you will need an Azure CDN Configured with an Azure Storage Account V2 as the source. I have a post demonstrating how to set that up here. You will also need an Azure Automation account. If you don’t have an Azure Automation account, I have a whole playlist on setting one up here. The Azure Automation runbook used in this example can be found here.
Flow Overview
The basic flow of operations is as outlined below:
- Content added (or removed) from the Storage Account blob source.
- Add or Delete action on the blob storage triggers Event Grid.
- Event Grid runs a web hook for the Azure Automation runbook, passing JSON data including file path.
- The Automation runbook parses JSON and runs a purge job against the path.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Event-Grid-Flow.png?resize=257%2C311&ssl=1)
Azure Automation
The web hook is required to set up the Event Grid action, so let’s start there. Start by going to your Azure Automation account and create a runbook.
![Create Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Create-Purge-Runbook.png?resize=226%2C230&ssl=1)
Create Purge Runbook
For this example, I’m only going to publish a single write-output command as a placeholder. Remember, for now, I only need a web hook for the runbook. We will get to the working runbook shortly. Save and publish the runbook.
![PowerShell Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Publish-Temp-Runbook.png?resize=468%2C161&ssl=1)
Now that we have a published runbook we can create a web hook to trigger it. From the runbook, go to the Add web hook at the top of the page.
This will take you to the first of two steps to create a web hook. Give the new web hook a name, make sure it’s set to enabled and set an expiration date. Note the date, the web hook will stop working after that date. Lastly, copy and paste the URL someplace secure. You will not be able to retrieve this URL once the web hook has been created. If you lose it, a new one will need to be created.
![Webhook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Create-Webhook-Step-1.png?resize=469%2C352&ssl=1)
Click OK to move to the next step. Specify the run environment and parameters in the Modify run settings section. If the Runbook required parameters or needs to run on a hybrid worker, that could be set here. Neither applies to this example, so click OK and Create to add the web hook.
The CDN module is required to run the purge command. Add that by going into Modules Gallery in the automation account. Search for CDN. Add the Az.CDN module.
![Azure Automation Module](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Add-CDN-Module.png?resize=512%2C246&ssl=1)
If you are using the default AzureRM commands in the Automation Account, add the AzureRM.CDN module instead. Also change the Unpublish-AzCDNEndpointContent command in the script to the AzureRM command: Unpublish-AzureRMCDNEndpointContent.
Event Grid
Now that we have a web hook, let’s move onto setting up Event Grid. Start by going to the Storage Account used as the CDN source. Look for Events on the sidebar of the Storage Account.
![Event Grid](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Storage-Events.png?resize=239%2C312&ssl=1)
This will open the Events, Get Started page. Select the More Options option.
![Event Grid More Options](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Events-More-Options.png?resize=446%2C209&ssl=1)
You will see multiple options to choose from. Select the Web Hook option.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Web-Hook-Option.png?resize=486%2C429&ssl=1)
Give the Event Subscription a name. Leave the Event Schema and Topic Details as default. Notice that the Event Types have the option of Blob Created or Blob Deleted. Leave both selected. Under Endpoint Details, select Web Hook.
![Event Grid Subscription](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Create-Event-Subscription.png?resize=501%2C494&ssl=1)
Next, under Web Hook, click on Select and endpoint. This will bring up the Subscriber Endpoint. Enter the Web Hook URL created in the previous step. Click Confirm Selection and Create to add the event.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Select-Web-Hook.png?resize=565%2C149&ssl=1)
After the event is created, go to Event Subscriptions to verify. Notice you can watch the activity from the Event Subscriptions page. It is also possible to modify the event by clicking on the event name.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Event-Subscriptions.png?resize=1024%2C559&ssl=1)
Test the Event
Now that the web hook is in place, let’s test. The event will trigger when new blobs are uploaded or deleted from the container. To test, simply upload or remove content from the container. The example below uses Storage Explorer to download and then upload index.html. This adds content and triggers the event.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Blob-Upload.png?resize=545%2C283&ssl=1)
Events now show activity as shown below.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Event-Triggered.png?resize=499%2C451&ssl=1)
Go to the Azure Automation Runbook. Under Jobs, you will see a job completed. This is the job Event Grid triggered.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Azure-Automation-Job-Triggered.png?resize=608%2C165&ssl=1)
Click on the Job and go to Output. You will see that the job output, in this example a simple message.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Runbook-Output.png?resize=189%2C123&ssl=1)
The output is not very useful. More important is the input. Notice the input WEBHOOKDATA is JSON Code with details about the event. Copy the data into an editor, such as Visual Studio Code and save it as a .json file. I used the Prettier VS Code extension to format the content as JSON.
![Runbook Input](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Input-JSON-Data.png?resize=512%2C199&ssl=1)
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Formatted-JSON-Data.png?resize=512%2C293&ssl=1)
Under RequestBody, there are a couple of values that give path information. This information is used below to run a purge against a specific file each time it’s uploaded or deleted. This example will use the value assigned to “url” to build a path for the purge operation.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/URL-Path.png?resize=512%2C43&ssl=1)
Azure Automation Runbook
Almost there! The Event Trigger is configured to fire a runbook. The runbook is configured and we have the parameter needed to run the purge command. The last step is to build a runbook.
Below I step through each section of the runbook. If you are unfamiliar with creating runbooks, I have the YouTube Playlist for you! The complete runbook is available on my GitHub site here .
The first section in the script is the comments. After that is the parameter section. This adds the JSON input to the $Web hookData variable for use later in identifying the file path.
![Azure Automation Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Parameter-Section.png?resize=307%2C126&ssl=1)
After the parameter section is the authentication section. This authenticates the runbook with the Azure Automation Run As account, giving it permissions to run the purge command.
![Azure Automation Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Authentication-Section.png?resize=496%2C253&ssl=1)
The next section assigns the CDN Profile name, Resource Group and Endpoint Name to variables. Update these fields with values from your environment. I also set the default error action to a variable, this is used for error handling.
![Azure Automation Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Set-Variables.png?w=1200&ssl=1)
After that, I manipulate the JSON data to get the correct file path. The data I need is in the RequestBody section of the JSON, the first step is to assign that data to a new variable, $requestbody
![Azure Automation Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Variable-requestBody.png?resize=649%2C177&ssl=1)
Once that’s assigned, I assign the value of “url” located in data to the $filePath variable. The value of url includes the full FQDN and file path. I don’t need the FQDN for the purge command, only the file path. The replace command is used to replace the FQDN with nothing, removing the part of the url I don’t need. For example, if this is the full url:
https://cdntestcir.blob.core.windows.net/source/index.html
replacing “https://cdntestcir.blob.core.windows.net” with “nothing” will leave “/source/index.html”, the path needed for the purge command. Update the url value from your environment.
The full command looks like below.
![Azure Automation Runbook](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Convert-File-Path.png?resize=725%2C243&ssl=1)
The try, catch blocks are used for error handling. Errors encountered as the runbook runs are written to the Error stream. For more information on the Try, Catch, Finally, blocks, see “The Big Book of PowerShell Error Handling” available from PowerShell.org https://leanpub.com/u/devopscollective
Last, the unpublish-AzCDNEndpointContent command is used to purge the file. This command takes a minute or two to run.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Unpublish-AzCdnEndpointContent-Command.png?resize=512%2C120&ssl=1)
Final Testing
Test by updating or deleting a file from the CDN Source Storage Account. This will trigger Event Grid to send the web hook and cause the Runbook to run. The Azure Automation runbook will report the completed job once finished.
![](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Completed-Job.png?resize=1024%2C190&ssl=1)
The Activity Log on the CDN Endpoint will show a successful purge. Clicking on the drop-down arrow will show details of the job.
![Event Grid and Azure Automation](https://i0.wp.com/www.ciraltos.com/wp-content/uploads/2019/06/Successful-Purge.png?resize=1024%2C277&ssl=1)
Summary
Here we are at the end. This post demonstrated how to use Event Grid and Azure Automation to automate the process of purging content from a CDN when content is deleted from the source or new content is added. The CDN use case is fairly specific, but Event Grid is integrated into many Azure services and the same workflow could be applied to a multitude of services to automate within Azure.