Keeping your laboratory resource group clean with Azure DevOps scheduled pipelines

If you’re like me, you might have an Azure Subscription with a Resource Group where you create VMs, App Services, Blob Storages, etc, etc only for testing purposes (I create them, play with them a little, and after some time, I delete them).

But sometimes I forget to delete these resources after playing with them, that’s a problem because I don’t want to pay for something I’m not using, that’s when Azure DevOps come to rescue :)

There’s a way to clean all resources in a resource group by applying an ARM template to it. When you apply a template to a RG, there’s two deployment modes:

  1. Complete: with this mode, all resources that not exist in the ARM template but in the resource group will be deleted.
  2. Incremental: with this mode the template content will be added to the existent resources in the resource group.

So, applying an empty ARM template into a resource group with complete mode should delete all resources in that resource group.

Ok, but how can we automate this process? By using Azure DevOps Pipelines scheduled release trigger.

First of all, we need to copy that empty ARM template into our Azure DevOps Project to create an artifact with it and use it in the release pipeline. To do so I’m going to create a new repository called management with this folder structure:

/deploy/templates/resource-group/empty.json

and I’ll copy the empty template into that empty.json file.

After that, let’s create an empty Release Pipeline in Azure DevOps selecting the Empty Job template:

Then, click on Add an artifact, select Azure Repos Git as source type , select your project, repository and source branch and click on Add.

After that, we’ll have our empty template available in our release pipeline.

Note: I recommend to set up a Service Connection with contributor access only to the desired resource group and use it in the release pipeline to connect to Azure.

Then, we’re going to use the Azure resource group deployment task that’s avilable on Azure DevOps

We add this task to our release pipeline and configure:

  • Azure Subscription : select your service connection name.
  • Action: select “Create or update resource group”.
  • Resource Group: select your resource group name
  • Location: select your resource group location
  • Template location: select “Linked artifact”.
  • Template: select the path where your template is located.
  • Deployment mode: Complete

And that’s it!, I’ve named my pipeline Clean Test Environment. If you click on Save and Create Release It should execute successfully, and remove all content in your resource group.

Ok, now that we’ve checked that it works, we’re going to schedule this release to execute everyday at 00:00, to do so, we edit the release and click on schedule icon:

And configure the release trigger as follows:

With this we’ll ensure our testing resource group keeps clean.

developer, cloud based, DevOps enthusiast,cats fan . . .