Skip to content

Use GitHub Actions to remove Resource Groups conditionally

License

Notifications You must be signed in to change notification settings

alfredodeza/rm-resource-groups

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 

Repository files navigation

rm-resource-groups

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: O'Reilly

🎥 Click the image above to access the full course on O'Reilly

Check with consumption

Rough steps to double check with consumption over a certain dollar amount:

  1. List all resource groups for the given subscription: az group list
  2. 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
  1. 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']}")
  1. Nuke it with az group delete -n $GROUP_NAME

Nuke it based on name

Query the list that contains a name and produce matches:

az group list --query "[?name.contains(@, 'demo')]" | jq '.[] | .name'

🥇 Learn Objectives

  • 🚀 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

🔗 R E S O U R C E S

About

Use GitHub Actions to remove Resource Groups conditionally

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published