Prevent your Azure bill from running wild by using GitHub Actions to remove Resource Groups conditionally. In this lesson I show how you can automate the removal of Azure resources on demand or by schedule. With GitHub Actions and an Azure account you can prevent services from running wild and causing unnecessary billing overcharges!
With automation in place, there is no need to remember what VM you left running!
First, you'll start by creating a GitHub Action that authenticates to Azure, then you will query locally your resource groups to identify which ones you can delete. Next, you will port the query over to the Action workflow. Finally, you will delete resources that match, making sure those are fully removed, preventing a large bill.
The video is available on YouTube as well as in the O'Reilly platform in the below link:
🎥 Click the image above to access the full course on O'Reilly
Rough steps to double check with consumption over a certain dollar amount:
- List all resource groups for the given subscription:
az group list
- Get consumption: with
curl -H "Authorization: Bearer $AZURE_TOKEN" -X GET https://management.azure.com/subscriptions/$AZURE_SUBSCRIPTION_ID_DEMOS/providers/Microsoft.Consumption/usageDetails?api-version=2021-10-01
- From the consumption output, look at an existing group and then check dollar amount:
In [15]: for item in usage:
...: print(f"Resource Group: {item['properties']['resourceGroup']}")
...: print(f" Cost in USD: {item['properties']['costInUSD']}")
- Nuke it with
az group delete -n $GROUP_NAME
Query the list that contains a name and produce matches:
az group list --query "[?name.contains(@, 'demo')]" | jq '.[] | .name'
- 🚀 Create a GitHub Action workflow to query Resource Groups
- 🚀 Query Resource Groups that match locally
- 🚀 Use scripting to extract Resource Group names
- 🚀 Remove Resource Groups with the Azure CLI in GitHub Actions